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.

Expected Exceptions

Avoiding Problems with the Using Statement

Thanks and enjoy!

Comments (14)

  1. Cnorred says:

    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.

  2. Cnorred says:

    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

  3. Cnorred says:

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

  4. Cnorred says:

    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.

  5. Cnorred says:

    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

  6. Cnorred says:

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

  7. Cnorred says:

    good knowledge giving article thanks

  8. Cnorred says:

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

    skills…

  9. Cnorred says:

    Very usefull giving article..

    Thanks for the list…

  10. Cnorred says:

    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.

  11. Cnorred says:

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

  12. Cnorred says:

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

  13. Cnorred says:

    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.

  14. Cnorred says:

    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.  

Skip to main content