In this post, we look at a bit of the code behind MauiApp and focus on how the middleware and endpoints are configured.
In my previous post, I looked at the code behind MauiAppBuilder. At the end of the post, we had created an instance of the MauiAppBuilder and called Build() to create a MauiApp.
This article belongs to the series: Exploring MAUI App Builder. In this series, we will talk about some new features included in the new .NET MAUI.
MauiApp: a relatively thin wrapper around one type
In comparison to the MauiAppBuilder constructor, the MauiApp constructor is relatively simple:
public sealed class MauiApp : IHost { private readonly IHost _host; internal MauiApp(IHost host) { _host = host; Logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(Environment.ApplicationName); } // ... }
The constructor and field initializers do 2 primary things:
- Save the provided Host in the _host field. This is the same Host type as when you use the generic host directly, as in the ASP.NET Core 3.x/5.
- Set the Logger property from the host. This property is used to register additional logging providers.
MauiApp mostly delegates the implementation of the IHost.
Starting the MauiApp and building the middleware pipeline
The standard way to build your application is to call builder.Build() on your MauiApp. This in turn calls IHost.StartAsync, which starts the complex startup interactions.
GenericWebHostService is where the middleware pipeline is built in order to run your app. There’s nothing different in this process for MauiApp compared to the generic Host, so rather than dive through the (extensive!) code, we’ll take a look at how MauiApp sets up the middleware pipeline instead.
MauiAPp’s middleware pipeline
One of the big differences with MauiApp compared to the generic host is that MauiApp sets up various middleware by default. In the previous post, I showed that MauiAppBuilder calls MauiHostingDefaults.ConfigureDefaults().
In addition to these, MauiAppBuilder sets up extra middleware. This is where the ConfigureAppConfiguration method I mentioned in the previous post comes in. Depending on how you configure your MauiApp, ConfigureAppConfiguration adds additional middleware to your pipeline.
The code for this is a little complex, as it requires handling multiple edge cases. Rather than try and keep track of the code in too much detail, we’ll look at some examples instead, and see what the resulting middleware pipeline looks like.
The empty pipeline
We’ll start with the most basic (and somewhat pointless) application, in which we don’t add any extra middleware to the application:
MauiAppBuilder builder = MauiApp.CreateBuilder(); builder.UseMauiApp<App>(); MauiAppapp app = builder.Build();
This setup is the most basic of configurations and results in a middleware pipeline that contains an “empty” middleware built from MauiApp’s ApplicationBuilder.
A pipeline with extra middleware
In this example, we add some additional middleware to the pipeline: Fonts, Effects, and Handlers middleware.
MauiAppBuilder builder = MauiApp.CreateBuilder(); builder.UseMauiApp<App>(); // Add some additional middleware builder.ConfigureFonts(fonts => { // Add your fonts here with fonts.AddFont() }); builder.ConfigureEffects(effects => { // Add your effects here with effects.Add() }); builder.ConfigureMauiHandlers(handlers=> { // Add your handlers here with handlers.AddHandler() }); MauiAppapp app = builder.Build();
With this setup, we still have the same core middleware as in the previous section, but the MauiApp.MauiAppBuilder pipeline now contains additional middleware.
Note:
For now, these are the configuration we can work with. The MauiAppBuilder pipeline is limited at the moment but I hope can be more interesting in the near future.
Summary
In tIn this post we saw how the new MauiApp builds the middleware pipeline for your application. We started by looking at the constructor of the MauiApp, to g
For each pipeline, I showed the code you write and the resulting middleware pipeline. Overall, this can simplify middleware ordering issues, but it’s good to be aware of what’s happening.
And that’s all for now. If you still don’t follow me on social media, you can do it on Twitter or Linkedin. It will be great to see you over there.