In Windows 10 taste 2004, we’re introducing the concept that that of Hosted Apps to the Windows App Model. Hosted apps are registered as unbiased apps on Windows, however require a number procedure so that you could run. An instance could be a script file which calls for its host (eg: Powershell or Python) to be put in. By itself, it is just a file and does no longer have any approach to seem as an app to Windows. With the Hosted App Model, an app can claim itself as a number, after which programs can claim a dependency upon that host and are referred to as hosted apps. When the hosted app is obtainable, the host executable is then introduced with the id of the hosted app equipment as a substitute of its personal id. This lets in the host so that you can get right of entry to the contents of the hosted app equipment and when calling APIs it does so with the hosted app id.

Modern apps are outlined to Windows by way of signed MSIX programs. A equipment supplies id, so it’s identified to the gadget and contains all the knowledge, property, and registration wisdom for the app it contains. Many apps have scenarios the place they wish to host content material subject material topic subject material and binaries, very similar to extensibility issues, from different apps. There also are scenarios the place the host app is additional of a runtime engine that relatively somewhat script content material subject material topic subject material. On very best of all of it, there’s a want to have those hosted apps to appear and behave like a separate app at the gadget – the place it has its personal get started tile, id, and deep integration with Windows possible choices very similar to BackgroundTasks, Notifications, and Share. Using the Hosted App Model, a retail kiosk app can simply be rebranded, or a Python or Powershell script can now be handled as a separate app.

Developers try to accomplish this at the moment in both of 2 techniques. First, they just use a shortcut at the desktop to free up the host. But this enjoy does no longer have any deep integration with Windows and the shell, given that ‘app’ is the host executable no longer the script. To get an extra deeply built-in enjoy, the opposite is for builders to create a packaged app that comes with the host binaries inside the equipment. While the equipment would now be a separate app and provide the probability for deep Windows integration, this way is inefficient as each and every app would want to redistribute the host and could have conceivable servicing and licensing problems.

The Hosted App Model solves the wishes of those hosted apps. The Hosted App Model is decided by means of two items, a “Host” which is made to be had to different apps, and a “Hosted App” that declares a dependency upon the host. When a hosted app is obtainable, the result’s that the host is then operating beneath the id of the hosted app equipment, so it might more than likely load visible property, content material subject material topic subject material from the Hosted App equipment location, and when it calls APIs it does so with the id declared within the Hosted App. The Hosted App will get the intersection of functions declared between the Host and Hosted App – which means that that that if a Hosted App can not ask for extra functions than what the Host supplies. In this preliminary unencumber of the Hosted App Model packaged desktop apps are supported, and we can be increasing enhance to UWP hosts in longer term releases.

More specifically, a Host is the executable in a equipment declared by the use of the HostRuntime extension which issues to the principle executable or runtime procedure for the hosted app. The HostRuntime extension has an Id function, and this identifier is referenced as a dependency by the use of the Hosted App in its equipment manifest. A number can come to a decision the equipment id it’s at the moment operating beneath by the use of relating to the Windows.ApplicationModel.Package.Current api.

A Hosted App is an app that declares a equipment dependency on a Host, and leverages the HostRuntime Id for activation as a substitute of specifying an Entrypoint executable in its personal equipment. It most often contains content material subject material topic subject material, visible property, scripts, or binaries that can be accessed by the use of the host. Hosted App programs will also be Signed or Unsigned:

  • Signed programs might include executable knowledge. This turns out to be useful in scenarios that experience an extension mechanism, permitting the host to load a dll or registered segment within the hosted app equipment.
  • Unsigned programs can best include non-executable knowledge. This turns out to be useful in scenarios the place the hostruntime best will have to load pictures, property and content material subject material topic subject material very similar to script knowledge. Unsigned programs will have to come with a undeniable Unsigned Publisher OID of their Identity or they gained’t be allowed to test in. This prevents unsigned programs from spoofing a signed equipment id.

Declaring a Host

Declaring a Host is quite easy. All you want to do is to say the HostRuntime equipment extension to your AppxManifest.xml. The HostRuntime extension is package-wide and so is asserted as a kid of the equipment segment. Below is an excerpt from an instance AppxManifest.xml appearing the HostRuntime get entry to that declares an app as a Host with Id “PythonHost.”

Code example of AppxManifest.xml showing the HostRuntime entry that declares an app as a Host with Id “PythonHost”

  • hostRuntime – a package-wide extension defining runtime wisdom used when activating a Hosted App.
  • Executable – The executable binary that would be the host procedure
  • RuntimeBehavior and TrustLevel – A hosted app will run with the definitions expressed within the extension. For instance, a hosted app the use of the Host declared above will run the executable PyScriptEngine.exe, at mediumIL imagine degree.
  • HostRuntime Id – A novel identifier used to specify a Host in a equipment. A equipment could have a couple of Host Apps, and each and every will have to have a singular HostRuntime Id. This identifier is referenced by the use of the Hosted App.

A hosted app will have to claim a equipment dependency upon the host, and specify the HostId to make use of. If the equipment is unsigned, it is going to have to go back with the Unsigned Publisher OID to ensure the equipment id does no longer combat with a signed equipment. Also the TargetDeviceFamily will have to are compatible the host so it does no longer try to deploy on devices that aren’t supported by the use of the host. The following is an instance of a manifest for a Hosted App that takes a dependency upon the Python host.

Code example of a manifest for a Hosted App that takes a dependency upon the Python host

  • Unsigned Publisher OID2.25.311729368913984317654407730594956997722=1 This identifier is sought after when a Hosted App can be unsigned. The identifier guarantees any unsigned equipment can not spoof the id of a signed equipment.
  • HostRuntimeDependency – A Hosted App equipment will have to claim a HostRuntimeDependency at the Host app. This is composed of the Name and Publisher of the Host equipment, and the min taste it’s made up our minds via. These will also be found out beneath the <Identity> segment within the Host equipment. When deployed, if the HostRuntimeDependency can’t be found out, the registration fails.
  • HostId – Instead of bringing up the standard Executable and EntryPoint for an app or extension, the HostId function expresses a dependency on a Host app. As a consequence, the Hosted App inherits the Executable, EntryPoint and runtime attributes of the Host with the required HostId. When registered, if the HostId isn’t found out, the deployment fails.
  • Parameters (no longer necessary)– parameters which can be handed at the command line to the host app. The host must grab what to do with those parameters, and so there’s an implied contract between the host and hosted app.

One of the advantages of the brand new HostRuntime is that it allows a number to dynamically take a look at in a hosted app equipment at runtime. This dynamically registered equipment does no longer want to be signed. This lets in a number to dynamically generate the content material subject material topic subject material and manifest for the hosted app equipment after which take a look at in it. We are operating with the brand new Microsoft Edge browser to have the benefit of the Hosted App Model for Progressive Web Apps (PWAs) – changing the internet app manifest into an app manifest, equipment the extra internet content material subject material topic subject material into an MSIX equipment and take a look at in it. In this type, a PWA is its personal unbiased app registered to the gadget even though it’s being hosted by the use of Edge.

The new APIs for registering a equipment are:

  • Management.Deployment.PackageManager.UploadPackageByUriAsync() is used for registering an MSIX equipment
  • Management.Deployment.PackageManager.Check inPackageByUriAsync() is used for registering a free file AppxManifest.xml file.

In the case the place the hosted app is unsigned, its manifest will have to meet the next will have to haves:

  1. The unsigned equipment can not include any Executable attributes in its Application or Extension parts (e.g.: no <Application Executable=…> or <Extension Executable=…>), and it might more than likely’t specify every other activation knowledge (Executable, TrustLevel, and so forth). The Application node best helps the HostId and Parameters parts.
  2. An unsigned equipment will have to be a Main equipment kind – it can’t be a Bundle, Framework, Resource or Optional equipment.

In flip, the host procedure registering an unsigned hosted app equipment will have to meet the next will have to haves:

  1. The procedure will have to have equipment id
  2. The procedure will have to have the equipment keep watch over capacity <rescap:Capability Name=”equipmentManagement”/>

Let’s take a look at two examples. The first, WinFormsToastHost, is a Host with a signed Hosted App that shows come with an extension this is dynamically loaded into the host. The 2nd, NumberGuesser, an instance of the use of python as a number and a script file as a hosted app equipment. You can to hunt out the improvement code for every at https://aka.ms/hostedappsample.

WinFormsToastHost

Host

The host on this instance is a straightforward Windows Forms app that presentations its equipment id, location, and calls the ToastNotification APIs. It additionally has the potential to load a binary extension from a hosted app equipment. When run beneath its personal id, it does no longer show the extension wisdom. The app is packaged with the Windows App Packaging Project which incorporates the manifest declarations for being a number.

WinformsToastHost-Extension

The hosted app is a .NET dll that implements an extension mechanism for the host to load. It additionally features a packaging endeavor that declares its id and dependency upon the hostruntime. You will see this id mirrored within the values displayed when the app is administered. When registered, the hostruntime has get right of entry to to the hostedapp’s equipment location and thus can load the extension.

Running the improvement

You can load the supply code in Visual Studio as follows:

  1. Open WinformsToastHost.sln in VS2019
  2. Build and deploy WinformsToastHost.Package
  3. Build and deploy HostedAppExtension
  4. Goto Start menu and unencumber ‘WinformsToastHost’
  5. Goto Start menu and unencumber ‘Hosted WinformsToastHost Extension‘

Here is a screenshot of the host operating. Notice its equipment id and trail, and the UX for loading an meeting isn’t to be had as a result of it is not operating as a hosted app.

Screenshot of the WinForms Toast host running.

Now unencumber the hosted app. Notice the id and trail have modified, and that the UX for dynamically loading an extension meeting is enabled.

Screenshot of the WinForms Toast host running.

When the “Run hosted” button is pressed, you’ll get a conversation from the binary extension:

Screenshot of a message from the hosted app.

Here is the Task Manager main points view appearing every apps operating on the similar time. Notice that the host binary is the executable for every:

Screenshot of Task Manager .

And when clicking at the Show Toast button for each and every app, the gadget acknowledges the 2 other identities within the motion center:

Screenshot of Show Toast button.

The Host

In this case, the host is created from 2 tasks – first is PyScriptEngine which is wrapper written in C# and uses the Python nuget equipment to run python scripts. This wrapper parses the command line and has the potential to dynamically take a look at in a manifest at the side of unencumber the python executable with a trail to a script file. The 2nd endeavor is PyScriptEnginePackage which is a Windows App Packaging Project that installs PyScriptEngine and registers the manifest that comes with the HostRuntime extension.

The Hosted App

The Hosted App is made up of a python script, NumberGuesser.py, and visible property. It doesn’t include any PE knowledge. It has an app manifest the place the declarations for HostRuntimeDependency and HostId are declared that identifies PyScriptEngine as its Host. The manifest additionally contains the Unsigned Publisher OID get entry to this is required for an unsigned equipment.

Running the improvement

To run this construction you first want to compile and deploy the host, then you’ll be able to use the host from the commandline to dynamically sign inside the hosted app.

  1. Open PyScriptEngine.sln answer in Visual Studio
  2. Set PyScriptEnginePackage given that Startup endeavor
  3. Build PyScriptEnginePackage
  4. Deploy PyScriptEnginePackage
  5. Because the host app declares an appexecutionalias, it is conceivable in an effort to to speak about with a command really useful and run “pyscriptengine” to get the utilization understand:

Screenshot of a usage notice.

6. Use the python host to sign inside the NumberGuesser recreation from the commandline:

Screenshot of registering the numberGuesser game

7. Now, click on on on on “Number Guesser (Manifest)” to your get started menu, and run the sport! See what number of tries it takes you to bet the quantity:

Screenshot of Number Guesser (Manifest)”

Let’s test what’s operating. Notice how PyScriptEngine is executing beneath the equipment id of NumberGuesser!

Screenshot of PyScriptEngine is executing under the package identity of NumberGuesser.

In abstract, we’re happy to place throughout you additional energy and contours to the home house home windows platform, and we’re excited to seem what inventive concepts you’ll have for the Hosted App Model. In addition to Microsoft Edge, we’re operating with groups around the corporate and be expecting to seem additional apps leveraging the Hosted App Model at some point.