Click here to Skip to main content
15,888,008 members
Articles / Web Development / ASP.NET

Schema Importer Extension

Rate me:
Please Sign up or sign in to vote.
2.60/5 (4 votes)
11 Nov 2007CPOL2 min read 17.9K   7  
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:

C#
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:

XML
<system.xml.serialization>
<schemaImporterExtensions> 
<add name="Ambuj" type="Test, testp, 
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdef0123456489" /> 
</schemaImporterExtensions>
</system.xml.serialization>

The code:

C#
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.
            compileUnit.ReferencedAssemblies.Add("TestService_AM.dll");
            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");
            mainNamespace.Types.Add(speaker);
            CodeMemberField ageField = 
                new CodeMemberField(new CodeTypeReference(typeof(string)), 
                "firstNameField");

            speaker.Members.Add(ageField);
            return "TestService_AM.Customer";
        }
        else
        {
            // No match, 
            return base.ImportSchemaType(name, ns, context, schemas, importer,
                   compileUnit, mainNamespace, options, codeProvider);
        }
    }
}

License

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


Written By
Web Developer
Unknown
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 --