Writing custom WsdlImportExtension

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Here is an example which shows how to implement IWsdlImportExtension, which imports the custom JMS address from the port type.
This example shows how to:

  1. Recognize custom address which is not SOAP prefixed
  2. Read required custom attributes from the address node
  3. Remove the port extension to prevent warning generation
using System;
using
System.Collections.Generic;
using
System.Text;
using
System.ServiceModel.Description;
using
System.Collections.ObjectModel;
using
System.ServiceModel.Channels;
using System.Xml;
 
namespace MyNamespace
{
   
/// <summary>
    /// This class is used to import the address of the port defined for using of JMS.
    /// </summary>
    public class JmsWsdlImporter : IWsdlImportExtension
    {
        #region
IWsdlImportExtension Members

        private string m_DestinationStyle;
       
public string
DestinationStyle
        {            
get { return
m_DestinationStyle; }
        }
        private string m_InitialContextFactory;
        public string InitialContextFactory
        {
           
get { return
m_InitialContextFactory; }
        }

       
private string
m_JndiConnectionFactoryName;
       
public string
JndiConnectionFactoryName
        {           
get { return
m_JndiConnectionFactoryName; }
        }
       
private string
m_JndiDestinationName;
       
public string
JndiDestinationName
        {
           
get { return
m_JndiDestinationName; }
        }

       
private string
m_JndiProviderURL;
        
public string
JndiProviderURL
        {
           
get { return
m_JndiProviderURL; }
        }

        /// <summary>
        /// Not used
        /// </summary>
        ///
<param name="wsdlDocuments"></param>
        ///
<param name="xmlSchemas"></param>
        ///
<param name="policy"></param>         public void BeforeImport(System.Web.Services.Description.ServiceDescriptionCollection wsdlDocuments,
       System.Xml.Schema.
XmlSchemaSet xmlSchemas, ICollection<System.Xml.XmlElement> policy)
        {
            return;
        }

        /// <summary>
        ///
Not used
        /// </summary>

        /// <param name="importer"></param>
        /// <param name="context"></param>
        public void ImportContract(WsdlImporter importer, WsdlContractConversionContext context)
 
       {             return;         }

        public void ImportEndpoint(WsdlImporter importer, WsdlEndpointConversionContext context)
        {
            if (context.WsdlPort != null)
            {                 XmlElement node = context.WsdlPort.Extensions.Find("address", http://schemas.xmlsoap.org/wsdl/jms/);
                if (node != null)                 {
                    foreach (XmlAttribute attr in node.Attributes)                     {
                        // Example in the Port definition in the WSDL
                       /*
                          <port name="jmsPort" binding="port_type:My_jmsSoap">                             <jms:address destinationStyle='topic' initialContextFactory='fr.dyade.aaa.jndi2.client.NamingContextFactory'                                     jndiConnectionFactoryName="JBITopicConnectionFactory" jndiDestinationName="AddOrderTopic"
                                    jndiProviderURL="scn://host:16400" />
                          </port>
                         */
                        switch (attr.LocalName.ToLower())
                        {
                            case "destinationstyle":
                               
this
.m_DestinationStyle = attr.Value;
                                
break
;

                           
case "initialcontextfactory"
:
                               
this
.m_InitialContextFactory = attr.Value;
                               
break
;

                           
case "jndiconnectionfactoryname"
:
                               
this
.m_JndiConnectionFactoryName = attr.Value;
                               
break
;

                           
case "jndidestinationname":
                                this.m_JndiDestinationName = attr.Value;
                                break;

                            case "jndiproviderurl":
                               
this
.m_JndiProviderURL = attr.Value;
                               
break
;
                        }
                        context.Endpoint.Address = new System.ServiceModel.EndpointAddress(http://a);
                    }


                    // This ensures that no warning is generated after importing of metadata.
                    // Please note that import process with all installed extensions has to
                    // remove all read policies. All not recognized policies will be served as an error
                    // which is stored in the WsdlImporter.Errors property.
                    context.WsdlPort.Extensions.Remove(node);
                }
            }
        }
        #endregion
    }
}

Following example shows how to start metadata importing process:

        public void IMportMetadata()
        {
            MetadataExchangeClient mexClient = new MetadataExchangeClient(new Uri("http://localhost:8001/mex"),
           
MetadataExchangeClientMode.HttpGet);
           
            mexClient.ResolveMetadataReferences =
true;
           
MetadataSet metaDocs = mexClient.GetMetadata();

           
WsdlImporter wsdlImporter = new WsdlImporter(metaDocs);
           
SbbPolicyImporter customImporter = new SbbPolicyImporter(SbbNetCoreHelper.GetDefaultSbbCore());
           
SbbWsdlImporter sbbWsdlImporter = new SbbWsdlImporter();

            importer.WsdlImportExtensions.Add(sbbWsdlImporter);
           
ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
        }


Posted Apr 17 2007, 07:08 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.