wcf - wsdl更改端口类型和绑定

时间:2011-10-05 20:20:30

标签: .net wcf web-services wsdl

我是wcf的新手,并尝试从客户端提供wsdl创建web服务;我无法更改一些wcf生成的wsdl条目以匹配提供的wsdl。我发现了这个:WSDL-first approach: How to specify different names for wsdl:port and wsdl:binding? 它确切地描述了我遇到的问题,但提供的解决方案在Visual Studio 2010中不能与.NET 4.0一起使用。

这是网络配置:

    <?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>

自定义类:

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

它编译正常(我有一个警告web配置说元素'behavior'具有无效的子元素'portName'。预期的可能元素列表:'clientVia,callbackDebug,callbackTimeouts,clear,clientCredentials,transactedBatching,dataContractSerializer ,dispatcherSynchronization,remove,synchronousReceive,enableWebScript,webHttp,endpointDiscovery,soapProcessing',这似乎与VS中的错误有关)

生成的wsdl仍显示wsdl:port name =“BasicHttpBinding_IService1”binding =“tns:BasicHttpBinding_IService1”&gt;'而不是修改后的端口名称。

您可以在此处找到整个测试项目:http://dl.dropbox.com/u/13875536/CustomWsdlExtension.zip 提前谢谢。

1 个答案:

答案 0 :(得分:0)

它不起作用,因为您的web.config不反映您的服务和合同,因此根本不使用配置。

这部分错了:

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

必须使用服务类和服务合同的全名。您刚从链接的答案中复制了配置 - 这还不够。配置必须适合您要使用的类:

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

前面的答案也没有显示如何更改绑定名称。对此没有特殊的编码。 Endpoint元素具有bindingNamebindingNamespace属性。