How-To: Get Started with Hosting
Hosting
can be used to register services that will be accessible throughout the application via dependency injection (DI). This tutorial will walk you through the critical steps needed to leverage hosting in your application.
Important
This guide assumes you used the template wizard or dotnet new unoapp
to create your solution. If not, it is recommended that you follow the Creating an application with Uno.Extensions documentation to create an application from the template.
Step-by-step
1. Installation
Add
Hosting
to the<UnoFeatures>
property in the Class Library (.csproj) file. If you already haveExtensions
in<UnoFeatures>
, thenHosting
is already installed, as its dependencies are included with theExtensions
feature.<UnoFeatures> Material; + Hosting; Toolkit; MVUX; </UnoFeatures>
2. Create and Configure IApplicationBuilder
We need to expose the
IHost
instance to the rest of App.cs. Add the following property to your class file:private IHost Host { get; set; }
As soon as your app is launched, use the
CreateBuilder()
extension method to instantiate anIApplicationBuilder
from yourApplication
object:protected override void OnLaunched(LaunchActivatedEventArgs e) { var appBuilder = this.CreateBuilder(args) .Configure(host => { // Configure the host builder }); ... }
3. Build the IHost
Finally, build the host and assign it to the
Host
property:protected override void OnLaunched(LaunchActivatedEventArgs e) { var appBuilder = this.CreateBuilder(args) .Configure(host => { // Configure the host builder }); Host = appBuilder.Build(); ... }