我使用以下代码连接WCF服务。但我得到了以下错误。
内容类型application / soap + xml;服务http://demo.ca/LicenseWebService.svc不支持charset = utf-8。客户端和服务绑定可能不匹配。
代码:
var address = new EndpointAddress("http://demo.ca/LicenseWebService.svc");
var transport = new HttpTransportBindingElement();
transport.KeepAliveEnabled = false;
var binding = new CustomBinding(transport);
factory = new ChannelFactory<LicenseWebIService>(binding, address);
channel = factory.CreateChannel();
WCF服务的Webconfig:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>
之前我使用的是BasicHttpBinding,它正在运行。
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Name = "BasicHttpBinding_LicenseWebIService";
myBinding.Security.Mode = BasicHttpSecurityMode.None;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endPointAddress = new EndpointAddress("http://demo.ca/LicenseWebService.svc");
factory = new ChannelFactory<LicenseWebIService>(myBinding, endPointAddress);
channel = factory.CreateChannel();
BasicHttpBinding代码工作正常。但我需要根据Microsoft Review of my code设置KeepAlive = false;
。所以我试图用CustomBinding
替换。现在我的误差超过了错误。
有人建议我使用CustomBinding解决上述问题吗?
但是,如果有人能告诉我如何在BasicHtttpBinding中设置KeepAlive = false
,那也没关系。
答案 0 :(得分:1)
CustomBinding通常至少需要两个元素:transport(你拥有它)和消息编码(你没有它,而WCF试图从传输中猜测它)。 尝试明确添加消息编码部分(并且您可以指定编码):
var address = new EndpointAddress("http://demo.ca/LicenseWebService.svc");
var transport = new HttpTransportBindingElement();
transport.KeepAliveEnabled = false;
var messageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8);
var binding = new CustomBinding(new [] { messageEncoding, transport });
factory = new ChannelFactory<LicenseWebIService>(binding, address);
channel = factory.CreateChannel();