gpt4 book ai didi

c# - WCF 中的 CustomBinding 中的客户端和服务绑定(bind)可能不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:56 24 4
gpt4 key购买 nike

我正在使用以下代码连接 WCF 服务。但我收到以下错误。

内容类型 application/soap+xml;服务不支持 charset=utf-8 http://demo.ca/LicenseWebService.svc .客户端和服务绑定(bind)可能不匹配。

代码:

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 服务的 Web 配置:

<?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 对我的代码的审查来设置 KeepAlive = false;。所以我尝试用 CustomBinding 替换。现在我遇到了 Mismatched 错误。

有人可以建议我使用 CustomBinding 解决上述问题吗?

但是,如果有人可以建议我如何在 BasicHttpBinding 中设置 KeepAlive = false 也很好。

最佳答案

CustomBinding 通常至少需要两个元素:传输(您有)和消息编码(您没有,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();

关于c# - WCF 中的 CustomBinding 中的客户端和服务绑定(bind)可能不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42835197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com