Resolve the Client Application Hanging Problem in WCF

In this article I will explain how to eliminate the "client application hanging" problem in WCF.

To explain it I will create:

  • Create a website and add a WCF service to it.
  • Publish the website on the server
  • Create the client application and add the reference of the WCF service into it.

To understand it in depth you need to create a website that contains a WCF service.

Step 1:

  1. Create an ASP.NET website named "WCF_Website".

    Create a website

  2. Add a WCF service named "MyService" into it.

    WCF service name

  3. Add an "operation contract" into the interface of the WCF service.

    [OperationContract]
    int Add(int a , int b);

    operation contract
     
  4. Implement the entire interface into the "MyService" class and return the addition of both input values.

    public int Add(int a, int b)
    {
        
    return a + b;
    }

    addition

Step 2:

  1. Publish the website in a local folder as in the following:

    Publish the website

  2. Then publish it on the main server using a FTP client or you can publish it directly onto the server.

    FTP client

  3. Check the "Myservice .svc" via the browser as in the following:

    browser

Step 3:

  1. Add a new web application project named "ClientWebApplication".

    New project of web application

  2. Add the Service Reference into it.

    Add the Service Reference

  3. Type the URL of the WCF service and click on the Go button. After selecting your service, click on the "OK" button.

    URL of the WCF service

  4. Access the method of the WCF service with 2 integer values by the client object of the WCF service.

    ServiceReference1.MyServiceClient objclient = new ServiceReference1.MyServiceClient();

    Response.Write(objclient.Add(10, 50));

    Access the method of WCF service

Problem

Run the Page. After a specific time instance your page will hang when you refresh it again and again.

Run the Page
Solution

This problem occurs just due to exceeding the default instance limitation that is created after every new request of the page when page is refreshed.
So you need to close every instance of the client. To do this, use the following code in the client application.

objclient.Close();

client application

As you can see, when you run the WCF service the default documentation of the service says "Always close the client."
 
Run the WCF service