首页 > 解决方案 > 从 .net core 2.1 Web 应用程序使用 WCF 服务

问题描述

我有一个成功使用 WCF 服务的 .net 框架应用程序,当我尝试将设置复制到我的 .net 标准 2.0 类库(由 .net core 2.1 Web 应用程序使用)时,我会收到各种错误,具体取决于如何我设置它。

首先,这是有效的消费者配置:

<netTcpBinding>
<binding name="netTcpBinding_Service" closeTimeout="00:10:00" openTimeout="00:40:00" receiveTimeout="00:32:00" sendTimeout="00:10:00" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647">
  <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
  <reliableSession ordered="true" inactivityTimeout="00:10:00" />
  <security mode="TransportWithMessageCredential">
    <message clientCredentialType="Certificate" algorithmSuite="Default" />
    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
  </security>
</binding>
</netTcpBinding>
<behaviors>
  <endpointBehaviors>
    <behavior name="ClientBehavior">
      <clientCredentials>
        <clientCertificate findValue="Client" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
        <serviceCertificate>
          <defaultCertificate findValue="Server" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
          <authentication certificateValidationMode="PeerOrChainTrust" revocationMode="NoCheck" />
        </serviceCertificate>
      </clientCredentials>
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </endpointBehaviors>
</behaviors>
      <endpoint address="net.Tcp://localhost:8004/Service" behaviorConfiguration="ClientBehavior" binding="netTcpBinding" bindingConfiguration="netTcpBinding_RACService" contract="ServiceInterfaces.IRACService" name="IService">
    <identity>
      <dns value="Server" />
    </identity>

场景 1:尝试尽可能多地复制设置:

    private static NetTcpBinding CreateNetTcpBinding()
    {
        var security = new NetTcpSecurity
        {
            Message = new MessageSecurityOverTcp(),
            Transport = new TcpTransportSecurity()
        };
        security.Message.ClientCredentialType = MessageCredentialType.Certificate;
        security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
        security.Transport.SslProtocols |= SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;

        security.Mode = SecurityMode.TransportWithMessageCredential;

        var binding = new NetTcpBinding
        {
            CloseTimeout = new TimeSpan(0, 10, 0),
            OpenTimeout = new TimeSpan(0, 40, 0),
            ReceiveTimeout = new TimeSpan(0, 32, 0),
            Name = "NetTcpBinding",
            SendTimeout = new TimeSpan(0, 10, 0),
            MaxBufferPoolSize = 524288,
            MaxReceivedMessageSize = 2147483647,
            Security = security,
        };

        return binding;
    }

获取此行的异常:security.Message.ClientCredentialType = MessageCredentialType.Certificate; 异常:

{System.PlatformNotSupportedException:“MessageCredentialType.None”以外的值不支持 MessageSecurityOverTcp.ClientCredentialType。在 System.ServiceModel.MessageSecurityOverTcp.set_ClientCredentialType(MessageCredentialType 值)


场景 2:将 MessageCrendentialType 更改为 None

private static NetTcpBinding CreateNetTcpBinding()
{
    var security = new NetTcpSecurity
    {
        Message = new MessageSecurityOverTcp(),
        Transport = new TcpTransportSecurity()
    };
    security.Message.ClientCredentialType = MessageCredentialType.None;
    security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
    security.Transport.SslProtocols |= SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;

    security.Mode = SecurityMode.TransportWithMessageCredential;

    var binding = new NetTcpBinding
    {
        CloseTimeout = new TimeSpan(0, 10, 0),
        OpenTimeout = new TimeSpan(0, 40, 0),
        ReceiveTimeout = new TimeSpan(0, 32, 0),
        Name = "NetTcpBinding",
        SendTimeout = new TimeSpan(0, 10, 0),
        MaxBufferPoolSize = 524288,
        MaxReceivedMessageSize = 2147483647,
        Security = security,
    };

    return binding;
}

private static EndpointAddress CreateEndpoint()
{
    var identity = new DnsEndpointIdentity("Server");
    var uri = new Uri("net.Tcp://localhost:8004/Service");
    var endpoint = new EndpointAddress(uri, identity);

    return endpoint;
}
public static ServiceProxy CreateClientProxy(string url)
{
    ServiceProxy racServiceProxy;
    racServiceProxyStore.TryGetValue(url, out racServiceProxy);

    try
    {
        if ((racServiceProxy != null) && (racServiceProxy.State == CommunicationState.Faulted ||
                                          racServiceProxy.State == CommunicationState.Closed))
        {
            racServiceProxy.Abort();
            racServiceProxy = null;
        }
        if (racServiceProxy == null)
        {
            CallBackProxy cb = new CallBackProxy();
            InstanceContext ctx = new InstanceContext(cb);

            var binding = CreateNetTcpBinding();
            var endpoint = CreateEndpoint();
            racServiceProxy = new ServiceProxy(ctx, binding, endpoint);
            racServiceProxy.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "Client");

        }
    }
    catch (InvalidOperationException ex)
    {
        throw;
    }
    catch (Exception ex)
    {
    }
    return racServiceProxy;
}

public void InvokeService(IWCFMessage message, OnStatusReceived onStatusReceived, OnResultReceived onResultReceived)
{
    try
    {
        Guid processId = ((WCFMessage)message).ProcessID;

        callBackProxy.AddEvents(onStatusReceived, onResultReceived, processId);

        if (this.State == CommunicationState.Opened || this.State == CommunicationState.Created)
        {
            if (this.State == CommunicationState.Created)
                this.ChannelFactory.Open();

            base.Channel.InvokeRACService(message);
        }
        else
        {
            throw new RacServiceProxyChannelException("Channel Faulted", (int)this.State);
        }
    }

获得例外:this.ChannelFactory.Open();. 如果我跳过这一行,我会得到异常:base.Channel.InvokeRACService(message); 异常:

{System.PlatformNotSupportedException:不支持 NetTcpBinding.CreateMessageSecurity。在 System.ServiceModel.NetTcpBinding.CreateMessageSecurity() 在 System.ServiceModel.NetTcpBinding.CreateBindingElements() 在 System.ServiceModel.Channels.Binding.EnsureInvariants(String contractName) 在 System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose ) 在 System.ServiceModel.ChannelFactory.OnOpening() 在 System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout) 在 System.ServiceModel.Channels.CommunicationObject.OpenAsyncInternal(TimeSpan timeout) 在 System.ServiceModel。 System.ServiceModel 中的 ChannelFactory.EnsureOpened()。


场景 3:更改安全模式:(security.Mode = SecurityMode.None;在 CreateNetTcpBinding 方法中)

调用服务时base.Channel.InvokeService(message); 出现异常异常:

{System.ServiceModel.CommunicationException:套接字连接被中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题造成的。本地套接字超时为“00:09:59.8599605”。---> System.Net.Sockets.SocketException:现有连接被远程主机在 System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted() 在 System.ServiceModel.Channels.SocketConnection.BeginReadCore(Int32 offset, Int32 size , TimeSpan 超时, Action 1 callback, Object state) --- End of inner exception stack trace --- at System.ServiceModel.Channels.SocketConnection.BeginReadCore(Int32 offset, Int32 size, TimeSpan timeout, Action1 回调, Object state) at System.ServiceModel.Channels.SocketConnection.BeginRead(Int32 offset, Int32 size, TimeSpan timeout, Action1 callback, Object state) at System.ServiceModel.Channels.DelegatingConnection.BeginRead(Int32 offset, Int32 size, TimeSpan timeout, Action1 回调,对象状态)在 System.ServiceModel.Channels.ConnectionHelpers.IConnectionExtensions.ReadAsync(IConnection 连接,Int32 偏移量,Int32 大小,TimeSpan 超时)在 System.ServiceModel.Channels.ConnectionHelpers.IConnectionExtensions.ReadAsync(IConnection 连接,字节 []缓冲区,Int32 偏移量,Int32 大小,TimeSpan 超时)在 System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreambleAsync(IConnection 连接,ArraySegment1 preamble, TimeSpan timeout) at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnectionAsync(TimeSpan timeout) at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpenAsync(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.OnOpenAsyncInternal(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.OpenAsyncInternal(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.Runtime.TaskHelpers.CallActionAsync[TArg](Action1 个动作,TArg 参数)在 System.ServiceModel.Channels.CommunicationObject.OpenOtherAsync(ICommunicationObject other, TimeSpan timeout) 在 System.ServiceModel.Channels.ServiceChannel.OnOpenAsync(TimeSpan timeout) 在 System.ServiceModel.Channels.CommunicationObject.OnOpenAsyncInternal(TimeSpan timeout) ) 在 System.ServiceModel.Channels.CommunicationObject.OpenAsyncInternal(TimeSpan timeout) 在 System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel 的 System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout)。 Channels.ServiceChannel.ICallOnce.Call(ServiceChannel 通道,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan 超时,CallOnceManager 级联)在 System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(MethodCall methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(MethodInfo targetMethod, Object[] args) --- 堆栈跟踪从上一个引发异常的位置结束--- 在 System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)}System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(MethodCall methodCall, ProxyOperationRuntime operation) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(MethodInfo targetMethod, Object[] 的 ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时args) --- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)}System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(MethodCall methodCall, ProxyOperationRuntime operation) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(MethodInfo targetMethod, Object[] 的 ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时args) --- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)}Object[] args) --- 在 System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)}Object[] args) --- 在 System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)}

标签: c#wcfnettcpbinding.net-core-2.1

解决方案


推荐阅读