This is a simple example of how to use CQRS / EventSourcing pattern with NServiceBus, WebAPI, Unity and Event Store.  The original solution was based on a design provided by Paublo Castilla, so many thanks to him. It took me sometime to understand the design and patterns use.

The purpose of this solution is to provide a fully self documenting example. I wanted to also use Unity rather than StructureMap as a DI container for no reason other than to see if it could be done.



Install the NServiceBus platform and EventSource database. Make sure the EventStore database is running in the console window wth the default name and password. 

Make sure both the AddLocation and UI projects are set to start up. This is done through ConfigurationManager where Multiple Projects can be selected to start up.

About the solution

This solution purpose is to receive GPS positions and record them to an EventStore database using the CQRS pattern. Although the solution accepts position via REST It could be modified o receive positions in a number of ways such as messages, TCP/IP sockets etc. 

ASP.NET SIGNAL/R could be used to notify the user of the event outcome. However, this is not the purpose of this article.


Putting the Location Data on the Bus

A REST POST is received. This can be from anywhere a JavaScript client, a native mobile app, mobile web site, Microsoft Band. I used POSTMAN within Google

The POST data contains the location data in JSON format and is received by an MVC WebAPI method in the LocationController which is in the UI project. Once received the data from the JSON body is decoded into class instance addLocation. This is achieved using the [FromBody] attribute in the controller method POST using Route("/api/Client") which is appended to RoutePrefix("Location") 

NServiceBus sends a message to the end point AddLocation using Bus.Send. For our example AddLocation end point is just NetServiceBus app hosted in a console windows listening for incoming messages. This project is found under the Services directory. 

So the JSON data received by the POST is assigned to a new class instance called AddLocationCommand. NServiceBus then just sends a serialised instance of AddLocationCommand in it's constructor


public void Post([FromBody] AddLocationDTO addLocation) 
            LocationTracker.WebApiApplication.Bus.Send("AddLocation"new AddLocationCommand() 
            {  Id =, 

Receiving the Location Data

So now NServiceBus host (running in ac ommand window) receives the serialised message instance called AddLocationCommand which It  then deserialises.  The NServiceBus host checks for any registered classes which implement IHandleMessages<AddLocationCommand>. That's right the type matches the class name received. in this case we have a class registered called AddLocationHandler and an instance of this gets created  and it's single method Handler(AddLocationCommand message) is called with the deserialised contents of the message received


publicclass AddLocationHandler : IHandleMessages<AddLocationCommand> 
        public AddLocationHandler() 
        publicvoid Handle(AddLocationCommand message) 



OK, that is basically it.

The next level of complexity is how we write and subscribe to the EventSource database which is handled through a generic pattern called the Composite Design Pattern in a class called EventStoreDomainRepository. This actually makes the solution for the beginner (i.e that was me) confusing but in terms of growing the number of aggregates (if you are big into DDD and I am not well not yet) easier. 

Remember the purpose of subscribing is so when an event is written to the EventStore database an action  can be taken. 

Subscriptions could be used to update Polygot databases (SQL Server etc) to achieve a status called Eventual Consistency, send messages on the bus and whole host of other things.

Subscriptions are made using the EventStore.ClientAPI and can be run in a seperate process.



I have gone through the solution and documented it as much as possible so it is easy to understand.