Those who've done a lot of web service development in .net 1.1, like myself, will welcome the new SehcmaImporterExtension with open arms! Finally we have an easy way to customize the proxy generation on the client in a manner that is easy to share types across both service and consumer. In 1.1 this was really not possible using the auto proxy generation tools, and even if you did customize the Reference.cs file it would just be overwritten every time you refreshed the reference. Well, now, thats all over with! There is a new
article on Asp.net written by Jelle Druyts, which discusses this new extension and shows you how to implement both basic and advanced scenarios. After reading the article, I was very excited by the possibilities. Not to mention the amount of time and flexibility I would gain. Heres an excerpt from the article which assumes a shared type of Customer with a namespace specified in its XmlType attribute:
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://schemas.samplebusiness.net/SharedTypes" && name == "Customer")
{ // Add a 'using' directive ('Imports' in VB.NET) for the CLR namespace.
mainNamespace.Imports.Add(new CodeNamespaceImport("SampleBusinessTypes"));
// Indicate that no XML schema type should be imported but that a well-known CLR type will be used.
return "SampleBusinessTypes.Customer";
}
else
{ // No match, delegate to the base class.
return base.ImportSchemaType(name, ns, context, schemas, importer,
compileUnit, mainNamespace, options, codeProvider);
}
}
}
So, not bad at all so far. Granted this is a simple scenario, but still something that I could use right away with some of my existing apps. Now that the extension is created on your client app, all you have to do is register it. Which can easily be done in the app config file:
<system.xml.serialization>
<schemaImporterExtensions>
<add name="CustomerSchemaImporterExtension" type="JelleDruyts.SchemaImporterExtensions.CustomerSchemaImporterExtension
JelleDruyts.SchemaImporterExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c88d0fcd698a2de7">
</add>
</schemaImporterExtensions>
</system.xml.serialization>
And thats it! Now everytime the proxy is generated (web reference refresh), the Customer class will be successfully mapped to the type in your shared library. Very cool stuff.