Changing the Exception Handling for .NET adCenter API Examples
Just letting you know that we'll be changing the exception handling in our adCenter examples for C# and Visual Basic to reflect the WCF recommended approach for exception handling. Please consider doing the same with your implementations if you are not already doing so.
Currently our examples use try/catch/finally and release the client (call the Close() method) in the finally block. The problem is that Close() can throw exceptions. If Close() throws an exception, you must call the Abort() method to ensure that all resources are released; otherwise, you could be leaking resources on the server. The recommended practice is to call Close() within the try block, and call Abort() from the caught exceptions.
For example,
static void Main(...)
{
try
{
service = new CampaignManagementServiceClient("BasicHttpBinding_ICampaignManagementService");
AddAdGroupsToCampaign(service, ...);
service.Close();
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);
if (service != null)
{
service.Abort();
}
}
catch (TimeoutException e)
{
Console.WriteLine(e.Message);
if (service != null)
{
service.Abort();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (service != null)
{
service.Abort();
}
}
}
static void AddAdGroupsToCampaign(service, ...)
{
try
{
...
AddAdGroupsResponse response = service.AddAdGroups(request);
}
catch (FaultException<AdApiFaultDetail> fault)
{
...
}
catch (FaultException<ApiFaultDetail> fault)
{
...
}
}
The following links provide the details from the WCF docs:
Accessing Services Using a WCF Client
Handling Exceptions
Handling exceptions in client applications is straightforward. If a channel is opened, used, and closed inside a try block, then the conversation has succeeded, unless an exception is thrown. Typically, if an exception is thrown the conversation is aborted.
Avoiding Problems with the Using Statement
Thanks and enjoy!
Comments
- Anonymous
February 15, 2010
The article is good provides useful information & examples about .NET adCenter API.I like the article very much as it is very informative and in future hope to see more of such articles. - Anonymous
September 20, 2010
Hii am a .net Microsoft certified developer and getting new things i got really very interesting exception handling information from this page and also i will use it with .net framework 4 - Anonymous
October 09, 2010
Thankyou ,you are given a main important aspects of .NET!!!!! - Anonymous
December 15, 2010
Gone through the post which is based on the Exception Handling for .NET adCenter API.The post is explained along with the code which makes us to understand better. Nedd to upload the code so as to check the functioning of the code. then only will be able to comment on the applicability of the code. - Anonymous
December 16, 2010
this was a really quality article. In theory I?d like to write like this also ? taking time and real effort to make a interesting article? but what can I say? I procrastinate a lot and never seem to get it done - Anonymous
February 05, 2011
.NET is another good concept for programming and exception handling is also take part in the .NET programming and handle the exceptions. - Anonymous
February 24, 2011
good knowledge giving article thanks - Anonymous
April 11, 2011
The comment has been removed - Anonymous
April 12, 2011
Very usefull giving article..Thanks for the list... - Anonymous
June 22, 2011
Great post about changing the exception handing for net ad center. Currently our examples use try/catch/finally and release the client (call the Close() method) in the finally block. - Anonymous
June 23, 2011
Excellent coding. This blog have a very knowledgeable information about .NET. - Anonymous
December 26, 2011
Hello I enjoyed your article. I think you have some good ideas and every time i learn something new i don't think it will ever stop always new info , Thanks for all of your hard work!. - Anonymous
January 16, 2012
You made certain good points there. I did a search on the subject matter and found the majority of people will have the same opinion with your blog. - Anonymous
February 02, 2012
good post about exception handling clearly i think it is very help full to all the .net frame work usersthan q for shearing with us.