Export (0) Print
Expand All

Getting Started Using C# with Bing Ads Services

Visual Studio integrates web services support into the IDE. To use web services, you must have .NET Framework 3.0 or later installed.

The examples within C# Examples for Bing Ads have been developed and run in a similar environment as described below. Your custom configuration may vary.

System_CLiX_note Note

You may download some C# samples within a Visual Studio solution at Code Gallery.

Setting Up the Development Environment

The examples are developed and run with Visual Studio 2012. For information about installing Visual Studio, see Visual Studio Downloads.

Generating the Proxy Classes

To generate proxy classes for a Bing Ads web service in Visual Studio 2012, perform the following steps.

  1. Open Visual Studio 2012.

  2. Create a new Visual Studio C# -> Console Application project.

  3. Within the new Project right-click on the References node and select the Add Service Reference command.

  4. We recommend that you use message contracts when you create your client proxy, although it isn't required. To create a client proxy that uses message contracts, perform the following steps.

    1. In the Add Service Reference window, click Advanced.

    2. In the Service Reference Settings window, select the Always generate message contracts check box.

    3. Click the OK button.

    For more information about message contracts, see Message Contracts.

  5. In the Address field, type the URL of the web service that you want to reference. For a list of addresses, see Bing Ads Web Service Addresses. Typically, you use the sandbox address when developing your application. For information about referencing the production address after developing and testing your application, see Deploy Your Application.

  6. Click Go. Visual Studio then contacts the given URL and downloads the necessary information.

  7. In the Namespace field, type a descriptive name for the namespace. This can be any name that you want, as long as it is unique within your project; for example, "BingAds.Reporting".

  8. Click OK. Visual Studio then creates the necessary files and adds them to your project.

  9. Repeat these steps for any other service references that you may need.

To use the proxy classes that you generated, add a using statement that identifies the namespace that contains the proxy classes. The using statement is of the following form.

using project_name.namespace_name;

Replace project_name with the name of your project and replace namespace_name with the name of your web service namespace. For example, the following is the using statement you would use if your Visual Studio project name were Test and the name of your proxy namespace were BingAds.Reporting.

using Sample.BingAds.Reporting;

You must also include a using statement for System.ServiceModel.

You can now reference the proxy classes and operations in your application.

Message Contracts

Using message contracts creates client proxy code that uses the Request and Response classes for the service operations. For example, the AddCampaigns service operation uses an AddCampaignsRequest object, and then returns an AddCampaignsResponse object. The Request class contains all the header and parameter information that the client passes to the service operation. The Response class contains all the information that the operation returns to the client.

The following shows the signature of the AddCampaigns operation using message contracts.

public AddCampaignsResponse AddCampaigns 
  AddCampaignsRequest request

If you don’t use message contracts, you must pass all the header and parameter information as parameters to the service operation as shown in the following example.

public ApiCallTrackingData AddCampaigns 
  ApplicationToken applicationToken,
  System.Nullable<int> customerAccountId,
  DeveloperToken developerToken,
  UserCredentials userCredentials,
  int accountId,
  Campaign[] campaigns,
  out int[] campaignIds

For more information about message contracts, see Using Message Contracts.


Depending on how many entities you manage, the service may return a large number of objects. For example, an account can define a large number of ad extensions. As a best practice you should update your client’s app.config configuration file to increase the values of the following attributes.

  • maxArrayLength – The readerQuotas node contains the maxArrayLength attribute. Increase the size to 124000 to support current limits and future growth.

  • maxReceivedMessageSize – The binding node contains the maxReceivedMessageSize attribute. Increase the size to 12400000 to support current limits and future growth.

    System_CLiX_note Note

    maxReceivedMessageSize and maxBufferSize must have the same value, so if you update maxReceivedMessageSize, you must also update maxBufferSize equally.

  • maxItemsInObjectGraph – The dataContractSerializer node in the behaviors section contains the maxItemsInObjectGraph attribute. Increase the size to 124000 to support current limits and future growth. Note that the app.config file does not include the behaviors section by default.

The following app.config example shows the maxArrayLength and maxItemsInObjectGraph attributes set to the suggested values.

<?xml version="1.0" encoding="utf-8" ?>
                <behavior name="CampaignManagementClientBehavior">
                    <dataContractSerializer maxItemsInObjectGraph="124000" />

                <binding name="BasicHttpBinding_ICampaignManagementService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="12400000" maxBufferPoolSize="524288" maxReceivedMessageSize="12400000"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="124000"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
            <endpoint address="https://api.bingads.microsoft.com/Api/Advertiser/CampaignManagement/v9/CampaignManagementService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICampaignManagementService"
                behaviorConfiguration="CampaignManagementClientBehavior" />

Deploy Your Application

When you have written and tested your application in the sandbox environment and it is ready to deploy to the production environment, you must replace the sandbox WSDLs with the production WSDLs. To change the WSDL for a service reference, perform the following steps.

  1. In the Solution Explorer, select the service reference.

  2. On the Project menu, click Configure Service Reference.

  3. In the Address box, replace the existing URL with the new URL.

  4. Click OK.

  5. After the service reference is updated, rebuild your application.

Community Additions

© 2014 Microsoft