locked
Problems with an Closed Connection RRS feed

  • Question

  • I have a WCF service that I consume by my WPF client.
    The connection is mannaged by a singleton class.
     
    Now I know that after 10 minutes of inactivity the connection is closed on server side. But I can not find a method on my wcfclient to check the state of the connection.
    I use the following code.
    private ServiceController()
    
    
    
            {
    
    
    
                this.m_ProPlanClient = new ServiceProPlanClient();
    
    
    
                
    
    
    
                this.AttachEventHandlers();
    
    
    
                this.InitialiseCollections();
    
    
    
            }
    
    
    
    
    
    
    
    
    
    
    
    private void CheckAndReopenConnection()
    
    
    
            {
    
    
    
                
    
    
    
                if (this.ProPlanClient.State == CommunicationState.Closed)
    
    
    
                {
    
    
    
                    this.ProPlanClient.Open();
    
    
    
                }
    
    
    
            }
    
    
    
    
    
    
    
    
    
    
    
     public void GetAllCustomersAsync()
    
    
    
            {
    
    
    
                this.CheckAndReopenConnection();
    
    
    
    
    
    
    
                this.ProPlanClient.GetAllCustomersAsync();
    
    
    
            }
    
    
    
    
    The connectionstate is always Open, it does not matter wheter I wait 5 min or 25 min.
    Are there any ideas on how the check if the connection is really still open.
    because I do not want to close my connection after every singel call.
    Thursday, May 14, 2009 12:37 PM

Answers

  • Is the error you refer to a new error?

    You need to recreate the proxy.

    private void CheckAndReopenConnection()
            {
                try
                {
                    this.ProPlanClient.Ping();
                }
                catch
                {
                    ((IChannel)this.ProPlanClient).Abort();
                    this.ProPlanClient = null;
                }

                if (this.ProPlanClient == null)
                {
                    this.ProPlanClient = // recreate channel
                    this.ProPlanClient.Open();
                }
            }

    Tuesday, May 19, 2009 12:13 AM

All replies

  • WCF is not psychic. The only way it will know if the connection is dead is if it tries to talk to the service. Therefore, if you want to know if the service is dead have a "ping" operation on the contract and call that to see if you get a failure
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    • Proposed as answer by Marco Zhou Friday, May 15, 2009 5:09 AM
    • Marked as answer by Marco Zhou Friday, May 15, 2009 5:09 AM
    • Unmarked as answer by Kevin Hendriks Friday, May 15, 2009 6:38 AM
    Thursday, May 14, 2009 1:23 PM
  • I think I am still doing something wrong
    I made the following
    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
            void Ping();
    
    
            public void Ping()
            {
            }
    
    

    and client code
    private void CheckAndReopenConnection()
            {
                try
                {
                    this.ProPlanClient.Ping();
                }
                catch
                {
                    if (this.ProPlanClient.State != CommunicationState.Opened && this.ProPlanClient.State != CommunicationState.Opening)
                    {
                        this.ProPlanClient.Open();
                    }
                }
            }
    I can not call Open because I still get an error.
    Am I missing something or going the wrong way.
    Thursday, May 14, 2009 2:35 PM
  • Is the error you refer to a new error?

    You need to recreate the proxy.

    private void CheckAndReopenConnection()
            {
                try
                {
                    this.ProPlanClient.Ping();
                }
                catch
                {
                    ((IChannel)this.ProPlanClient).Abort();
                    this.ProPlanClient = null;
                }

                if (this.ProPlanClient == null)
                {
                    this.ProPlanClient = // recreate channel
                    this.ProPlanClient.Open();
                }
            }

    Tuesday, May 19, 2009 12:13 AM
  • This solved my problem. I just needed a few lines more.
    Tuesday, May 19, 2009 6:48 AM