网络上的WCF.具有安全模式的TCP none给出异常

本文关键字:none TCP 异常 WCF 网络 安全模式 | 更新日期: 2023-09-27 18:12:35

我们使用回调与WCF流式传输数据。WCF托管在IIS中(默认设置,只是添加了net。TCP到协议)

我们正试图禁用WCF上的安全性,所以我将安全模式设置为"无",在客户端和服务器上,但我得到以下CommunicationException:

socket连接被终止。这可能是由错误引起的方法超过接收超时,或正在处理您的消息远程主机,或底层网络资源问题。本地套接字超时时间为'00:00:59.8439844'.

我已经把跟踪服务和TraceViewer给了我以下异常(System.ServiceModel.ProtocolException):

流安全在http://www.w3.org/2005/08/addressing/anonymous,但没有安全保障语境是协商好的。这可能是由远程端点引起的在它的绑定中缺少一个StreamSecurityBindingElement。

客户端Program.cs:

using System;
using System.ServiceModel;
using WcfClient.ServiceReference1;
class Program {
    static void Main() {
        Callback callback = new Callback();
        InstanceContext context = new InstanceContext(callback);
        Service1Client service1Client = new Service1Client(context, 
                                                           "NetTcpBinding_IService1");

        service1Client.GetData(0);
        Console.Read();
        service1Client.Stop();
    }
}
internal class Callback : IService1Callback {
    public void StreamSignalData(int[] result) {
        foreach (int i in result) {
            Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff") + 
                              ": " + i);
        }
    }
}
客户机App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IService1">
          <security mode="None" />
        </binding>
      </netTcpBinding>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_IService1">
          <reliableSession ordered="true" />
          <security mode="None" />
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/wcf-error/Service1.svc" 
        binding="wsDualHttpBinding"
        bindingConfiguration="WSDualHttpBinding_IService1" 
        contract="ServiceReference1.IService1"
        name="WSDualHttpBinding_IService1" />
      <endpoint address="net.tcp://localhost/wcf-error/Service1.svc"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1"
        contract="ServiceReference1.IService1" name="NetTcpBinding_IService1">
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>
WCF Service1.svc.cs

using System;
using System.Net.Security;
using System.ServiceModel;
using System.Threading;
[ServiceContract(SessionMode = SessionMode.Required, 
                 CallbackContract = typeof(IStreamCallback), 
                 ProtectionLevel = ProtectionLevel.None)]
public interface IService1 {
    [OperationContract]
    void GetData(int value);
    [OperationContract]
    void Stop();
}
public interface IStreamCallback {
    [OperationContract(IsOneWay = true)]
    void StreamSignalData(int[] result);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1 {
    private Timer _timer;
    private readonly IStreamCallback _callback =
            OperationContext.Current.GetCallbackChannel<IStreamCallback>();
    public void GetData(int value) {
        _timer = new Timer(StreamData, null, 0, 500);
    }
    public void Stop() {
        _timer.Dispose();
    }
    private void StreamData(object state) {
        int[] randomNumbers = new int[50];
        Random random = new Random();
        for (int i = 0; i < 50; i++) {
            randomNumbers[i] = random.Next(100);
        }
        _callback.StreamSignalData(randomNumbers);
    }
}
WCF web . config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation targetFramework="4.0" debug="true"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpBinding" >
          <security mode="None" />
        </binding>
      </netTcpBinding>
      <wsDualHttpBinding>
        <binding name="wsDualHttpBinding">
          <security mode="None" />
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <protocolMapping>
      <add scheme="http" binding="wsDualHttpBinding"/>
    </protocolMapping>
    <services>
      <service name="Service">
        <endpoint address="" binding="wsDualHttpBinding" 
          bindingConfiguration="wsDualHttpBinding"
          name="EndPointHTTP" contract="WcfService1.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" name="mex"
          contract="IMetadataExchange" />
        <endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding"
          name="EndPointTCP" contract="WcfService1.IService1" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <useRequestHeadersForMetadataAddress />
          <dataContractSerializer />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

你知道这是什么吗?谷歌上的大多数点击都说客户端和服务器上的配置应该是相同的(即安全到没有),但我似乎找不到错误。谷歌的StreamSecurityBindingElement没有很好的解释…

网络上的WCF.具有安全模式的TCP none给出异常

我无法复制您的问题,但我有相同的设置和我注意到的一些事情:

在您的WCF Service1.svc.cs.

    namespace WcfService1
    {
        [ServiceContract(SessionMode = SessionMode.Required,
            CallbackContract = typeof (IStreamCallback),
            ProtectionLevel = ProtectionLevel.None)]
        public interface IService1
        {
        .....
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
        [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
        public 
    class Service1 : IService1
    {

<service name="Service">应为<service name="WcfService1.Service1">

服务的类型

配置中的Services部分:

<services>
    <service name="WcfService1.Service1">
        <endpoint address="" binding="wsDualHttpBinding"
            bindingConfiguration="wsDualHttpBinding"
            name="EndPointHTTP" contract="WcfService1.IService1">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" name="mex"
            contract="IMetadataExchange" />
        <endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding"
            name="EndPointTCP" contract="WcfService1.IService1" />
    </service>
</services>
WCF Service1.svc.cs

using System;
using System.Net.Security;
using System.ServiceModel;
using System.Threading;
namespace WcfService1
{
    [ServiceContract(SessionMode = SessionMode.Required,
        CallbackContract = typeof (IStreamCallback),
        ProtectionLevel = ProtectionLevel.None)]
    public interface IService1
    {
        [OperationContract]
        void GetData(int value);
        [OperationContract]
        void Stop();
    }
    public interface IStreamCallback
    {
        [OperationContract(IsOneWay = true)]
        void StreamSignalData(int[] result);
    }
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Service1 : IService1
    {
        private Timer _timer;
        private readonly IStreamCallback _callback =
            OperationContext.Current.GetCallbackChannel<IStreamCallback>();
        public void GetData(int value)
        {
            _timer = new Timer(StreamData, null, 0, 500);
        }
        public void Stop()
        {
            _timer.Dispose();
        }
        private void StreamData(object state)
        {
            int[] randomNumbers = new int[50];
            Random random = new Random();
            for (int i = 0; i < 50; i++)
            {
                randomNumbers[i] = random.Next(100);
            }
            _callback.StreamSignalData(randomNumbers);
        }
    }
}

确保你处理_callback.StreamSignalData(randomNumbers);当客户端断开连接时。您将得到一个通信异常。