我有一个 UWP 应用程序托管应用服务 https://learn.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service在同一过程中(进程内应用服务 https://learn.microsoft.com/en-us/windows/uwp/launch-resume/convert-app-service-in-process).
当从 UWP 主机应用程序正在运行(以及运行时)的同一台 PC 中运行的控制台应用程序使用该 AppService 时,我注意到AppServiceConnection
持续25-30秒。此后,无法与 UWP 主机执行进一步的通信,除非AppServiceConnection.OpenAsync()
再次被执行。
相反,以下question https://stackoverflow.com/questions/41945981/limitations-of-remote-app-service-background-task-in-uwp表明 AppService 连接没有时间限制。
有人可以澄清预期的行为应该是什么,或者指出我可能会缺少什么以获得“无限时间”的 AppService 连接吗?
代码片段
UWP 主机 - 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();
}
...
控制台客户端应用程序
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));
}
}
}
截图