Blog Post

Implementing Akka into ASP.NET web application

Monday, March 6, 2017 11:37 PM

While learning about Akka.NET, I wanted to know how to implement Akka.NET into ASP.NET MVC.

Most examples do have example with SignalR, but I wanted just pure version that is written by me. I believe that this gives me the hands on experiences and also I can experience all the pain points for basic solution if there are any.

The benefit of using SignalR with actor system is to create true live responses to the user and the process can go both ways between client and the server.

 

How do we setup actor system within MVC Application.

You can also download the example from my repository on GitHub (https://github.com/cpoDesign/WebMvcWithAkkaNet)

 

We need to go through basic steps:

  1. We start with new web application project in Visual Studio and we choose MVC.
  2. We need to install nuget package 
<package id="Akka" version="1.1.3" targetFramework="net461" />

Now we have all prerequisites we need to setup actor system with our application.

Registering the system with the application

We need to start of creating our system when the application starts and correctly close it when the application is shutting down. We can to that in our global.asax.cs.

 protected void Application_Start()
 {
     // existing code omitted for clarity
     // setup akka system
     // config automatically loads from App.config / Web.config
     ActorSystemRefs.ActorSystem = Akka.Actor.ActorSystem.Create("myactorsystem");
     SystemActors.ExampleActorRef = ActorSystemRefs.ActorSystem.ActorOf(Props.Create(() => new ExampleActor()), "exampleActor");
  }

 protected async void Application_End()
 {
     await ActorSystemRefs.ActorSystem.Terminate();

     //wait up to two seconds for a clean shutdown
     await ActorSystemRefs.ActorSystem.WhenTerminated;
 }

ActorSystemRefs: What is this?

This static class is used to associate our actor system to and also it allows us to access it anywhere in our web application 

  public class ActorSystemRefs
    {
        public static Akka.Actor.ActorSystem ActorSystem;
    }

Lets use our actor framework to return us value for action from home controller.

 

public class HomeController : Controller
{
    [HttpPost]
    public async Task<ActionResult> SendRequest(string uri)
    {
        var str = await SystemActors.ExampleActorRef.Ask<string>(uri, TimeSpan.FromSeconds(5));
        return new ContentResult() {Content = str};
    }
}

 

The implementation for this action in controller is done but, what actor I am using?

 

public class ExampleActor : ReceiveActor
{
    public ExampleActor()
    {
        ReceiveAsync<string>(async str =>
        {
            try
            {
                var result = await SayHi(str);
                Sender.Tell(result, Self);
            }
            catch (Exception e)
            {
                Sender.Tell(new Failure { Exception = e }, Self);
            }
        });
    }

    static async Task<string> SayHi(string messageString)
    {
        return $"{DateTime.Now.ToString("O")} - hi -  {messageString}";
    }
}
Blog
Products
Error pages
Links and tools