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:
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=188.8.131.52, Culture=neutral, PublicKeyToken=abcdef0123456489" />
public class CustomerSchemaImporterExtension : SchemaImporterExtension
public override string ImportSchemaType(string name, string ns,
XmlSchemaObject context, XmlSchemas schemas,
XmlSchemaImporter importer, CodeCompileUnit compileUnit,
CodeNamespace mainNamespace, CodeGenerationOptions options,
if (ns == "http://ambuj" && name == "Customer")
CodeTypeDeclaration speaker = new CodeTypeDeclaration("Customer");
CodeMemberField ageField =
new CodeMemberField(new CodeTypeReference(typeof(string)),
return base.ImportSchemaType(name, ns, context, schemas, importer,
compileUnit, mainNamespace, options, codeProvider);