.NET Core中的wsHttpBinding解决方法 [英] wsHttpBinding workarounds in .NET Core

查看:483
本文介绍了.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框架的完整版本或mono变体。

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解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆