本文介绍了.NET Core 中的 wsHttpBinding 解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 dotnet 核心 WCF wsHttpBinding 解决方法.

I am looking for a dotnet core WCF wsHttpBinding workaround.

我知道 .net 核心 WCF 实现目前不支持 wsHttpBinding(请参阅此处的支持矩阵 https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md)

I am aware that .net core WCF implementation currently does not support wsHttpBinding (see support matrix here https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md)

我正在与一个似乎只支持 wsHttpBinding 的传统第三方服务集成.我们的技术栈是 .net 核心,所以我无法恢复到 .net 框架或单声道变体的完整版本.

I'm integrating with a legacy third party service that appears to only support wsHttpBinding. Our tech stack is .net core, so I cannot revert to the full version of .net framework or mono variant.

问题是是否可以通过自定义绑定使用该服务?我希望有一种解决方法可能不是功能齐全,但至少允许我使用该服务.

The question is whether it's possible to use the service via custom bindings? I am hoping that there is a workaround that maybe isn't fully functional, but at least allows me to consume the service.

var cBinding = new CustomBinding();
        var textBindingElement = new TextMessageEncodingBindingElement()
        {
            MessageVersion = MessageVersion.Soap12WSAddressing10
        };
        cBinding.Elements.Add(textBindingElement);
        var httpBindingElement =
            new HttpsTransportBindingElement
            {
                AllowCookies = true, MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue,
            };
        cBinding.Elements.Add(httpBindingElement);


        var myEndpoint = new EndpointAddress("https://..../Service.svc/wss");
        using (var myChannelFactory = new ChannelFactory<ISearch>(cBinding, myEndpoint))
        {
            myChannelFactory.Credentials.UserName.UserName = "...";
            myChannelFactory.Credentials.UserName.Password = "...";

            ISearch client = null;

            try
            {
                client = myChannelFactory.CreateChannel();

                var result = client.Find(new Search("Criteria")).Result;

                ((ICommunicationObject)client).Close();
                myChannelFactory.Close();
            }
            catch (Exception ex)
            {
                (client as ICommunicationObject)?.Abort();
            }
        }

客户端被创建并调用了服务,但它失败了,因为:

Client gets created and a call is made to the service, but it fails because:

Message = "无法处理消息.这很可能是因为操作 '' 不正确,或者因为消息包含无效或过期的安全上下文令牌,或者因为绑定之间存在不匹配

Message = "The message could not be processed. This is most likely because the action '' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between binding

推荐答案

算了,它们与 Core 并不完全兼容.在某些特定的情况下,可能会有对 WsHttpBinding 的基本调用.你可以参考下面的例子.
服务器.

Forget it, they are not completely compatible with Core. Under some specified case, there may be a basic call to WsHttpBinding. You could refer to the following example.
Server.

   Uri uri = new Uri("http://localhost:11011");
    WSHttpBinding binding = new WSHttpBinding();
    binding.Security.Mode = SecurityMode.None;
    using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
    {
        sh.AddServiceEndpoint(typeof(IService), binding, "");
        ServiceMetadataBehavior smb;
        smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
        if (smb==null)
        {
            smb = new ServiceMetadataBehavior()
            {
            };
            sh.Description.Behaviors.Add(smb);
        }
        Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
        sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
        sh.Opened += delegate
        {
            Console.WriteLine("Service is ready");
        };
        sh.Closed += delegate
        {
            Console.WriteLine("Service is clsoed");
        };
        sh.Open();

客户端(自动生成)

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_IService))
        {
            System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
            System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
            result.Elements.Add(textBindingElement);
            System.ServiceModel.Channels.HttpTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpTransportBindingElement();
            httpBindingElement.AllowCookies = true;
            httpBindingElement.MaxBufferSize = int.MaxValue;
            httpBindingElement.MaxReceivedMessageSize = int.MaxValue;
            result.Elements.Add(httpBindingElement);
            return result;
        }

调用.

ServiceReference1.ServiceClient client2 = new ServiceReference1.ServiceClient();
try
{
    var res = client2.SayHelloAsync();
    Console.WriteLine(res.Result);

}
catch (Exception)
{
    throw;
}

虽然在大多数情况下无法调用由 WsHttpBinding 创建的 WCF 服务.官方也无意继续支持 wshttpbinding 的计划.这里有相关的讨论.https://github.com/dotnet/wcf/issues/31
https://github.com/dotnet/wcf/issues/1370

While in most cases it is impossible to call WCF service created by WsHttpBinding.Officials also have no intention of continuing to support plan for wshttpbinding.Here are related discussions.https://github.com/dotnet/wcf/issues/31
https://github.com/dotnet/wcf/issues/1370

这篇关于.NET Core 中的 wsHttpBinding 解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 06:33