我有一个托管应用程序服务的主机 UWP 应用程序,在其 App.xaml.cs 中我有以下代码: 私有(private)BackgroundTaskDeferral _appServiceDeferral; 私有(private) AppServiceConnection _appServiceConnection; protected 覆盖 void OnBackgroundActivated(BackgroundActivatedEventArgs args) { base.OnBackgroundActivated(参数); 如果(args.TaskInstance.TriggerDetails 是 AppServiceTriggerDetails) { _appServiceDeferral = args.TaskInstance.GetDeferral(); args.TaskInstance.Canceled += OnAppServicesCanceled; AppServiceTriggerDetails details = args.TaskInstance.TriggerDetails as AppServiceTriggerDetails; _appServiceConnection = details.AppServiceConnection; _appServiceConnection.RequestReceived += Connection_RequestReceived; _appServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed; }
我的主机应用程序是一个 UWP 应用程序,它会在某个时间启动一个 win32 应用程序,Win32 是一个客户端应用程序。我有以下 Win32 应用程序代码:
connection = new AppServiceConnection(); connection.AppServiceName = "MyTestCommunicationService"; connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName; connection.RequestReceived += ClientConnection_RequestReceived; connection.ServiceClosed += Connection_ServiceClosed;
var status = await connection.OpenAsync().AsTask();
在 UWP 主机应用程序中,我可以向 win32 应用程序发送消息:
if (_appServiceConnection!= null)
{
ValueSet valueSet = new ValueSet();
valueSet.Add("Command", dataContent);
AppServiceResponse response = await _appServiceConnection.SendMessageAsync(valueSet);
_isSuccess = response.Status == AppServiceResponseStatus.Success;
}
我看到很多例子,客户端向应用程序连接的主机应用程序发送请求,然后主机应用程序处理请求。我想知道我的代码是否是在主机应用程序和 win32 应用程序之间进行双向通信的正确方法。我测试了它正在运行的代码,但想知道有关应用服务连接的最佳实践。
最佳答案
是的,AppServiceConnection 是一个双向通信管道。双方都有一个 AppServiceConnection 实例,可用于向另一方发起请求,以及接收来自另一方的请求。
关于uwp - AppServiceConnection可以双向通信吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939592/