Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...