无法在 .net core 中使用 WCF WSHttpBinding 进行消费

9
我正在尝试将项目从.NET迁移到.NET Core。在.NET中,我最初使用了WCF WSHttpBinding服务,但无法在.NET Core中使用相同的服务。我尝试在客户端使用BasicHttpBinding连接到WsHttpBinding,但是它会抛出一个错误,说明绑定应在客户端和服务器端匹配。请建议如何在不修改客户端WSHttpBindings的情况下,在.NET Core上实现WSHttpBinding。

类似于 https://dev59.com/-5jga4cB1Zd3GeqPNac4 的东西? - Chetan
6个回答

7

我能够使用自定义绑定使其正常工作。还有可以配置的https、文本和二进制绑定元素。

var binding = new CustomBinding
{
    Elements = { new HttpTransportBindingElement //or HttpsTransportBindingElement for https:
    {
        MaxBufferSize = int.MaxValue,
        MaxReceivedMessageSize = int.MaxValue
    }}
};

我确认这个对我有效。我正在将WCF客户端从4.7框架移植到Core 2.2。 - Alexey Kulikov
哎呀,这对我没有起作用。我得到了这个错误信息:{"该终结点不支持 http://tempuri.org/XXXService/GetXXXList 操作。该终结点仅处理 WS-ReliableMessaging February 2005 版本的消息。"} - AussieJoe
@AussieJoe,我有点晚来参加派对,但是看起来回应似乎是有效的,并且服务告诉你正在尝试调用一个不存在的端点。 - Michel

4
WCF WSHttpBinding 目前还不支持在 .net core 2.1 中使用。 以下是 .Net core 支持的绑定列表:
- BasicHttpBinding - CustomBinding - NetHttpBinding - NetTcpBinding
欲了解更多支持特性,请点击此处

客户端和服务器端的绑定应该相同吗?因为如果我尝试使用BasicHttpBinding或NetHttpBinding连接到在.NET Core中使用WSHttpBinding的客户端,它会抛出异常“客户端和服务器绑定不匹配”。 - Niraj Bhattad

4

我在完整框架上使用了WSHttpBinding。迁移到.net core 2.2后,我遇到了相同的问题。

后来我发现BasicHttpsBinding(它是https,而不是http)完全符合我的需求。我只需用BasicHttpsBinding替换WSHttpBinding,一切都正常工作(在Windows和Linux上都可以)。


1
BasicHttpsBinding 对我不起作用,但是 CustomBinding(由 Nicholas J. Markkula 建议)可以使用。 - Michael Freidgeim
愿上帝保佑你。每一个在我苦苦挣扎期间在互联网上找到的人,无论是评论还是回答,都帮助我将. Net Framework 4.0 wcf服务消耗在net core 2.1 web API中。 - Chestera

1
这是一个更完整的答案,可以帮助那些试图使用.NET Core (.NET Core 2.2)调用SOAP服务端点并且难以使其工作的人。
对于这个答案,我假设您已经添加了一个连接服务。如果没有,请参考这个Microsoft文章
以下示例使用BasicHttpBindingNtlm凭据类型(DOMAIN\Username):
public async Task<string> ImportSalesOrder(string jsonString)
{
    var binding = new BasicHttpsBinding();
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

    var endpoint = new EndpointAddress(_appConfig["endpoint"]);

    var client = new eCommIntegrationMgt_PortClient(binding, endpoint);

    client.ClientCredentials.UserName.UserName = _appConfig["usernam"];
    client.ClientCredentials.UserName.Password = _appConfig["password"];

    try
    {
        var result = await client.ImportSalesOrderAsync(jsonString);

        return result.return_value;
    }
    catch (Exception)
    {
        throw;
    }
}

_appConfig 是一个全局变量,可以通过 DI(依赖注入)获得。如果您不使用 DI,可以将其替换为硬编码的值。这里的 catch 是多余的,但您可以添加自定义的错误处理/日志记录。

eCommIntegrationMgt_PortClient 是客户端,即包含所有终端的服务对象。


1
这是我使用的代码片段。
        var binding = GetCustomBinding();

        private Binding GetCustomBinding()
        {
            CustomBinding result = new CustomBinding();
            TextMessageEncodingBindingElement textBindingElement = new TextMessageEncodingBindingElement();
            result.Elements.Add(textBindingElement);
            HttpsTransportBindingElement httpsBindingElement = new HttpsTransportBindingElement
            {
                AllowCookies = true,
                MaxBufferSize = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue,
                AuthenticationScheme = System.Net.AuthenticationSchemes.Negotiate
            };
            result.Elements.Add(httpsBindingElement);
            return result;
        }

0

.Net Core目前还不支持WsHttpBinding。 您可以尝试使用以下方法。 https://www.nuget.org/packages/WsSecurityCore/

您可以将响应作为XmlNodeList或Xml完整字符串获取,并使用XmlSerializer将其转换为响应类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接