Share via


與遠端應用程式服務通訊

除了使用 URI 在遠端裝置上啟動應用程式之外,您也可以在遠端裝置上執行應用程式服務並與其通訊。 任何以 Windows 為基礎的裝置都可以作為用戶端或主機裝置使用。 這可讓您以幾乎無限的方式與連線的裝置互動,而不需要將應用程式帶入前景。

在主機設備上設定應用程式服務

若要在遠端裝置上執行應用程式服務,您必須已經在該裝置上安裝該應用程式服務的提供者。 本指南將使用 CSharp 版本的隨機數產生器應用程式服務範例,此範例可在 Windows 通用範例存放庫中取得。 如需如何撰寫您自己的 UWP 應用程式服務的指示,請參閱建立和取用應用程式服務 (UWP)

無論您是使用已製作的 App Service 還是自行撰寫,都必須進行一些編輯,才能讓服務與遠端系統相容。 在 Visual Studio 中,前往應用程式服務提供者的專案 (在範例中稱為「AppServicesProvider」) 並選擇其 Package.appxmanifest 檔案。 右鍵單擊並選擇查看程式碼以查看文件的完整內容。 在主要 Application 元素內建立 Extensions 元素 (如果已經存在,則找到它)。 然後建立 Extension,將專案定義為應用程式服務,並參考其上層專案。

...
<Extensions>
    <uap:Extension Category="windows.appService" EntryPoint="RandomNumberService.RandomNumberGeneratorTask">
        <uap3:AppService Name="com.microsoft.randomnumbergenerator"/>
    </uap:Extension>
</Extensions>
...

AppService 元素旁邊,新增 SupportsRemoteSystems 屬性:

...
<uap3:AppService Name="com.microsoft.randomnumbergenerator" SupportsRemoteSystems="true"/>
...

若要使用此 uap3 命名空間中的元素,如果指令清單檔尚未存在,您必須在指令清單檔頂端新增命名空間定義。

<?xml version="1.0" encoding="utf-8"?>
<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3">
  ...
</Package>

然後建置您的應用程式服務提供者專案,並將其部署至主機裝置。

從客戶端設備定位應用服務

要呼叫遠端應用程式服務的裝置需要具有遠端系統功能的應用程式。 這可以添加到在主機設備上提供應用程式服務的相同應用程式中 (在這種情況下,您將在兩個裝置上安裝相同的應用程式),或在完全不同的應用程式中實現。

本節中的程序代碼需要下列 using 語句,才能依目前方式執行:

using Windows.ApplicationModel.AppService;
using Windows.System.RemoteSystems;

您必須先具現化 AppServiceConnection 物件,就像在本機呼叫應用程式服務一樣。 建立和使用應用程式服務中會更詳細地說明此程式。 在此範例中,要設為目標的應用程式服務是隨機數產生器服務。

注意

假設 RemoteSystem 物件已在程式代碼中以某種方式取得,而該程式代碼中會呼叫下列方法。 如需如何設定此設定的指示,請參閱啟動遠端應用程式

// This method returns an open connection to a particular app service on a remote system.
// param "remotesys" is a RemoteSystem object representing the device to connect to.
private async void openRemoteConnectionAsync(RemoteSystem remotesys)
{
    // Set up a new app service connection. The app service name and package family name that
    // are used here correspond to the AppServices UWP sample.
    AppServiceConnection connection = new AppServiceConnection
    {
        AppServiceName = "com.microsoft.randomnumbergenerator",
        PackageFamilyName = "Microsoft.SDKSamples.AppServicesProvider.CS_8wekyb3d8bbwe"
    };

接下來,為目標遠端設備建立 RemoteSystemConnectionRequest 物件。 然後,它會用來開啟該裝置的 AppServiceConnection。 請注意,在下面的範例中,為了簡潔起見,錯誤處理和報告被大大簡化。

// a valid RemoteSystem object is needed before going any further
if (remotesys == null)
{
    return;
}

// Create a remote system connection request for the given remote device
RemoteSystemConnectionRequest connectionRequest = new RemoteSystemConnectionRequest(remotesys);

// "open" the AppServiceConnection using the remote request
AppServiceConnectionStatus status = await connection.OpenRemoteAsync(connectionRequest);

// only continue if the connection opened successfully
if (status != AppServiceConnectionStatus.Success)
{
    return;
}

此時,您應該已開啟遠端電腦上的應用程式服務連線。

透過遠端連線交換服務特定訊息

從這裡,您可以透過 ValueSet 物件的形式向服務發送和接收訊息 (關於詳細資訊,請參閱建立和使用應用程式服務)。 隨機數產生器服務會採用兩個具有索引鍵 "minvalue" 的整數,並 "maxvalue" 做為輸入、隨機選取其範圍內的整數,並使用索引鍵 "Result"將它傳回呼叫程序。

    // create the command input
    ValueSet inputs = new ValueSet();

    // min_value and max_value vars are obtained somewhere else in the program
    inputs.Add("minvalue", min_value);
    inputs.Add("maxvalue", max_value);

    // send input and receive output in a variable
    AppServiceResponse response = await connection.SendMessageAsync(inputs);

    string result = "";
    // check that the service successfully received and processed the message
    if (response.Status == AppServiceResponseStatus.Success)
    {
        // Get the data that the service returned:
        result = response.Message["Result"] as string;
    }
}

現在您已連線到目標主機裝置上的應用程式服務、在該裝置上執行作業,以及接收資料給用戶端裝置以回應。

已連線的應用程式與裝置 (Project Rome) 概覽
啟動遠程應用程式
建立和使用應用程式服務
遠端系統 API 參考
遠端系統範例