Reputation: 548
While stepping through the debugger, the IBackgroundTask.Run()
method executes, but HttpClient().GetAsync()
never returns or throws an exception. (Obviously) nothing is wrong with this bit if I run it in a foreground method.
public sealed class BackgroundHttpClientTest : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
var response = await new Windows.Web.Http.HttpClient().GetAsync(new Uri("https://www.someUrl.com"));
}
}
Not sure what I'm missing here. I double checked all of the
declarations in the appmanifest and just don't know where else to look. Is there some limitation with IBackgroundTask
that I'm unaware of?
Edit: forgot to mention this is for a Win10 Universal app
Upvotes: 2
Views: 182
Reputation: 8039
You need to use task deferrals when using async methods in your background task, otherwise the Task can terminate unexpectedly when the execution flow reaches the end of the Run
method.
public sealed class BackgroundHttpClientTest : IBackgroundTask
{
BackgroundTaskDeferral _deferral;
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
var response = await new Windows.Web.Http.HttpClient().GetAsync(new Uri("https://www.someUrl.com"));
_deferral.Complete();
}
}
Read more in the official documentation here
Upvotes: 1