Is there a limit on the connection duration using AppService?

170 Views Asked by At

I have a UWP app that hosts an AppService in it's same process (in-process AppService).

When consuming that AppService from a Console app running in the same PC the UWP host app is running (and while it is running), I have noted that the AppServiceConnection lasts between 25-30 seconds. After that time no further communication can be performed to the UWP host unless AppServiceConnection.OpenAsync() is executed again.

Aversely, the following question suggests that there is no time limit to an AppService connection.

Could someone clarify what the expected behavior should be or point out what I might be missing in order to have a "unlimited time" AppService connection?

code snippets

UWP host - app.xaml.cs

...

private AppServiceConnection AppServiceConnection;
private BackgroundTaskDeferral AppServiceDeferral;

protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
    base.OnBackgroundActivated(args);

    IBackgroundTaskInstance taskInstance = args.TaskInstance;
    AppServiceTriggerDetails appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;
    AppServiceDeferral = taskInstance.GetDeferral();
    taskInstance.Canceled += OnAppServicesCanceled;
    AppServiceConnection = appService.AppServiceConnection;
    AppServiceConnection.RequestReceived += OnAppServiceRequestReceived;
    AppServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed;
}
private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
    AppServiceDeferral messageDeferral = args.GetDeferral();
    ValueSet response = (await IPC.Controller.ProcessIncomeRequests(args.Request.Message.ToDictionary())).ToValueSet();
    await args.Request.SendResponseAsync(response);
    messageDeferral.Complete();
}

private void OnAppServicesCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
    AppServiceDeferral.Complete();
}
private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
{
    AppServiceDeferral.Complete();
}

...

Console client app

public static AppServiceConnection AppServiceConnection;
public static async Task<bool> ConnectToUWPApp()
{
    if(AppServiceConnection != null)
        AppServiceConnection.Dispose();

    AppServiceConnection = new AppServiceConnection() 
    {
        AppServiceName = "<AppServiceName>",
        PackageFamilyName = "<PackageFamilyName>"
    };
    var connStatus = await AppServiceConnection.OpenAsync(); // Opens AppService Connection to the installed UWP App
    return connStatus == AppServiceConnectionStatus.Success;
}

static async Task Main(string[] args)
{
    Console.WriteLine("Testing background communication time");
    Console.WriteLine("Press ENTER key to start the test");
    while (Console.ReadKey(true).Key != ConsoleKey.Enter) ;

    Stopwatch stopwatch = new Stopwatch();
    List<double> connectionsDuration = new List<double>();
    while (true)
    {
        Console.Write("\n\nConnecting... ");
        bool isIPCConnected = await ConnectToUWPApp();

        stopwatch.Restart();

        if (isIPCConnected)
        {
            Console.WriteLine("Succeeded");
            Console.Write("Exchanging IPC messages ");
            while ((await SendIPCMessage(new ValueSet() { }, false))?.StatusCode != null) 
            { 
                await Task.Delay(100);
                Console.Write(".");
            }
            stopwatch.Stop();
            connectionsDuration.Add(stopwatch.Elapsed.TotalSeconds);
            Console.WriteLine($"\nIPC connection lost after {stopwatch.Elapsed.TotalSeconds} seconds");
            Console.WriteLine($"Total connections duration average: {connectionsDuration.Average()} seconds");
        }
        else
        {
            Console.WriteLine("IPC Connection could not be established. Please make sure there's a running instance of the UWP App.");
            Console.WriteLine("Retrying in 30 seconds...");
            await Task.Delay(TimeSpan.FromSeconds(30));
        }
    }

}

screenshots

enter image description here

1

There are 1 best solutions below

0
On

Since In-process AppService is started via OnBackgroundActivated, according to the Background task resource constraints, background tasks are limited to 30 seconds of wall-clock usage.