【问题标题】:WsHttpBinding on .Net Core.Net Core 上的 WsHttpBinding
【发布时间】:2020-09-05 11:22:11
【问题描述】:

我正在将 .Net 4.x 项目迁移到 .Net Core 3.1。该项目严重依赖SOAP Web 服务。由于.Net Core不支持基于配置的WCF服务消费,我通过代码调用服务。当前项目使用ws2007HttpBinding 绑定。

我的问题是ws2007HttpBinding.Net Core 中并不正式存在。除了HttpBindingHttpsBindingCustomBinding,我别无选择。我用不同的配置尝试了所有这些,但每次都遇到错误。有时它抱怨https 方案(我正在使用的服务是https),有时它说它已经预料到text/xml 但没有看到它,这可能是双方使用不同的SOAP 协议的结果,并且有时我只是收到超时错误。

在我最后一次尝试中,我安装了 Microsoft.NETCore.Platforms 3.1.2System.ServiceModel.Http 4.7.0 以及更高版本的 4.8.0-preview3.20412.3。后者实现了WsHttpBindingWs2007HttpBinding。但是,两者都不起作用。这次我得到了错误:

System.NotSupportedException:不支持指定的方法。

非常感谢任何解决方案或建议。

这是堆栈跟踪和部分代码:

System.AggregateException: One or more errors occurred. (Specified method is not supported.)
 ---> System.NotSupportedException: Specified method is not supported.
   at System.ServiceModel.MessageSecurityOverHttp.CreateSecurityBindingElement(Boolean isSecureTransportMode, Boolean isReliableSession, MessageSecurityVersion version)
   at System.ServiceModel.WSHttpSecurity.CreateMessageSecurity(Boolean isReliableSessionEnabled, MessageSecurityVersion version)
   at System.ServiceModel.WSHttpBinding.CreateMessageSecurity()
   at System.ServiceModel.WSHttpBindingBase.CreateBindingElements()
   at System.ServiceModel.WSHttpBinding.CreateBindingElements()
   at System.ServiceModel.Channels.Binding.EnsureInvariants(String contractName)
   at System.ServiceModel.Description.ServiceEndpoint.EnsureInvariants()
   at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)
   at System.ServiceModel.ChannelFactory.CreateFactory()
   at System.ServiceModel.ChannelFactory.OnOpening()
   at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.OpenAsyncInternal(TimeSpan timeout)
   at System.Runtime.TaskHelpers.WaitForCompletion(Task task)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open()
   at System.ServiceModel.ChannelFactory.EnsureOpened()
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()
   at client.Inquiry_DataAsync(String NationalCode) in C:\Users\Reference.cs:line 5577
   at class1.<>c__DisplayClass13_0.<Inquiry_Data>b__0() in C:\Users\Code.cs:line 349
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification

代码:

        PermissiveCertificatePolicy.Enact("*");

        var binding = new WSHttpBinding();

        binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

        ServiceClient client = new ServiceClient(
            binding, new EndpointAddress("https://example.com/service.svc"));
        client.ClientCredentials.UserName.UserName = username;
        client.ClientCredentials.UserName.Password = password;
        client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
        {
            CertificateValidationMode = X509CertificateValidationMode.None,
            RevocationMode = X509RevocationMode.NoCheck
        };

        _result = Task.Run(() => client.InquiryDataAsync("ABC")).Result;

【问题讨论】:

    标签: .net wcf .net-core soap wshttpbinding


    【解决方案1】:

    Core不支持Wshttpbinding,wcf只支持BasicHttpBinding、CustomBinding、NetHttpBinding、NetTcpBinding:

    所以目前有两种解决方案:

    修改服务器的绑定,不要使用Wshttpbinding。

    客户端继续使用 .net 框架。

    有关核心中的 WCF 功能的更多信息,您可以参考此链接:

    https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md

    【讨论】:

    • 我无法修改服务器绑定,我们无法控制它。此外,System.ServiceModel.HttpWsHttpBinding 的支持有限。不幸的是,在我的情况下,这种支持还不够。
    • 这种情况下,客户端只能使用.net框架。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2018-01-22
    • 2016-06-21
    相关资源
    最近更新 更多