Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MAUI support #293

Merged
merged 14 commits into from
May 17, 2023
Merged

Add MAUI support #293

merged 14 commits into from
May 17, 2023

Conversation

Arkatufus
Copy link
Contributor

@Arkatufus Arkatufus commented May 4, 2023

Fixes #289

Changes

  • Add partial MAUI support

NOTES

  • We will never fully support MAUI officially, i.e. we will not bundle/include MAUI SDK into Akka.Hosting
  • This will be a discussion thread for MAUI support, we will strip the MAUI sample code on the actual merge-able PR (non-compilable by CI/CD, missing MAUI SDK).

private readonly ILogger<AkkaHostedService> _logger;

public AkkaHostedService(AkkaConfigurationBuilder configurationBuilder, IServiceProvider serviceProvider,
ILogger<AkkaHostedService> logger, IHostApplicationLifetime applicationLifetime)
ILogger<AkkaHostedService> logger, IHostApplicationLifetime? applicationLifetime)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAUI does not support IHostApplicationLife, we need to pass in null and disable termination hook.

services.AddHostedService<AkkaHostedService>();
if (Util.IsRunningInMaui)
{
services.AddSingleton<AkkaHostedService>(provider =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAUI does not support hosting, hosted services had to be registered as a singleton service.

{
public static class AkkaMauiSupport
{
public static async Task StartAkka(IServiceProvider provider, CancellationToken token = default)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Akka hosted service have to be started manually by users, AkkaHostedService is internal, we have to use these wrapper methods to let user call the service StartAsync() method

{
get
{
_runningInMaui ??= AppDomain.CurrentDomain.GetAssemblies().Any(asm => asm.GetName().Name.StartsWith("Microsoft.Maui"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAUI runtime detection

registry.Register<ClickActor>(echo);
});
})
.AddTransient<IMauiInitializeService, AkkaInitializer>()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Register a IMauiInitializeService to start Akka hosted service.
This is the only way to start a service automatically during start-up in MAUI.

{
public void Initialize(IServiceProvider services)
{
AkkaMauiSupport.StartAkka(services).GetAwaiter().GetResult();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Akka hosted service starter, we call the wrapped service here.

Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds MAUI detection, but all of the actual code for making Akka.Hosting workable in MAUI will be located in its own repository here: https://github.com/akkadotnet/Akka.Hosting.Maui

@Aaronontheweb Aaronontheweb enabled auto-merge (squash) May 17, 2023 16:13
@Aaronontheweb Aaronontheweb merged commit 92708e7 into akkadotnet:dev May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MAUI support for Akka.Hosting (disable IHostedService)
2 participants