Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WSDL-first approach: How to specify different names for wsdl:port and wsdl:binding?

Tags:

wsdl

wcf

I am following WSDL-first (provided by our client) approach for developing WCF service but WSDLs generated from my wcf service is slightly different from WSDL provided to me by our client and because of this mismatch, client is facing difficulties to make a call to my service.

Client provided wsdl:

<wsdl:service name='CheckoutService'> <wsdl:port binding='tns:CheckoutBinding' name='CheckoutServicePort'> <soap:address location='place holder to service uri' /> </wsdl:port> </wsdl:service>


WSDL generated from wcf service:

<wsdl:service name="CheckoutService"> <wsdl:port binding="tns:CheckoutBinding" name="CheckoutBinging"> <soap:address location="place holder to service uri" /> </wsdl:port> </wsdl:service>

and, my service settings are as follows:

<endpoint name="CheckoutBinding" address="" binding="basicHttpBinding" bindingName="CheckoutServicePort" bindingConfiguration="basicHttpBinding" bindingNamespace="<<namespace>>" contract="<<contractname>>" />

I have used WSCF.Blue for generating server-stub code from the client provided wsdl and made minor changes in the generated code to emit WSDL exactly same as the one provided by client but i am not getting any idea about what change to make in the config file or in generated code to get the same "wsdl:port/@name" as in the client provided wsdl file.

As per this url, serviceendpoint name property is mapped to wsdl:port/@name and wsdl:binding/@name. Based on this, endpoint/@name attribute value in my config file is mapped to wsdl:port/@name and wsdl:binding/@name but i want different names to be mapped to wsdl:port/@name and wsdl:binding/@name attributes.

Kindly help me in achieving this.

like image 649
Niranjan Avatar asked Jan 16 '11 06:01

Niranjan


People also ask

What is port type and binding in WSDL?

PortType defines the abstract interface of a web service. Conceptually it is like a Java interface since it defines an abstract type and related methods. In WSDL the port type is implemented by the binding and service elements which indicate the protocols, encoding schemes etc to be used by a web service implementation.

What is port name in WSDL?

A port defines an individual endpoint by specifying a single address for a binding. The name attribute provides a unique name among all ports defined within in the enclosing WSDL document.

What are the two attributes of the port element in WSDL?

The port element has two attributes: name and binding . The name attribute provides a unique name among all ports defined within the enclosing WSDL document.

What is a WSDL binding?

A WSDL binding describes how the service is bound to a messaging protocol, particularly the SOAP messaging protocol. A WSDL SOAP binding can be either a Remote Procedure Call (RPC) style binding or a document style binding. A SOAP binding can also have an encoded use or a literal use.


1 Answers

You can try to implement IWsdlExportExtension and in ExportEndpoint modify wsdl:port/@name. Then implement IEndpointBehavior which will add your extension to an endpoint. To use your new behavior you have two choices:

  • Add behavior from code. When service is hosted in IIS you have to create custom ServiceHost and ServiceHostFactory. In self hosting you can just iterate endpoints and add behavior.
  • Add behavior from configuration. You must implement custom BehaviorExtensionElement, register this element and use it in endpointBehaviors related to your endpoint.

Here is simple example with extension element:

using System;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;

namespace CustomWsdlExtension    
{
    public class PortNameWsdlBehavior : IWsdlExportExtension, IEndpointBehavior
    {
        public string Name { get; set; }

        public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
        {
        }

        public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
        {
            if (!string.IsNullOrEmpty(Name))
            {
                context.WsdlPort.Name = Name;
            }
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }

    public class PortNameWsdlBehaviorExtension : BehaviorExtensionElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get 
            { 
                object value = this["name"];
                return value != null ? value.ToString() : string.Empty; 
            }
            set { this["name"] = value; }
        }

        public override Type BehaviorType
        {
            get { return typeof(PortNameWsdlBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new PortNameWsdlBehavior { Name = Name };
        }
    }
}

And configuration:

  <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="portName" type="CustomWsdlExtension.PortNameWsdlBehaviorExtension, CustomWsdlExtension" />
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="customPortName">
          <portName name="myCustomName" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="CustomWsdlExtension.Service">
        <endpoint address="" binding="basicHttpBinding" contract="CustomWsdlExtension.IService"
                  behaviorConfiguration="customPortName" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

My WSDL then looks like:

<wsdl:service name="Service">
    <wsdl:port name="myCustomName" binding="tns:BasicHttpBinding_IService">
        <soap:address location="http://localhost:2366/Service.svc" /> 
    </wsdl:port>
</wsdl:service>
like image 97
Ladislav Mrnka Avatar answered Sep 18 '22 08:09

Ladislav Mrnka