.NET Core 中的 wsHttpBinding 解决方法

wsHttpBinding workarounds in .NET Core

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

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

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

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

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();
            }
        }

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

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

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

   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