次の方法で共有


Importing and Exporting WSDL Annotations

How do I add custom annotations to the contracts that are generated from WSDL?

You first need to start with an IWsdlImportExtension. Your extension gets called each time a contract is discovered during import. Processing happens in multiple passes so we aren't quite ready to mess with generated contract yet. Instead, this is the opportunity to setup some deferred work that can be run once the contract is fully resolved.

 class MyWsdlImporter : IWsdlImportExtension
{
   public void BeforeImport(ServiceDescriptionCollection wsdlDocuments, XmlSchemaSet xmlSchemas, ICollection<XmlElement> policy)
   {
   }

   public void ImportContract(WsdlImporter importer, WsdlContractConversionContext context)
   {
      context.Contract.Behaviors.Add(new MyServiceContractGenerator());
      foreach (Operation operation in context.WsdlPortType.Operations)
      {
         OperationDescription description = context.Contract.Operations.Find(operation.Name);
         if (description != null)
         {
            description.Behaviors.Add(new MyOperationContractGenerator());
         }
      }
   }

   public void ImportEndpoint(WsdlImporter importer, WsdlEndpointConversionContext context)
   {
   }
}

You'd probably want to be a little bit more discriminating about which contracts you attach to and you'd probably want to actually pass some data to the contract generator so that it knows how to modify the contracts. Let's ignore those little problems and look at the contract generators.
The contract generator gets invoked once it's time to actually mess with the generated contract.

 class MyServiceContractGenerator : IServiceContractGenerationExtension, IContractBehavior
{
   public void GenerateContract(ServiceContractGenerationContext context)
   {
      // muck with generated service contract
   }

   #region IContractBehavior Members
    ...
   #endregion
}

class MyOperationContractGenerator : IOperationContractGenerationExtension, IOperationBehavior
{
   public void GenerateOperation(OperationContractGenerationContext context)
   {
      // muck with generated operation contract
   }

   #region IOperationBehavior Members
    ...
   #endregion
}

The IWsdlImportExtension plugs in through the configuration file of the WSDL importer (typically svcutil.exe).

 <client>
   <metadata>
      <wsdlImporters>
         <extension type=" ... " />
      </policyImporters>
   </metadata>
</client>

You should be able to figure out how the reverse process works starting from some strategic examination of IWsdlExportExtension.

Next time: A Proxy Proxy Factory

Comments

  • Anonymous
    January 25, 2008
    How do I perform XML validation against an entire message? There is a method to read the body of the

  • Anonymous
    May 20, 2008
    I've previously talked about using WSDL extensions to provide custom modifications to the WSDL import