Call WCF service – error handling & disposing

The final example how to correctly call WCF service and end communication if error occurred:

ServiceClient proxy = null;
bool success = false;
try
{
   proxy = new ServiceClient();
   proxy.ServiceMethod();
   proxy.Close();
   success = true;
}
finally
{
   // if failed and not null
   if (!success && proxy != null)
   {
      proxy.Abort();
   }
}

Remember always call Close to end communication. In other case it will can cause to crash server host (IIS for example) after reaching threads or memory limit.

Avoid to call non-IIS WCF service by using the using statement syntax. It will hide the actual communication exceptions details if server side error occurred.

Advertisements
Posted in WCF. Tags: . Leave a Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: