简体   繁体   中英

WCF Server Connect to the client Automatically When Connection was aborted

I am using WCF service in my WindowsApplication... when i was running the application both server and client, The server disconnected the connetion in few minutes.... How shall i reconnect the client automatically When Connection was aborted....

This is my Client code:

    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);

} }

This Is My Server Code:

    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();

    }

Now How Shall i Connect to the client Automatically When server cuts the Connection? Anyone Tell me The Solution of this Problem... Thanks in Advance.....

I use something like this:

//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(); 
}

I don't completely understand your question, I'm afraid - your Winforms app is hosting the service, or is it the client calling a WCF service??

WCF doesn't typically use the concept of having a constant connection between client and server.

The client builds a client-side proxy on which is calls methods that the server exposes. Basically, each call is independant of all the others - a connection only exists between the client and the server for the duration of the call. The connection isn't always up - it's only in place when a call is actually happening.

So I don't completely understand what you want to "reconnect" - there is not always-on connection in the first place.

What can happen is that if an exception happens on the server side and isn't caught and handled properly, then the client-side proxy can become invalid. In WCF terms, the "channel" between the client and the server has been "faulted" , eg has become unusable. If you were to call the server again with a client-side proxy in a faulted state, you'd receive a client-side exception.

You can check for a faulted channel state on the client-side proxy before making a call with this code:

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

if the channel is indeed faulted, then you need to re-create the proxy again and you should be back in business.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM