首页 > 解决方案 > 从 .net 核心调用 Soap 1.2 服务

问题描述

我正在尝试使用 .net core 3.1 中的 SOAP 1.2 WCF 服务。
我有一个在 .net 框架 4 中工作的客户端。它使用具有安全模式 TransportWithMessageCredential 的 wsHttpBinding。

首先,我尝试在我的 .net 核心客户端中使用 wsHttpBinding,但出现“不支持平台”异常。所以我切换到 BasicHttpsBinding 但是当我调用一个函数时导致另一个异常:

ProtocolException:内容类型文本/xml;服务 https://domain/Service.svc 不支持 charset=utf-8。客户端和服务绑定可能不匹配。

根据我的发现,BasicHttpsBinding 适用于 Soap 1.1,而 wsHttpBinding 适用于 Soap 1.2。
所以我尝试根据https://stackoverflow.com/a/53336689将 Soap-version 设置为 1.2但这给了我另一个例外:

SocketException:现有连接被远程主机强行关闭。

这是 .net 4 的工作配置(为了便于阅读,有些缩写):

<bindings>
        <wsHttpBinding>
            <binding name="ServiceEndpoint" 
                messageEncoding="Text" textEncoding="utf-8">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>      

这是我的 .net 核心代码(不工作):

var binding = new BasicHttpsBinding();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Mode = BasicHttpsSecurityMode.TransportWithMessageCredential;
// Code from https://stackoverflow.com/a/53336689
var customTransportSecurityBinding = new CustomBinding(binding);
var textBindingElement = new TextMessageEncodingBindingElement
{
    MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
};
// Replace text element to have Soap12 message version
customTransportSecurityBinding.Elements[1] = textBindingElement;

var serviceClient = new Svc.ServiceClient(customTransportSecurityBinding, new EndpointAddress("https://domain/Service.svc"));
serviceClient.ClientCredentials.UserName.UserName = "usr";
serviceClient.ClientCredentials.UserName.Password = "pwd";
var units = serviceClient.GetUnitsAsync().Result; // Exception here

标签: wcf.net-coresoapsoap-clientwcf-binding

解决方案


服务器和客户端必须使用相同的绑定进行通信。你的服务器使用wsHttpBinding,而客户端使用BasicHttpsBinding,所以不能正常通信。

在此处输入图像描述

你说的对。Core 目前不支持 wsHttpBinding,所以有两种解决方案:

1:将服务器的wsHttpBinding改为BasicHttpBinding,核心不支持Message安全模式。对于 Core 中的 WCF 功能,您可以参考以下链接:

https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md

2:客户端使用.net框架而不是core。

我建议您使用第二种解决方案。毕竟core对wcf的支持不好。

如果问题仍然存在,请随时告诉我。


推荐阅读