Changing the Exception Handling for .NET adCenter API Examples

Changing the Exception Handling for .NET adCenter API Examples

  • Comments 14

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.

Expected Exceptions

Avoiding Problems with the Using Statement

Thanks and enjoy!

Comments
  • 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.

  • Hi

    i 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

  • Thankyou ,you are given a main important aspects of .NET!!!!!

  • 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.

  • 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

  • .NET is another good concept for programming and exception handling is also take part in the .NET programming and handle the exceptions.

  • good knowledge giving article thanks

  • ewaveinfotech is working on project basis and Monthly/Hourly Basis.You can Hire Offshore Dedicated Developers or Programmers with

    skills...

  • Very usefull giving article..

    Thanks for the list...

  • 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.

  • Excellent coding. This blog have a very knowledgeable information about .NET.

  • 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!.

  • 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.

  • good post about exception handling clearly i think it is very help  full to all the .net frame work users

    than q for shearing with us.  

Page 1 of 1 (14 items)
Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post