EventStore persistent subscriptions running in an Azure WebJob

EventStore from and on StackOverflow as get-event-store is an extremely feature rich and super fast event storing database.

If you are designing CQRS/ES in C# / Dotnet you are probably a user of Azure too. As easy as azure makes it get things going out of the box, newer things like EventStore and out of process pub/sub subscriptions can be difficult to work out.

I am not going to talk about CQRS of EventStore but instead of how to solve a particular problem with dealing with Persistent Subscriptions in Azure. The process circled in red on the diagram below.

You could use a Local Service Bus (LSB) that trigger Azure Functions but that would mean we loose the Persistent Subscriber functionality the EventStore gives us and that would be a shame.

So creating a Persistent Subscriber in a Dotnet console application is easy and fairly well documented. You can then run this anywhere you like.

Running a console application in Azure is not that easy without using VM's - I wanted to share how to get persistent subscriptions working as a continuous WebJob as it took me some time to work this out.

Find a tutorial on how to create WebJobs in VisualStudio and setup everything as required, omitting anything to do with Events or Schedules. Just get the core application going.

Do a test WebJob deployment to make sure that you are authorised to publish your project to your App Service and verify that you can see it under WebJobs.

You will need to change your App Service to Continuously Running but you should get notified of this when trying to deploy a continuation running WebJob.

The first of code straight after Main() should be

ServicePointManager.DefaultConnectionLimit = Int32.MaxValue;

Then followed by your config and these two important lines of code
var host = new JobHost(config);
//thread blocked in here- if it falls over want azure to report failure/ restart
//host.RunAndBlock(); //Do not block here!

Functions class create a method called ProcessMethod
public class Functions
public static void ProcessMethod(Microsoft.Extensions.Logging.ILogger logger)
The key part to keep the connection open is to put a while loop INSIDE the using statement for the persistent connection.
using (var conn = EventStoreConnection.Create(settings, new IPEndPoint(IP, DEFAULTPORT)))
  conn.ConnectToPersistentSubscription(STREAM, GROUP, (_,x) => { //events });
  while (true)

If you put the while loop outside the using then the connection will drop immediately. Also if any exception occurs then it will blow out of this process and back to caller,where we deliberately removed the RunAndBlock, so that we can see it has stopped or be notified as needed.

I have made a nice and clean working example putting all of these parts together.
Hope it helps you.