Eric's Blog

Day to day experience in .NET
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

The Small Things: SchemaImporterExtension

In .NET 2.0 there is a great extensibility point for the wsdl.exe and xsd.exe called schema importer extension. By writing a class that inherits from SchemaImporterExtension and overrides the ImportSchemaType, you can affect how the tools generates the code. This is useful if you have created a class in your service that you want to share with the client too.
 
    1 public class ContractsSchemaImporterExtension : SchemaImporterExtension
    2 {
    3     public override string ImportSchemaType(string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
    4     {
    5         if (ns.StartsWith("MyNamespace"))
    6         {
    7             mainNamespace.Imports.Add(newCodeNamespaceImport("Company.MyDotNetNamespace"));
    8 
    9             switch (name)
   10             {
   11                 case "CreateUserRequest":
   12                     return typeof(CreateUserRequest).Name;
   13 
   14 
   15         return base.ImportSchemaType(name, ns, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
   16     }
   17 }
 
In the implementation you define how the incoming XSD types should map to the existing .NET types. In line 5 I start by checking that I recognizes the namespace and in that case adds the .NET namespace where my types exists. Of course this line will have to move into the case if the .NET types belongs to different .NET namespaces. In the switch I check the XSD types and returns the .NET type I want to represent it with.
To configure the tools to use the SchemaImporterExtension you have two choices; either to add a <schemaImporterExtension> element to the <system.xml.serialization> element in the machine.config file, or to supply a list of schema importer extensions via the /parameter option of the tools. Personally I prefer the first alternative because then I can use the tools through Visual Studio (in the Add Web Reference dialog for example).
 
Published den 10 oktober 2006 13:47 by ericqu

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Erics Blog said:

The XmlSerializer has some minor, but great new features. A while ago I blogged about how it is possible

oktober 31, 2006 09:11
 

sapna said:

I created schemaimporter extension and used it with wsdl tool Parameter option. But getting error specified assembly is not found.But schemaimporter is installed in GAC. where else do i need to place this dll .
november 12, 2008 07:14
 

ericqu said:

That should be enough, but you will probably have to add full assembly reference (version, public key and so on). Have you done that?

Personally I haven't used wsdl with parameter option.

november 12, 2008 08:00

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems