WCF服务器在连接中止时自动连接到客户端

时间:2010-02-20 10:10:43

标签: c# wcf

我在我的WindowsApplication中使用WCF服务...当我运行服务器和客户端的应用程序时,服务器在几分钟内断开连接....如何在连接中止时自动重新连接客户端.. ..

这是我的客户代码:

    public void connecttoserver()
    {
        D:
        try
        {

        EndpointAddress ea = new EndpointAddress(@"net.tcp://10.0.3.33:2222/ClsPCMain");
        EndpointAddress ea = new EndpointAddress(StrAddress);
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);

        binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.PortSharingEnabled = true;
        binding.ReceiveTimeout = TimeSpan.MaxValue;
        binding.SendTimeout = TimeSpan.MaxValue;
        binding.OpenTimeout = TimeSpan.MaxValue;
        binding.CloseTimeout = TimeSpan.MaxValue;
        binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.MaxConnections = Int16.MaxValue;
        binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
        binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
        binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
        binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;
        binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
        binding.Security.Mode = SecurityMode.None;
        ChannelFactory<InterfaceClass.IService> Client = new ChannelFactory<InterfaceClass.IService>(binding,ea);

            InterfaceClass.IService serviceobj = Client.CreateChannel(ea);


            clsStatus.connectstatus = false;






            ClsPC objclsPc = serviceobj.PCInfoMethod(Environment.UserName, Environment.UserDomainName, Dns.GetHostName(), Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString());

            if (objclsPc.imageid == 1)
            {


                clsStatus.FullSizeImage = true;
                clsStatus.ThumbnailImage = false;
            }
            else
            {
                clsStatus.ThumbnailImage = true;
                clsStatus.FullSizeImage = false;

            }
        Client.Close();
        Client=null;
            //serviceobj = null;


        }

        catch (Exception ex)
        {
            logobj.Write(ex);

} }

这是我的服务器代码:

    public clsHostService()
    {

        string StrAddress = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "url2.txt");
        ServiceHost host = new ServiceHost(typeof(clsService));
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);
        ServiceEndpoint endpointinfo = host.AddServiceEndpoint(typeof(IService), binding, StrAddress);

        endpointinfo.Binding.CloseTimeout = TimeSpan.MaxValue;
        endpointinfo.Binding.OpenTimeout = TimeSpan.MaxValue;
        endpointinfo.Binding.ReceiveTimeout = TimeSpan.MaxValue;
        endpointinfo.Binding.SendTimeout = TimeSpan.MaxValue;



        XmlDictionaryReaderQuotas BindingQuota = binding.ReaderQuotas;
        BindingQuota.MaxArrayLength = Int32.MaxValue;
        BindingQuota.MaxBytesPerRead = Int32.MaxValue;
        BindingQuota.MaxDepth = Int32.MaxValue;

        binding.MaxConnections = Int16.MaxValue;
        binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.MaxBufferSize = Int32.MaxValue;
        binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.CloseTimeout = TimeSpan.MaxValue;
        binding.OpenTimeout = TimeSpan.MaxValue;
        binding.ReceiveTimeout = TimeSpan.MaxValue;
        binding.SendTimeout = TimeSpan.MaxValue;

        ServiceThrottlingBehavior throttlingBehavior =new ServiceThrottlingBehavior();
        throttlingBehavior.MaxConcurrentCalls = Int32.MaxValue;
        throttlingBehavior.MaxConcurrentInstances = Int32.MaxValue;
        throttlingBehavior.MaxConcurrentSessions = Int32.MaxValue;
        host.Description.Behaviors.Add(throttlingBehavior);




        host.Open();
        Console.WriteLine("Server Started");

        Console.ReadLine();

    }

现在如何自动连接到客户端当服务器切断连接时? 有人告诉我这个问题的解决方案...... 在此先感谢.....

2 个答案:

答案 0 :(得分:1)

我不完全理解您的问题,我担心 - 您的Winforms应用程序正在托管该服务,还是客户端调用WCF服务?

WCF通常不使用客户端和服务器之间持续连接的概念。

客户端构建一个客户端代理,在该代理上调用服务器公开的方法。基本上,每个呼叫都独立于所有其他呼叫 - 在呼叫期间,客户端和服务器之间仅存在连接。连接不是总是向上 - 只有在实际发生呼叫时它才会到位。

所以我并不完全明白你想要“重新连接”的内容 - 首先并没有永远在线的连接。

如果在服务器端发生异常并且未正确捕获和处理异常,那么客户端代理可能会变为无效。在WCF术语中,客户端和服务器之间的“信道”已经“出现故障”,例如,已变得无法使用。如果您在故障状态下使用客户端代理再次调用服务器,则会收到客户端异常。

您可以在使用此代码进行呼叫之前检查客户端代理上的故障通道状态:

if(client.State == CommunicationState.Faulted)
{
    client = new YourServiceClient();            
}

如果频道确实出现故障,那么您需要重新创建代理,然后您应该重新开始营业。

答案 1 :(得分:1)

我使用这样的东西:

//Somewhere in the main
ConfigureWcf();
ConnectToServer();
//...

void ConnectToServer()
{
  myService = new ServiceReference.ServiceClient(context);
  myService.Open();
  myService.InnerChannel.UnknownMessageReceived += InnerChannel_UnknownMessageReceived;
  myService.InnerChannel.Closed += InnerChannel_Closed;
}

void StartConnecting()
{
   //use 5 attempts to connect to server
   ConnectToServer();
}

void InnerChannel_Closing(object sender, EventArgs e)
{
  //Connection to server closed!
  //Write to log
  StartConnecting(); 
}