Click here to Skip to main content
14,302,993 members

Schema Importer Extension

Rate this:
2.60 (4 votes)
Please Sign up or sign in to vote.
2.60 (4 votes)
11 Nov 2007CPOL
Enabling custom proxy code generation.

Schema Importer Extensions

In addition to the runtime extensibility provided by IXmlSerializable, there is also a need for greater design-time control. When using Add Web Reference or wsdl.exe, the Web Services infrastructure interprets the WSDL of the target service and generates proxy classes to interact with that service. Customer feedback has indicated the need for even further control. Version 2.0 of the .NET Framework introduces several new features in this area to improve the usability of generated proxy classes. To enable custom proxy code generation, Schema Importer Extensions have been introduced.

A Schema Importer Extension is a type that can be registered with the Web Services infrastructure through code or via config, and will be called during the schema import process to allow an extension to interpret the schema and inject code into the proxy. During schema import, each extension is called in the configured order for each schema type encountered, and the extension can choose to inject code or to simply ignore that type in the schema. This mechanism allows developers to build extensions that will map schema constructs matched by name, namespace, and/or shape to custom classes or code.

This type of control is useful when the client of a Web Service has custom types that are much richer than those generated by wsdl.exe that the developer wants to be used by the proxy. Prior to .NET Framework 2.0, this was possible only by modifying the generated proxy. These changes were lost when the proxy was regenerated. Schema Importer Extensions can now be developed and registered to map the schema type to the custom type every time the proxy is generated.

The following example shows a simple example of using a Schema Importer Extension to map a schema type to an existing client class:

using System;
using System.Collections;

To allow this extension to execute when generating proxy code, you need to register it in machine.config using the following configuration section:

<add name="Ambuj" type="Test, testp, 
    Version=, Culture=neutral, PublicKeyToken=abcdef0123456489" /> 

The code:

using System.Collections.Generic;
using System.Collections.Specialized;

using System.Text;
using System.Xml.Serialization; 

using System.Xml.Schema;
using System.Xml;

using System.CodeDom;
using System.CodeDom.Compiler; 

public class CustomerSchemaImporterExtension : SchemaImporterExtension

    public override string ImportSchemaType(string name, string ns,
           XmlSchemaObject context, XmlSchemas schemas, 
           XmlSchemaImporter importer, CodeCompileUnit compileUnit, 
           CodeNamespace mainNamespace, CodeGenerationOptions options, 
           CodeDomProvider codeProvider)
        // Check if the namespace and type name match.
        if (ns == "http://ambuj" && name == "Customer")
            // namespace.
            mainNamespace.Imports.Add(new CodeNamespaceImport("TestService_AM"));

            // Indicate that no XML schema type should be imported but that a
            // well-known CLR type will be used.

            CodeTypeDeclaration speaker = new CodeTypeDeclaration("Customer");
            CodeMemberField ageField = 
                new CodeMemberField(new CodeTypeReference(typeof(string)), 

            return "TestService_AM.Customer";
            // No match, 
            return base.ImportSchemaType(name, ns, context, schemas, importer,
                   compileUnit, mainNamespace, options, codeProvider);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ambuj kumar
Web Developer
Ambuj kumar
Software Engineer
4 years exp. on WinForm, Addins and Web Services

Work place - Hyderabad

Comments and Discussions

-- There are no messages in this forum --
Posted 11 Nov 2007


7 bookmarked