Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to cancel service.SearchStreamAsync() ? #360

Closed
sommmen opened this issue Mar 8, 2022 · 3 comments · Fixed by #369
Closed

How to cancel service.SearchStreamAsync() ? #360

sommmen opened this issue Mar 8, 2022 · 3 comments · Fixed by #369
Labels
question Further information is requested

Comments

@sommmen
Copy link

sommmen commented Mar 8, 2022

What is your question?

Hello - i'd like for my app to be able to shutdown gracefully and for that i need to be able to cancel any outgoing requests - it seems like the method:

var service = _client.GetService(Services.V10.GoogleAdsService);
var customerService = _client.GetService(Services.V10.CustomerService);
var searchStream = await service.SearchStreamAsync(customerId, query, resp =>
{
    results.AddRange(resp.Results);
});

Does not have an overload for cancellationToken. I could not find anything in the docs - how can i cancel any outgoing requests?

@sommmen sommmen added the question Further information is requested label Mar 8, 2022
@AnashOommen
Copy link
Member

Hi @sommmen right now you have to write a longer version like this:

string query = "YOUR_QUERY_HERE";
var request = new SearchGoogleAdsStreamRequest()
{
    CustomerId = customerId.ToString(),
    Query = query,
};

GoogleAdsServiceClient googleAdsService = client.GetService(
    Services.V10.GoogleAdsService);

SearchStreamStream searchStream = googleAdsService.SearchStream(request);

var responseStream = searchStream.GetResponseStream();
while (await responseStream.MoveNextAsync(CancellationToken.None))
{
    SearchGoogleAdsStreamResponse resp = responseStream.Current;
    foreach (GoogleAdsRow row in resp.Results)
    {
        // Process the row.
    }
}

If I added an optional CancellationToken parameter on the SearchStreamAsync method, that will solve the issues, right?

@sommmen
Copy link
Author

sommmen commented Mar 8, 2022

Yeah generally any Async method that supports cancellation in some way should have a CancellationToken cancellationToken = default parameter.

After browsing a bit it looks like the following works as well:

_ = await service
    .SearchStreamAsync(customerId, query, resp =>
    {
        results.AddRange(resp.Results);
    }, SummaryRowSettingEnum.Types.SummaryRowSetting.NoSummaryRow, CallSettings.FromCancellationToken(cancellationToken));

I'm doing the same for customerService.ListAccessibleCustomers:

var customers = customerService
    .ListAccessibleCustomers(new ListAccessibleCustomersRequest(), CallSettings.FromCancellationToken(cancellationToken))
    .ResourceNames
    .ToArray<string>();

Anyhow adding a cancellation token to the async method would be great and would be what .net devs expect. If what i'm doing right now is the way to go that is acceptable as well - but i'd love a small mention in the docs in that case - because i just found this by some trial and error with the code so it was hard to find.

AnashOommen added a commit that referenced this issue Mar 14, 2022
…set a cancellationtoken on the callsettings instead. Fixes #360
AnashOommen added a commit that referenced this issue Mar 15, 2022
…set a cancellationtoken on the callsettings instead. Fixes #360 (#369)
@sommmen
Copy link
Author

sommmen commented Mar 16, 2022

Thanks buddy!

rakia-abdi pushed a commit to rakia-abdi/googleads that referenced this issue Aug 31, 2022
…set a cancellationtoken on the callsettings instead. Fixes googleads/google-ads-dotnet#360 (#369)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants