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:
- Create an ASP.NET website named "WCF_Website".
- Add a WCF service named "MyService" into it.
- Add an "operation contract" into the interface of the WCF service.
[OperationContract]
int Add(int a , int b);
- 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;
}
Step 2:
- Publish the website in a local folder as in the following:
- Then publish it on the main server using a FTP client or you can publish it directly onto the server.
- Check the "Myservice .svc" via the browser as in the following:
Step 3:
- Add a new web application project named "ClientWebApplication".
- Add the Service Reference into it.
- Type the URL of the WCF service and click on the Go button. After selecting your service, click on the "OK" button.
- 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));
Problem
Run the Page. After a specific time instance your page will hang when you refresh it again and again.
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();
As you can see, when you run the WCF service the default documentation of the service says "Always close the client."