[SOLVED] Akka.NET actor system in ASP.NET

Issue

I created a service with a RESTful API in ASP.NET, hosted in IIS. Inside this service, I would like to create an actor system with Akka.NET.

Upon creating the actor system:

var actorSystem = ActorSystem.Create("myActorSystem");

The following exception is thrown:

A first chance exception of type ‘System.InvalidOperationException’ occurred in System.Web.dll
Additional information: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async=”true” %>. This exception may also indicate an attempt to call an “async void” method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

The actor system is inherently a concurrent system with asynchronous messages being exchanged between actors. As explained here, this actor system would not survive IIS taking down the AppDomain, which is probably why the aforementioned exception is thrown.

This article explains how to run background tasks in ASP.NET. However, I don’t see how I could use this for my actor system, as I have no control over the lifecycle of background tasks that might be created by Akka.NET.

Is there a way to make this work, or should I abandon the idea of having an actor system in an ASP.NET application?

EDIT: I also saw a question on Stackoverflow about implementing a REST service using Akka. Any advice about a solution similar to the Spray toolkit, but working for Akka.NET would be welcome.

Solution

Keep your ActorSystem as a shared property in some static class container – this way you may access it from the rest of your application. Actor system initialization/disposal can be done by:

  • Global.asax – use ActorSystem.Create(...) inside Global.asax Application_Start and dispose it with system.Shutdown() on Application_End.
  • OWIN – create actor system in OWIN’s Startup.Configuration method and shut it down by binding to host.OnAppDisposing event (how-to link).

Remember that IIS will startup your web app only after first request and tear it down automatically after some time when it’s idle. Therefore make sure, that your deployment script will ping application after publishing and set idle timeout (link) for long enough if you want your Akka actor system to run continuously.

Second option

Separate your Actor System logic and deploy it, for example, as a Windows Service (or Linux deamon). Turn on Akka.Remoting for it and create a proxy client, which will forward all application long-running sensitive tasks to external service. Similar solution is often used for things such as schedulers or event buses, when your application logic must be working continuously.

Answered By – Bartosz Sypytkowski

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *