我正在尝试创建具有匿名身份验证的 WCF 服务。当我将服务部署到不在当前域中的目标服务器时,我在尝试调用它时收到以下错误:
Content Type application/soap+xml; charset=utf-8 was not supported by the service http://myserver.com/page.svc. The client and service bindings may be mismatched.
就目前情况而言,我的 web.config 文件中有以下部分用于该服务:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
我一直在尝试各种绑定(bind)(wsHttpBinding 和 basicHttpBinding),但我要么收到上述错误(使用 basicHttpBinding),要么收到“访问被拒绝”消息(使用 wsHttpBinding)。这是我在使用 wsHttpBinding 时尝试使用的 web.config 部分
<system.serviceModel>
<services>
<service behaviorConfiguration="AMP.MainBehavior" name="AMP.Main">
<endpoint address="" binding="wsHttpBinding" contract="AMP.IMain">
<identity>
<dns value="myservice.com"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AMP.MainBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
该服务是使用 .NET 4.0 框架创建的。我需要匿名,但我不确定我错过了什么。我是 WCF 新手,所以还没有一切顺利。
谢谢,
最佳答案
该错误表明您发送的 HTTP 请求的内容类型不受支持。提到的内容类型在 SOAP 1.2 中使用,但 BasicHttpBinding 使用具有不同内容类型(text/xml;charset=utf-8)的 SOAP 1.1。因此您的服务和客户端配置之间可能存在一些不匹配。
在第二个示例中,问题是默认的 WsHttpBinidng 配置。默认情况下,WsHttpBinding 通过消息安全和 Windows 身份验证 => 进行保护,因为计算机位于不同的域中,因此 Windows 身份验证无法工作。您必须定义自己的 WsHttpBinding 配置。试试这个(客户端必须使用相同的绑定(bind)配置):
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Unsecured">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service ...>
<endpoint address="..." contract="..."
binding="wsHttpBinding" bindingConfiguration="Unsecured" />
...
</service>
</services>
</system.serviceModel>
关于WCF 服务绑定(bind) wsHttp 与无需身份验证的基本绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3821203/