I am working on a project where I play a couple of videos. To be able to play those videos I need to fetch their data when requested by the app through HttpClient
from System.Net.Http
. Everything works normally and the UWP app downloads required information from Internet when the app is in foreground.
As soon as the app leaves foreground and gets minimized by user the HttpClient
, whether it's from System.Net.Http
namesoace or Windows.Web.Http
namespace, does not work. I have even tried to set a breakpoint there and as soon as I move forward HttpClient
does not respond to the await client.GetAsync()
method and stays there without returning any result unless you activate the app again and then it returns any value. I cant use BackgroundTasks
because they need Triggers
but I need to access data on demand.
I have also read this article describing to request ExtendedExecutionState
but the result is still the same. I request this state when the app gets a request to play a list of items. Even after getting the result as Allowed
, the HttpClient
has the same behavior as above.
Is there any method that can execute internet related queries when requested and get some information?
The code that I execute to access data from internet is:
public static async Task<string> GetResponseDataFromAPI(string apiRequestUrl, CancellationTokenSource cts = default)
{
cts = cts ?? new CancellationTokenSource(TimeSpan.FromSeconds(20));
try
{
var responseData = await AuthRequestHelper.globalHttpClient.GetAsync(apiRequestUrl, cts.Token);
var result = await responseData.Content.ReadAsStringAsync();
return result;
}
catch (Exception ex)
{
INotifyHelper.iNotifyObject.IsVideoLoading = false;
INotifyHelper.iNotifyObject.IsDataLoading = false;
// show error
return "{ \"error\": {\"code\": " + $"\"{ex.HResult}\", \"message\": \"{ex.Message}\"" + "} }";
}
}
The returned value is then analyzed to get required data from it.
The code that is executed to request ExtendedExecutionSession
is:
public static async void RequestBackgroundExtendedExecution()
{
using (var session = new ExtendedExecutionSession())
{
session.Reason = ExtendedExecutionReason.Unspecified;
session.Description = "Background Playlist Playback";
session.Revoked += Session_Revoked;
if (await session.RequestExtensionAsync() is ExtendedExecutionResult result)
{
if (result == ExtendedExecutionResult.Denied)
{
// show user that background playlist playback will not be possible
}
}
}
}
Is there anything that can be done to execute the requested operations while in minimized state? A little help to point in the right direction will be much appreciated. Thanks