wsHttpBinding被识别为BasicHttpBinding的WCF服务配置问题
WCF服务强制使用wsHttpBinding的配置问题
问题场景
我有一个包含多个IIS托管WCF服务项目的解决方案,所有服务均配置为wsHttpBinding。使用WCF测试客户端引用各.svc文件时,其他服务均能被正确识别为wsHttpBinding,但有一个服务例外。该服务的web.config与其他项目完全一致,且代码中无服务模型相关设置,原因不明。
为进一步排查,我新建了一个仅包含单个WCF服务项目的解决方案,采用「WCF Service Application」模板(即带有内置GetData(int value)和GetDataUsingDataContract(CompositeType composite)方法的Service1)。我将绑定修改为wsHttpBinding并暴露了mex端点,但WCF测试客户端及其他引用该服务的项目仍将其识别为BasicHttpBinding。
使用的配置如下:
<system.serviceModel> <bindings> <wsHttpBinding> <binding name = "wsHttpBinding_IService1"> <security mode = "Message"> <transport clientCredentialType = "Windows" proxyCredentialType = "None" realm = ""/> <message clientCredentialType = "Windows" negotiateServiceCredential = "true"/> </security> </binding> </wsHttpBinding> </bindings> <services> <service name = "Service1"> <endpoint address = "" binding = "wsHttpBinding" contract = "WcfServiceTest.IService1" bindingConfiguration = "wsHttpBinding_IService1"> <identity> <dns value = "localhost"/> </identity> </endpoint> <endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel>
请问除了通过代码方式(如微软示例所示)外,还有什么方法可以强制该服务使用wsHttpBinding?
非代码方式解决方案
- 修正服务的完全限定名:配置中
<service name>必须使用服务实现类的完整命名空间+类名,比如示例中应改为WcfServiceTest.Service1而非Service1。WCF会因为服务名称不匹配,自动回退到默认的BasicHttpBinding,这是最常见的原因。 - 明确绑定配置的引用:确保端点的
bindingConfiguration属性准确指向定义好的wsHttpBinding名称,同时避免存在未命名的默认wsHttpBinding节点,防止配置被覆盖。 - 对齐IIS应用程序池的.NET版本:确认服务所在的IIS应用程序池使用的.NET Framework版本,与项目目标框架版本一致。版本不匹配会导致配置解析异常,触发默认绑定逻辑。
- 清理元数据缓存:WCF测试客户端和引用项目可能缓存了旧的BasicHttpBinding元数据:
- 关闭WCF测试客户端,删除
%USERPROFILE%\AppData\Roaming\Microsoft\WCF Test Client路径下的缓存文件 - 在引用服务的项目中删除现有服务引用,清理项目后重新添加
- 关闭WCF测试客户端,删除
- 显式绑定服务行为:给服务行为指定名称,并在服务节点中明确引用,避免隐式行为的不确定性:
<behaviors> <serviceBehaviors> <behavior name="DefaultServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <!-- 服务节点中指定behaviorConfiguration --> <service name="WcfServiceTest.Service1" behaviorConfiguration="DefaultServiceBehavior"> <!-- 端点配置 --> </service> - 检查.svc文件的工厂设置:确认.svc文件中没有指定会强制切换到BasicHttpBinding的工厂类,默认的.svc内容应为:
不要包含<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceTest.Service1" CodeBehind="Service1.svc.cs" %>Factory="System.ServiceModel.Activation.WebServiceHostFactory"这类会改变绑定类型的配置。
内容的提问来源于stack exchange,提问作者Nir
相关产品推荐
相关产品推荐

