googleads / google-ads-dotnet

This project hosts the .NET client library for the Google Ads API.

Home Page:https://developers.google.com/google-ads/api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MutateCampaignBudgets just hangs

RentCafeDev opened this issue · comments

After upgrading from V11 to V12. Calling CampaignBudgetServiceClient.MutateCampaignBudgets creates the budget, then just hangs and never returns the Resource Name of the budget just created.

Code snippet:
string BudgetName = "Shared Budget ";
long Amount = 0;
Boolean bIsShared = true;

        CampaignBudgetServiceClient budgetService = Client.GetService(Services.V12.CampaignBudgetService);

        if (data.IndividualBudget == 1) { BudgetName = "Network Display Budget "; bIsShared = false; }
        if (data.DisplayNetwork == 1) { Amount = Convert.ToInt64(Math.Floor(data.DailyBudget * 100)) * 10000; } else { Amount = Convert.ToInt64(Math.Floor(data.MonthlyBudget * 100 / 30.4)) * 10000; }

        CampaignBudget budget = new CampaignBudget()
        {
            Name = BudgetName + GetRandomString(),
            DeliveryMethod = BudgetDeliveryMethod.Standard,
            AmountMicros = Amount,
            ExplicitlyShared = bIsShared
        };

        CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()
        {
            Create = budget
        };

        MutateCampaignBudgetsResponse response = budgetService.MutateCampaignBudgets(CustomerId.ToString(), new CampaignBudgetOperation[] { budgetOperation }); -> this creates the budget, but just hangs and never gets to the line below.
        return response.Results[0].ResourceName;

Expected behavior:
The method should return the Resource Name of the budget just created.

Client library version and API version:
Client library version: Google.Ads.GoogleAds 14.21
Google Ads API version: V12
.NET version: 4.8
Operating system: Windows 10

Request/Response Logs:
No logs are created because the process just hangs on line: MutateCampaignBudgetsResponse response = budgetService.MutateCampaignBudgets(CustomerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });

Anything else we should know about your project / environment

Have you had a chance to figure out a solution to this issue?

Same here, when calling listaccessiblecustomer with .net framework. Did you find a solution for this?

Try using the async version. This issue has come up before -- it has nothing to do with .NET Framework, but certain subparts of .NET Framework that doesn't like waiting on threads (e.g. WinForms).

I changed my code to use async, but the issue is still happening. Below is my code.

    private async System.Threading.Tasks.Task<string> CreateBudget(AdData data, Int64 CustomerId)
    {
        string BudgetName = "Shared Budget ";
        long Amount = 0;
        Boolean bIsShared = true;

        CampaignBudgetServiceClient budgetService = Client.GetService(Services.V12.CampaignBudgetService);

        if (data.IndividualBudget == 1) { BudgetName = "Network Display Budget "; bIsShared = false; }
        if (data.DisplayNetwork == 1) { Amount = Convert.ToInt64(Math.Floor(data.DailyBudget * 100)) * 10000; } else { Amount = Convert.ToInt64(Math.Floor(data.MonthlyBudget * 100 / 30.4)) * 10000; }

        CampaignBudget budget = new CampaignBudget()
        {
            Name = BudgetName + GetRandomString(),
            DeliveryMethod = BudgetDeliveryMethod.Standard,
            AmountMicros = Amount,
            ExplicitlyShared = bIsShared
        };

        CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()
        {
            Create = budget
        };

        MutateCampaignBudgetsResponse response = await budgetService.MutateCampaignBudgetsAsync(CustomerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });
        return response.Results[0].ResourceName;

    }
    public void CreateCampaign()
    {
        AdData data = new AdData();
        Int64 CustomerId = 0;    //Use your own here
        data.MonthlyBudget= 50;

        string sBudgetResourceName = Convert.ToString(CreateBudget(data, CustomerId));
    }

    private async System.Threading.Tasks.Task<string> CreateBudget(AdData data, Int64 CustomerId)
    {
        string BudgetName = "Shared Budget ";
        long Amount = 0;
        Boolean bIsShared = true;

        CampaignBudgetServiceClient budgetService = Client.GetService(Services.V12.CampaignBudgetService);

        Amount = Convert.ToInt64(Math.Floor(data.MonthlyBudget * 100 / 30.4)) * 10000;

        CampaignBudget budget = new CampaignBudget()
        {
            Name = BudgetName + GetRandomString(),
            DeliveryMethod = BudgetDeliveryMethod.Standard,
            AmountMicros = Amount,
            ExplicitlyShared = bIsShared
        };

        CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()
        {
            Create = budget
        };

        MutateCampaignBudgetsResponse response = await budgetService.MutateCampaignBudgetsAsync(CustomerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });
        return response.Results[0].ResourceName;

    }

    private string GetRandomString()
    {
        return string.Format($"{Guid.NewGuid()} - {DateTime.Now.ToString("yyyy-M-d H:m:s.ffffff")}");
    }