【问题标题】:Hold TcpClient Connection with WCF保持与 WCF 的 TcpClient 连接
【发布时间】:2010-09-29 01:57:41
【问题描述】:

你好 我需要一个到后台服务器的 Tcp 连接,应用程序将使用此连接发送数据。我四处搜索,发现 WCF 单例适合此任务 这是我在下面使用的代码 sn-p 我的问题是好方法和任何问题都可能与此有关?

 string hostAddress = string.Empty;
           try
            {
                srvHost = new ServiceHost(typeof(ControllerClass));
                NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None);
                netTcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
                netTcpBinding.Security.Transport.ProtectionLevel =      System.Net.Security.ProtectionLevel.None;
                netTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;

                srvHost.AddServiceEndpoint(typeof(IControllerContract), netTcpBinding, hostAddress);
                srvHost.Credentials.WindowsAuthentication.AllowAnonymousLogons = true;

                ServiceThrottlingBehavior serviceThrottlingBehavior = new ServiceThrottlingBehavior();
                serviceThrottlingBehavior.MaxConcurrentCalls = 1000;
                serviceThrottlingBehavior.MaxConcurrentInstances = 1000;
                serviceThrottlingBehavior.MaxConcurrentSessions = 1000;
                srvHost.Description.Behaviors.Add(serviceThrottlingBehavior);
                srvHost.Open();
            }
            catch (System.TimeoutException timeoutEx)
            {

                Thread.Sleep(1000);
                ReOpenHostConnection();//initialize again Controller Class
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("cannot start Service Ex:{0}", ex.ToString()), TraceEventType.Error.ToString());
            }

//Controller Class Initialize Code Snippet

TcpClient iTcpClient = new TcpClient();
                iTcpClient.Connect(serverIP, serverPort);
                networkStream = iTcpClient.GetStream();
                aSychDataByte = new byte[iTcpClient.ReceiveBufferSize];
                networkStream.BeginRead(aSychDataByte, 0, incommTcpClient.ReceiveBufferSize, ReadAsych, null);

【问题讨论】:

    标签: wcf hosting tcpclient


    【解决方案1】:

    为什么将 TcpClient 与 WCF 结合使用?如果您需要基于低级别 Tcp 类的低级别 Tcp 通信构建客户端和服务器。如果您需要服务并且不关心消息格式,请使用 WCF。

    针对您的问题。你不需要单例。您只需要打开连接。为此,您需要在客户端上创建 WCF 代理实例(开放通道)并调用服务。它将创建与服务实例的连接,直到您关闭客户端代理、服务主机停止工作或超时(默认为 10 分钟客户端不活动)。

    【讨论】:

    • 我连接到第 3 方 tcp 服务器。首先连接启动我发送一个启动命令,这应该一直处于活动状态,客户端应用程序将通过此连接在单例 wcf 服务上发送数据
    • 您确定您的代码使用了您手动启动的连接吗?
    • 我在使用 tcpclient 对象的 wcf 单例程序上打开并保持与 tcpclient 对象的第 3 方服务器的 tcp 连接..
    猜你喜欢
    • 2015-04-18
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多