NOTE: A lot has changed in both the .NET and Akka.NET ecosystems since this post was originally written in 2015 and we have since released an updated blog post: “Best Practices for Integrating Akka.NET with ASP.NET Core and SignalR”.
Lots of folks have been asking about Akka.NET and ASP.NET MVC integration on StackOverflow and in our Gitter chat room, so we thought it was time we created the definitive post on how to integrate these two amazing technologies together.
Note: Everything in this article also applies to Web API, Nancy, WCF, and ASP.NET WebForms.
Use Cases
So when would you want to use Akka.NET and a web framework like ASP.NET together at the same time?
Well, you might be like Joel in our “Akka.NET Goes to Wall Street” case study and need to manage concurrent reads / writes to a shared object model.
If you’re building anything resembling a chat room, web-based game, collaboration software, and more - then congratulations: managing concurrent mutations to shared state is something you’re going to have to do. Akka.NET actors are a tremendously better option than sprinkling your code with locks.
In general, what most people use Akka.NET for in the context of ASP.NET is to communicate with others network services the ASP.NET app might depend on, such as remote Windows Services via Akka.Remote and Akka.Cluster. This is especially useful if you need to do any sort of stateful web application programming, but that’s a story for a different day.
How to Start an ActorSystem
in ASP.NET
If you’ve gone through Akka.NET Bootcamp, and you should if you haven’t yet, you know how to start an ActorSystem
:
Our Akka.NET Virtual Meetup on August 12th was a huge success - we had about 100 users on the livestream with us and the recording of the meetup has been viewed several hundred times since.
One of the topics I covered during my presentation about using Akka.NET in production at MarkedUp was the concept of stateful applications built with actors - the idea that state can reside within your application rather than outside of it by default, and how it was this idea that made our marketing automation product possible to build.
This tweet from an attendee sums up the realization well:
Exactly right! Actors can be stateful - and that means we no longer have to factor round-trip times to SQL Server, Redis, Cassandra, or whatever into the design of our applications. The application already has the state it needs to do its job by the time a request arrives!
The Limitations of Stateless Design
The traditional way of developing web applications is stateless - and that’s a natural consequence of HTTP, itself an inherently stateless protocol.