.NET Core中的wsHttpBinding解决方法

时间:2019-02-11 16:34:35

标签: c# .net wcf .net-core wshttpbinding

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

我知道.net核心WCF实现当前不支持wsHttpBinding(请参见https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md的支持列表)

我正在与似乎仅支持wsHttpBinding的旧式第三方服务集成。我们的技术堆栈是.net核心,因此我无法还原到.net框架的完整版本或mono变体。

问题是是否可以通过自定义绑定使用该服务?我希望有一个可能无法完全正常运行的解决方法,但至少可以让我使用该服务。

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 =“无法处理该消息。这很可能是因为操作”不正确,或者该消息包含无效或过期的安全上下文令牌,或者是绑定之间不匹配

1 个答案:

答案 0 :(得分:2)

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.mongofunction.base" />
    <mvc:annotation-driven />

    <import resource="SpringConfig.xml"/>

</beans>

客户端(自动生成)

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

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