アプリ サービスを使用すると、アプリは他のアプリにサービスを提供できます。 アプリ サービスは、別のプロセスでバックグラウンド タスクとして実行され、要求/応答通信チャネルを公開します。 他のアプリは、 AppServiceConnection を使用してサービスに接続します。
Important
アプリ サービスには MSIX パッケージ ID が必要です。 プロバイダー アプリとコンシューマー アプリの両方をパッケージ化する必要があります。 パッケージ化されていないデスクトップ アプリは、アプリ サービスをホストまたは接続できません。
Note
Windows アプリ SDKアプリは、アウトプロセスバックグラウンド タスクとしてのみアプリ サービスをホストできます。
Microsoft.UI.Xaml.Applicationでは、OnBackgroundActivatedオーバーライドは提供されません。ExtendedActivationKind にはアプリ サービスのアクティブ化の種類が含まれていないため、Windows アプリ SDK アプリは、UWP アプリの場合と同様に、独自のプロセスで直接アプリ サービスのアクティブ化を受け取ることはできません。 詳細については、インプロセス アプリ サービスはサポートされませんを参照してください。
アプリ サービスのしくみ
アプリ サービスは、次の要素で構成されます。
- サービスを宣言して実装するプロバイダー アプリ。
- サービスに接続して要求を送信するコンシューマー アプリ。
通信では、要求データと応答データに ValueSet オブジェクトが使用されます。
アプリ サービス プロバイダーを作成する
手順 1: マニフェストでアプリ サービスを宣言する
プロバイダー アプリのappServiceにPackage.appxmanifest拡張機能を追加し、サービスを実装するバックグラウンド タスク プロジェクトのエントリ ポイントを指定します。
<Extensions>
<uap:Extension Category="windows.appService">
<uap:AppService Name="com.example.myappservice"
EntryPoint="MyAppService.ServiceTask" />
</uap:Extension>
</Extensions>
手順 2: サービスを実装する
別のWindows ランタイム コンポーネント プロジェクトを作成し、IBackgroundTaskを実装します。 バックグラウンド タスクはアウトプロセスで実行されるため、プロバイダー アプリが Windows アプリ SDK と UWP のどちらを使用してビルドされているかに関係なく、このパターンが機能します。
// MyAppService.ServiceTask, in a separate Windows Runtime Component project
public sealed class ServiceTask : IBackgroundTask
{
private BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
taskInstance.Canceled += OnTaskCanceled;
var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
if (details != null)
{
details.AppServiceConnection.RequestReceived += OnRequestReceived;
}
}
private async void OnRequestReceived(AppServiceConnection sender,
AppServiceRequestReceivedEventArgs args)
{
var deferral = args.GetDeferral();
var response = new ValueSet();
response.Add("result", "Hello from the service!");
await args.Request.SendResponseAsync(response);
deferral.Complete();
}
private void OnTaskCanceled(IBackgroundTaskInstance sender,
BackgroundTaskCancellationReason reason)
{
_deferral?.Complete();
}
}
アプリ サービスを使用する
コンシューマー アプリから、 AppServiceConnection を作成し、要求を送信します。
using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections;
private async Task CallAppServiceAsync()
{
var connection = new AppServiceConnection
{
AppServiceName = "com.example.myappservice",
PackageFamilyName = "ProviderApp_1234567890abc"
};
var status = await connection.OpenAsync();
if (status != AppServiceConnectionStatus.Success)
{
// Handle connection failure
return;
}
var request = new ValueSet
{
{ "command", "add" },
{ "a", 5 },
{ "b", 3 }
};
var response = await connection.SendMessageAsync(request);
if (response.Status == AppServiceResponseStatus.Success)
{
int result = (int)response.Message["result"];
// result is 8
}
connection.Dispose();
}
Tip
PowerShell でPackageFamilyNameを実行してプロバイダー アプリのGet-AppxPackage -Name *ProviderApp*を見つけるか、プロバイダー アプリのパッケージ マニフェストを確認します。
サービスがプロセスを使い果たす必要がある理由
アプリ サービスのバックグラウンド タスクは、常に独自のプロセス (または UWP プロバイダーの場合のみプロバイダー アプリのプロセス) で実行されます。 Windows アプリ SDK プロバイダー アプリの場合、アプリ独自のプロセスでアプリ サービスのアクティブ化を受け取る方法がサポートされていないため、バックグラウンド タスクはアウトプロセスで実行される必要があります。 詳細については、「 インプロセス アプリ サービスはサポートされていません」を参照してください。
関連するコンテンツ
Windows developer