【问题标题】:wcf - wsdl change port type and bindingwcf - wsdl 更改端口类型和绑定
【发布时间】:2011-10-05 20:20:29
【问题描述】:

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

这是网络配置:

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

它编译正常(我有一个警告网络配置说 元素“行为”具有无效的子元素“端口名称”。预期可能的元素列表:“clientVia、callbackDebug、callbackTimeouts、clear、clientCredentials、transactedBatching、dataContractSerializer ,dispatcherSynchronization,remove,synchronousReceive,enableWebScript,webHttp,endpointDiscovery,soapProcessing'这似乎与VS中的一个bug有关)

生成的 wsdl 仍然显示 wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1">' 而不是修改后的端口名称。

你可以在这里找到整个测试项目:http://dl.dropbox.com/u/13875536/CustomWsdlExtension.zip 提前致谢。

【问题讨论】:

    标签: .net wcf web-services wsdl


    【解决方案1】:

    它不起作用,因为您的 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 属性。

    【讨论】:

    • 哎呀新手错误!工作得很好。但是更改 bindingName 属性只会更改绑定的第一部分,它仍然将合同名称连接到所选名称。 wsdl 显示:&lt;wsdl:port name="myCustomName" binding="tns:MyCustomBindingName_IService1"&gt; 而不是 &lt;wsdl:port name="myCustomName" binding="tns:MyCustomBindingName"&gt;。有任何想法吗?再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2011-10-19
    相关资源
    最近更新 更多