Fulltrust AppService in Mixed reality application -Example
Published Jan 15 2019 02:26 PM 750 Views
Microsoft
First published on MSDN on Mar 13, 2018


In this Example Demo we are  going to build a sample Mixed reality application which will call full trust Appservice . The app service will execute logic and returned the result back to the mixed reality application. The appservice is fulltrust application that mean that you can call API and method in this component which is typically win32 application. In our example we are going to write a text file on the desktop as an example. Please note that you need fulltrust capabilities enabled on your app if you would like the app to publish to windows store.



Step 1: Download mixed reality toolkit package

In this Step You will Download the mixed reality toolkit package. To obtained mixed reality toolkit package please visit a release note of mixed reality GitHub repository. At the time of this writing the latest release note is here -->  https://github.com/Microsoft/MixedRealityToolkit-Unity/releases/tag/2017.2.1.3

Note: Always download the latest mixed reality toolkit unity package. MRTK team roll new update very quickly

Step 2: Create project in Unity  & Import MRTK in your project

Create a new project in unity and import mixed reality toolkit in your project.   You can do this by choosing Assets -->Import package --> Custom package and then browsing to the mixed reality toolkit unity package file.





Step 3: Apply Mixed reality setting on your project

Once you import mixed reality toolkit, you will see a new menu in Unity "Mixed Reality toolkit". Click on it and apply "mixed reality project settings"  , followed by "mixed reality scene setting" . You can find the two setting under configure menu of Mixed reality main menu. Choose default check when doing this.

Step 4: Build your Game/App in Unity

We are going to simply add one cube to the scene. Create new cube in the scene and add "TapResponder" script to the cube.

Your scene should look like this







I have also added material to the project and have applied that material on the cube.  On the cube, click on the tapresponder script file , this will open the script file, change the code of onInputClicked event to as in below. To Avoid typing the code, you can copy the code from here and paste it in TapResponder.cs file.



in OnInputClicked, we are calling another static method (which we are going to write in later steps). This method is call every time the user tap on the cube.  The method return a random Color in the string and based on this value we changing the color of the Cube.

The #if  !UNITY_EDITOR tag will ensure that the code is not executed inside the unity editor.

Note: You can use motion controller trigger button to tap on the cube. Just focus on the cube, when the cursor is on the cube, click the trigger button on motion controller.

Step 5: create a new C# file in your project root (in Asset Folders) and name it "DeskopBridgeClass.cs". We will be creating a new class in this file and static methods. Double click on the file and copy the code from here .



At this stage we have completed Unity portion. The remainder of this project will be completed in visual studio. In this code we are wiring up launching the fulltrust process and also coding  the call to the app service bridge.

Step 6: Build the project

In unity from the file menu-->Build settings Choose Build type value "XAML" and click on Build. It will ask for path to build visual studio project. Provide a path where the visual studio project will be copied after build. Ensure you have checked  "Unity C# Project".

The reason we are choosing to build XAML here is get an instance of Application class. We need this class for AppServiceConnection which will be retrieve when background task is triggered. This entire logic is coded in OnBackgroundActivated method.

Please note that there is general consensus among the game developer that XAML increase the startup time by 1 second and also increase the memory usage by ~10MB. Ensure that is not an issue. For more info on this please refer to this Unity discussion .





Step 7:  Complete the remainder of code in Visual Studio

Open the VS project which you have build in the previous section.  In the main mixed reality project [Universal Windows] , open App.xaml.cs file. Do the following modificatio

1.Add the following using statements at the top of the file.

using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.AppService;
using AssemblyCSharpWSA;




2. Add new private variable in the app class

BackgroundTaskDeferral appServiceDeferral = null;




3.  In the constructor of the app class  call DeskopBridgeClass.activateFulltrust(); Method

4. add OnBackgroundActivated and OnTaskCanceled method to App Class.


protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{


base.OnBackgroundActivated(args);
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails)
{
appServiceDeferral = args.TaskInstance.GetDeferral();
args.TaskInstance.Canceled += OnTaskCanceled;
AppServiceTriggerDetails details = args.TaskInstance.TriggerDetails as AppServiceTriggerDetails;
DeskopBridgeClass.Connection = details.AppServiceConnection;
}
}



private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
if (this.appServiceDeferral != null)
{
// Complete the service deferral.
this.appServiceDeferral.Complete();
}
}


Step 8:  Add fulltrust capabilities to Package.appxmanifest

a. Right click on the package.AppxManifest file and click on "View Code"  Add  fulltrust  xml namespace in the package attribute


xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"


b. Add rescap to the IgnorableNameSpaces


IgnorableNamespaces="uap uap2 mp rescap desktop">


c. Add fulltrust to the capabilities tag


<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="runFullTrust" />
</Capabilities>


d . Add an App Service Extention and fulltrust extension to the package.appxmanifest file.

<Extensions>
<uap:Extension Category="windows.appService">
<uap:AppService Name="CommunicationService" />
</uap:Extension>
<desktop:Extension Category="windows.fullTrustProcess" Executable="win32\BackgroundProcess.exe" />
</Extensions>


CommunicationService is the AppServiceName. win32\BackgroundProcess.exe is an executable which will run in fulltrust and implement CommunicationService AppserviceBridge. We have not created this project yet. You can download this project from here . Compile this project, and place the binary in the win32 folder of the main UWP application.



You  do not have to add pdb file  if you are building release version.



Step 9:  Test

Compile and test the project. The app will launch in the mixed reality headset , when the app is launched, it will also launch the win32 process. When you tap on the Cube, The cub change the color. At the same time the win32 executable create a file "RandomNumber.txt" on the desktop of the current user. This file will record all the numbers which is generated by the win32 process when the user tap on the cube in the mixed reality headset.





Co-Authors
Version history
Last update:
‎Nov 22 2022 12:26 PM
Updated by: