【发布时间】:2020-09-05 11:22:11
【问题描述】:
我正在将 .Net 4.x 项目迁移到 .Net Core 3.1。该项目严重依赖SOAP Web 服务。由于.Net Core不支持基于配置的WCF服务消费,我通过代码调用服务。当前项目使用ws2007HttpBinding 绑定。
我的问题是ws2007HttpBinding 在.Net Core 中并不正式存在。除了HttpBinding、HttpsBinding 和CustomBinding,我别无选择。我用不同的配置尝试了所有这些,但每次都遇到错误。有时它抱怨https 方案(我正在使用的服务是https),有时它说它已经预料到text/xml 但没有看到它,这可能是双方使用不同的SOAP 协议的结果,并且有时我只是收到超时错误。
在我最后一次尝试中,我安装了 Microsoft.NETCore.Platforms 3.1.2 和 System.ServiceModel.Http 4.7.0 以及更高版本的 4.8.0-preview3.20412.3。后者实现了WsHttpBinding 和Ws2007HttpBinding。但是,两者都不起作用。这次我得到了错误:
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