【发布时间】: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