开发者

wcf - wsdl change port type and binding

开发者 https://www.devze.com 2023-04-10 18:44 出处:网络
I am new to wcf and trying to create a webservice from a client provided wsdl; I\'m having trouble changing some wcf generated wsdl entries to match the provided wsdl. I found this: WSDL-first approac

I am new to wcf and trying to create a webservice from a client provided wsdl; I'm having trouble changing some wcf generated wsdl entries to match the provided wsdl. I found this: WSDL-first approach: How to specify different names for wsdl:port and wsdl:binding? which exactly describes the issue i'm having but the solution provided there is not working in Visual Studio 2010 with .NET 4.0.

Here is the web config:

    <?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0">
        </compilation>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
    <system.serviceModel>

    <extensions>
            <behaviorExtensions>
                <add name="portName" type="CustomWsdlExtension.PortNameWsdlBehaviorExtension, CustomWsdlExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            </behaviorExtensions>
        </extensions>

    <services>
      <service name="CustomWsdlExtension.Service" behaviorConfiguration="MyBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="CustomWsdlExtension.IService" behaviorConfiguration="customPortName"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>

    <behaviors>
            <serviceBehaviors>
                <behavior name="MyBehavior">
         开发者_高级运维           <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>

      <endpointBehaviors>
                <behavior name="customPortName">
                    <portName name="myCustomName"/>
                </behavior>
            </endpointBehaviors>
        </behaviors>


        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
    </system.webServer>
</configuration>

And the custom class:

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 }; 
        } 
    } 
}     

It compiles ok (I have a warning web config saying The element 'behavior' has invalid child element 'portName'. List of possible elements expected: 'clientVia, callbackDebug, callbackTimeouts, clear, clientCredentials, transactedBatching, dataContractSerializer, dispatcherSynchronization, remove, synchronousReceive, enableWebScript, webHttp, endpointDiscovery, soapProcessing' which seems to be related to a bug in VS)

The generated wsdl still shows wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1">' rather than the modified port name.

You can find the whole test project here: http://dl.dropbox.com/u/13875536/CustomWsdlExtension.zip Thanks in advance.


It doesn't work because your web.config doesn't reflect your service and contract so the configuration is not used at all.

This part is wrong:

<services>
  <service name="CustomWsdlExtension.Service" ... >
    <endpoint contract="CustomWsdlExtension.IService" ... />
    ... 
  </service>
</services>

It must use full name of your service class and service contract. You just copied configuration from linked answer - it is not enough. Configuration must fit your classes to be used:

<services>
  <service name="CustomWsdlExtension.Service1" ... >
    <endpoint contract="CustomWsdlExtension.IService1" ... />
    ... 
  </service>
</services>

Also the previous answer doesn't show how to change the binding name. There is no special coding for that. Endpoint element in configuration has bindingName and bindingNamespace attributes for that purpose.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号