11

I am planing to use Microservice architecture for a project. The selected technology stack is .NET Core with Docker and RabbitMQ as a simple service bus and this should be able to deploy on Linux.

Lets say I have a Payment service and an Order Service, I want each of these services to expose REST endpoints. Because of that, I thought of making these two services as .NET Core Web APIs.

But the problem is the inter-service communication using RabbitMQ. Whenever I get a new order, I want to publish an event using RabbitMQ and then listen to that event in Payment service to perform certain operations (database updates). But since these are Web APIs, I don't think it's possible to listen to events as I described. (I feel like I might have to use something like a console application to subscribe to events.)

I would like to find the most viable method to achieve this using the best practices considering the scalability and extendability of the system.

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
PIKP
  • 753
  • 2
  • 15
  • 24
  • 1
    ASP.NET Core applications are console application. You can simply create an object that will listen to messages at application startup along with WebAPI registration.. – Andrii Litvinov Apr 19 '17 at 08:19
  • @AndriiLitvinov, Thank you for the response. Could you please explain a bit your suggestion? – PIKP Apr 19 '17 at 08:31

1 Answers1

11

When you create your application with .NET Core you bootstrap things in Main method and then register services and middle-ware in Startup class. So before you start web host you can also create and start your messaging services:

public class MessageListener
{
    public void Start()
    {
        // Listen to rabbit QM and all the things.
    }
}

public static void Main(string[] args)
{
    // Listen to messages.
    var messaging =  MessageListener();
    messaging.Start();

    // Configure web host and start it.
    var host = new WebHostBuilder()
        ...
        .Build();
    host.Run();
}

UPDATE:

In ASP.NET Core 2.0 IHostedService interface was introduced to achieve same goal and they mention this scenario in their announcement. Here is an example of how to implement one.

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • So what you are saying is, even in a .NET Core Web API project, I can simply register my listeners to handle event that will be published by other services? – PIKP Apr 19 '17 at 08:49
  • Yup, exactly that. If the logic of the subdomain is implemented in a service it does not matter how commands are send: through web api or through message queue. – Andrii Litvinov Apr 19 '17 at 08:50
  • Thanks, do you think it's a good idea to handle it like this? will there be any drawbacks? – PIKP Apr 19 '17 at 09:11
  • 2
    That's how I do it in my projects. The idea of microservice is to implemented a Bounded Context (a subdomain or part of it). It should have a domain model inside, which can be accessed either from web api controllers or from message handlers. – Andrii Litvinov Apr 19 '17 at 09:15
  • @PIKP, please check the updated if that's still relevant for you. – Andrii Litvinov Sep 03 '17 at 08:50