如何让桌面WCF项目生成的WSDL匹配Web项目的.asmx WSDL?
如何让WCF项目生成的WSDL匹配ASMX服务的WSDL格式
对齐绑定类型,使用ASMX兼容的BasicHttpBinding
ASMX服务默认采用BasicHttpBinding,确保WCF项目配置中使用该绑定而非其他WCF专属绑定。在App.config中添加以下配置:<bindings> <basicHttpBinding> <binding name="AsmxCompatibleBinding"> <security mode="None" /> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> </binding> </basicHttpBinding> </bindings>服务端点需指定该绑定配置:
<services> <service name="YourNamespace.YourService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="AsmxCompatibleBinding" contract="YourNamespace.IYourService" /> </service> </services>匹配服务契约的命名空间与操作Action
从客户的asmx?WSDL中提取目标命名空间、操作的Action和ReplyAction,确保WCF契约属性完全一致。示例VB.NET代码:<ServiceContract(Namespace:="http://your-customer-asmx-namespace.com/")> Public Interface IYourService <OperationContract(Action:="http://your-customer-asmx-namespace.com/TargetOperation", ReplyAction:="http://your-customer-asmx-namespace.com/TargetOperationResponse")> Function TargetOperation(ByVal input As RequestModel) As ResponseModel End Interface调整数据契约的序列化规则
ASMX默认使用XML序列化,WCF需对齐该规则:- 若使用
Serializable属性,直接标记实体类:<Serializable> Public Class RequestModel Public Property Id As Integer Public Property Content As String End Class - 若使用
DataContract,需指定与ASMX一致的命名空间:<DataContract(Namespace:="http://your-customer-asmx-namespace.com/")> Public Class RequestModel <DataMember> Public Property Id As Integer <DataMember> Public Property Content As String End Class
- 若使用
禁用WCF专属的WSDL扩展元素
WCF默认生成的WSDL包含ASMX没有的扩展内容,可通过配置移除:<behaviors> <serviceBehaviors> <behavior name="AsmxCompatibleBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <remove name="WsdlExporter" /> <add name="WsdlExporter" type="System.ServiceModel.Description.WsdlExporter, System.ServiceModel" /> </behavior> </serviceBehaviors> </behaviors>服务节点需引用该行为配置:
<service name="YourNamespace.YourService" behaviorConfiguration="AsmxCompatibleBehavior"> <!-- 端点配置 --> </service>反向生成兼容契约(备选方案)
直接基于客户的ASMX WSDL生成VB.NET代码,确保完全匹配:
打开命令提示符执行:wsdl.exe /language:VB http://customer-domain/their-service.asmx?WSDL用生成的代码替换现有WCF契约,再搭建服务即可。
若以上步骤仍未解决问题,提供以下信息便于排查:
- 客户ASMX WSDL的关键片段(命名空间、操作定义、数据结构)
- 你的WCF服务契约代码
- App.config的完整服务配置
- SoapUI中请求格式差异的具体标注截图
内容的提问来源于stack exchange,提问作者StackHelp
相关产品推荐
相关产品推荐

