Nikola Schou
Nikola Schou

Reputation: 2476

AKKA.NET actor system not shut down correctly in asp.net core application

I'm running my AKKA.NET actor system inside my asp.net core application (framework net46). I run the web-application from a command prompt by running 'dotnet run' and when I try to exit it using ctrl-c I get the following output and the prompt hangs there:

                c:\D3\>19:05:33.7024266 ENV=d0 Sdm.Web DEBUG Akka.Actor.Internal.ActorSystemImpl  Disposing system
            19:05:33.7084292 ENV=d0 Sdm.Web DEBUG Akka.Actor.Internal.ActorSystemImpl  System shutdown initiated
            [DEBUG][12/09/16 19:05:33][Thread 0023][EventStream] subscribing [akka://all-systems/] to channel Akka.Event.Debug
            19:05:33.7134423 ENV=d0 Sdm.Web DEBUG Akka.Actor.GuardianActor  Stopping
            [DEBUG][12/09/16 19:05:33][Thread 0023][EventStream] subscribing [akka://all-systems/] to channel Akka.Event.Info
            [DEBUG][12/09/16 19:05:33][Thread 0023][EventStream] subscribing [akka://all-systems/] to channel Akka.Event.Warning
            [DEBUG][12/09/16 19:05:33][Thread 0023][EventStream] subscribing [akka://all-systems/] to channel Akka.Event.Error

If I hit another ctrl-c it exits. But it doesn't seem right.

I'm using a dependency injection container to manage the life time of the actor system (singleton-scope) so I assume the container is actually trying to dispose the object at application shutdown.

Can anybody suggest what might be wrong?

Upvotes: 0

Views: 768

Answers (1)

Set
Set

Reputation: 49789

You need to directly stopping Akka.NET Actors if you want a graceful shutdown. To do so use application shutdown events provided by ASP.NET Core via IApplicationLifetime interface:

CancellationToken ApplicationStopping { get; }
CancellationToken ApplicationStopped { get; }

Obtaining an instance of IApplicationLifetime can be done during Startup.cs in the Configure method:

protected void DisposeResources()
{
    //Cleanup stuff when the app is shutting down
}

public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
{
    //register the application shutdown handler
    applicationLifetime.ApplicationStopping.Register(DisposeResources);
}

For more details see article ASP.NET Core application shutdown events and this SO question Kestrel shutdown function in Startup.cs in ASP.NET Core:

Upvotes: 3

Related Questions