Skip to content

akkadotnet/Akka.Hosting.Maui

Repository files navigation

Akka.Hosting.Maui

HOCON-less configuration, application lifecycle management, ActorSystem startup, and actor instantiation for Akka.NET. This is an Akka.Hosting adapter for .NET Maui applications.

The reason this exists in the first place is because Microsoft.Extensions.Hosting aren't supported on .NET Maui and there are no plans to support it. We've adapted Akka.Hosting to run using a Maui IMauiInitializeService instead an IHostedService behind the scenes - and this should make it trivially easy to use Akka.Hosting's 'ActorRegistry' to inject actors into your UI elements and background services.

Installation

First, you need to install the Akka.Hosting.Maui package into your Maui application:

dotnet add package Akka.Hosting.Maui

Next, inside your MauiProgram.cs (or equivalent) call the AddAkkaMaui method:

using Akka.Hosting.Maui;
using Akka.Hosting.MauiSample;
using Microsoft.Extensions.Logging;

namespace Akka.Hosting.MauiDemo;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if DEBUG
        builder.Logging.AddDebug();
#endif
        
        builder.Services
            .AddAkkaMaui("TestSys", config =>
            {
                config.WithActors((system, registry) =>
                {
                    var echo = system.ActorOf(ClickActor.Props);
                    registry.Register<ClickActor>(echo);
                });
            })
            .AddTransient<MainPage>();

        return builder.Build();
    }
}

And from there, you should be able to use any of the other Akka.Hosting methods to configure your ActorSystem and your actors!