An introduction to MassTransit using RabbitMQ (In ASP.NET Core)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dot net code central in today's video i am going to discuss about mass transit and how to use mass transit with asp.net core before we start getting into the code let's first discuss what is mass transit mass transit is an open source framework for building distributed applications in dotnet and the key features of mass transit are number one it has an out-of-box integration available for revit mq activemq azure service bus and amazon sqs as well as sns it supports message patterns such as event driven state machines sagas message consumers as well as distributed transactions now we will go into each of this feature in today's as well as subsequent videos because there is a lot of grounds to cover here and will not be possible to cover everything in a single video so in today's video i'm just going to cover the message consumer and how we can use the message consumer button for communicating across multiple microservices using mass transit through rabbitmq and in the subsequent video i'm going to talk about distributed transaction and sagas and then the next important feature it supports is in memory test harness which makes it very easy to implement unit as well as integration test and the other key feature it supports is advanced monitoring such as distributed tracing and service health checks first let's start with building asp.net core application using mass transit now i'm going to take two step approach for building the application first i'll build a asp.net core application where i am not going to use the extension methods that are provided by mass transit instead i am just going to use the basic service bus and the factory to show how the internal works because when we get and start using extension methods this all will be hidden i think it is important to understand the basic apis and then i am going to use the masstransit.asp.net nuget package and use the dependency injection extension methods to use mass transit so first let's start by creating a new asp.net core web application and i'm going to name this application as order service this is a service which ultimately you will be responsible for sending order through rabbitmq on the other side there will be inventory service which will be listening to the message and update the inventory based on the order first let's create the order service i'm going to select api and sp.net code 3.1 just go ahead and create this once the project is created i'm going to add the new get package for mass transit and as i mentioned earlier initially i'm not going to use the masstransit.asp.net core nuget package i'm just going to go with the mass transit base nuget package and of course i will also have to install the mass transit dot rebit mq nuget package so after i install the mass transit i am going to install the mass transit dot return q nuget package this is the nuget package we will need for accessing rabbitmq as i mentioned initially i'll just go ahead and create a base implementation in the configure method of the startup after all the configuration is done i'm going to use the bone implementation of mass transit because as i mentioned i'm not going to use the extension method initially and for the rapid mq i'm going to use the existing revit mq that i installed in one of my previous video and if you have not watched the video i'm going to share the link in the description i encourage you watch that because there i showed how i installed it using a docker it's very easy and straightforward to do that so currently it has only the default exchange and cues are all empty i'm going to go back to the order service and now i'm going to create the bus control provided by mass transit for that i'm going to declare a variable called pass equal to bus dot factory dot create using revit mq so here i am saying that i want to create a bus using revit mq as my broker and here i'm going to pass the config inside of the config the first thing we are going to do is we are going to declare the host for the revit mq so here i am going to pass the url of the revit mq that i have installed so that's the url and then after the host is configured what i'm going to do is i'm just going to declare a listener so i'm going to say config dot receive endpoint and the first parameter of the listener is the queue name so i'm going to create a temporary queue and then next is again uh action and for this action i'm going to pass c and here what i can do is i can do handle and for handle i have to pass a message what kind of message i want to handle and as you can see it's a properly defined type and this type is very critical and i'm going to explain why once we run this application it will be very clear for the time being i'm just going to go ahead and create a new class and it is also important that this model that we are going to use is available in a shared library and i'm going to explain why that as well and it will be clear when we go through running this application why it has to be a shared library i'm just going to name it as model and for this class i'm going to name it as order next i'm going to add this model as a dependency to this project and then i'll go back to the startup i'll use order here and then after we get that we'll get a context which is going to be returned when a message is received from the evidence queue this particular lambda expects a return type of task so we can do return console dot out dot write line sync and here we're going to do context dot message which is going to give us order dot name and i'm going to create a property called name in the order class so here we created the bus we created the host associated to the bus and created a received endpoint which is the handler which will be triggered when a message is received in this queue now what i'm going to do is i'm going to start the bus so i can do pass dot start ideally we should be using start async but since this is inside of the configure method for just for the demonstration i'm not going to use star testing and again this is not the code we are going to use we are going to use the extension method where everything is implemented properly this is just to get through the basic and understand how the message and the queue plays an important role and how it all tied up in revit mq by mass transit so we're going to do start and then here itself we can do bus dot publish and we can publish a message and for the message i'm going to again use new order name is equal to test name and that's about it i'm not going to stop the bus so i'm going to run this application now now when i run this application i can see the test name showing up here and now if i go back to the revit mq i'll see that there is a new exchange created with the name of them.q there's a new queue created with the name of tem dash queue and you can see the consumer is connected to this and it got a message and apart from the temp dash queue you can see that model column order is another exchange created and both of them are fanout exchanges and model column order is basically derived the name is derived from the type that i have given and if we go into the temp dash queue we can see that the model.order exchange is sending message to this exchange and then it is going to the damn dash queue queue so as you can understand the significance of this is that whatever object we use for handling or for publishing that becomes the top level exchange and that exchange then routes message into the exchange which is the default exchange created for the queue when we use the handle so here essentially what is happening is temp hq exchange and temp dash queue queues both are created and they are attached to each other and then based on order the model dot order exchange is getting created and anything which is coming from message.order will be routed to damdashq exchange and that binding is happening here because during the publish we are not specifying any exchange or anything we are just specifying what object it has to go so when we do that it will default get into the revit mq figure out is there an exchange with the model.order if not it will create one it will publish the message there so on the other side if no one is listening to this order the message will go into ether so that how it works now i'm going to get rid of all this code as you can see why model is so important this order object and why it has to be outside because it has to be used by both receiver as well as publisher so it makes sense keeping it as a separate model and we should ideally be creating a new get package out of it and using the nuget package in both publisher as well as receiver now let me get rid of this code because we are not going to use it and now i'm going to make this order service as a publisher alone but i need to add a couple of namespaces before i do that so the first namespace i'm going to add is mass transit dot extension dot dependency injection and the second namespace that i'm going to use is mass transit dot asp.net core once both these name spaces are installed what i'm going to do is i'm going to go into the configure services method and here i'm going to say services dot add mass transit and then i'm going to take the config action and here i'm going to configure the mass transit and just like before i can do config dot use revit mq and this time i'm going to get an action which has a context and a config and here we can say config dot host and we are going to pass the url of the host for the rabbitmq just as before and after that what we are going to do is we are going to say services dot add mass transit hosted service now as you can see it adds a mass transit i hosted service which includes a bus and endpoint health check once we add this mass transit hosted service it takes care of creating the bus and managing it we don't have to do anything so these are the only couple of lines of code we need to configure mass transit with the revit mq and that's about it and next what we'll do is we'll create a order controller and we're going to create an api read write controller and we're going to name it as order controller and here i'm going to create a constructor in the constructor what we can do is we can use i publish endpoint interface from mass transit which is going to help us publishing a message into the rabbitmq and on post i can use order object here and i can do publish endpoint dot publish order and here i can say order that's about it and instead of uh i can use sync task of my action result and here i can do an evade and then return okay i'm not doing any exception handling right now but we should be using a try catch block and handle the exception appropriately so this will publish a message to the order exchange but right now it just has the exchange we have not done any other thing i'm just going to get rid of all these exchanges so that we can start fresh and this cube as well okay so we are back to the beginning so now this controller will be used for publishing a message next what i'll do is i'll create the subscriber controller so for the subscriber controller i'm going to create a new asp.net core application and as i mentioned earlier this will be an inventory service and i'm going to create an api just like the other one and once the project is created i'm going to go ahead and add all the necessary nuget packages for mass transit so i'm going to install the mass transit and then i'm going to go ahead and install mass transit dot revit mq and then the mass transit dot extension dot dependency injection which is necessary for configuring the di and then finally i'm going to go ahead and install mass transit dot asp.net core so once all these three are installed i'm going to pretty much copy paste the code from the previous service but i'm going to make some changes here so this part of the code will remain the same just going to add the necessary name spaces now use revit mq part and configuring host part is going to remain same but remember like just like the last time we have to create the receive endpoint here so we'll do config dot receive endpoint and for the receive endpoint we are going to provide a name of the queue and this time we're going to probably provide a better name so let's say order order queue and then just like before we'll take the lambda but this time we are not going to use an inline method because the goal is not to do all the logic here right we are going to create a class which does this and this is where the consumer pattern comes into play so we can do configuration dot configure consumer and we can provide the consumer that we want to handle this message and we are going to name it as order consumer and then we are going to pass the context that is available at the top level i'm just going to create this order consumer in a new file and the other thing we have to do is up top we have to say config dot add consumer because we have to register the consumer order consumer now we're getting read squiggly because it does not implement the interface so has to implement the interface i consumer and we need the order model so for the time being i'm just going to add the existing project for the model but as i mentioned earlier this should be done through new get package let me add a project reference for the model and let me use order here and let's implement the interface and as you can see the interface comes with a single method and now here i can do the same thing i can say console.out.writelinesync and i can take the context dot message dot name that's all i'm going to use and i can use sync here so this should do now our startup is all configured so here what we have done is apart from configuring the rebidem queue we have added a consumer and then we configured the consumer when we attached an endpoint so now what is going to happen is it's going to create an order queue exchange and an order queue message and then it will create the model.order exchange which will be connected to the order queue exchange and the message will flow through that pattern so now if i run this application let me change the port number otherwise both of them will be competing for the same port okay and now let me run this application now one thing to keep in mind is the consumer is added as scoped by the mass transit so if we are using any dependency inside of the consumer we should also be using scoped unless we really want it to be a singleton and now if we go back to the debit mq we can see that the model.order fanout exchange is created which is sending message to the order queue exchange and order queue exchange is sending the message to the order queue so this is how the whole pipeline is created we are going to run the order service and now we are going to open up postman where we are going to make a post request and i have set the content type as application slash json and in the body we can pass the name hello mass transit let's open up the inventory service here and now if we send a message we should see the inventory service got the message hello mass transit and it is working as expected now one thing we can do here is in the inventory service we can introduce a dependency in the order consumer just to show that the dependency injection will work as expected and the easiest dependency to add is going to be the i logger because i don't have to create any class or anything so we can just create a logger define the logger and here we can say logger slash log information and at this point if we want we can get rid of the control dot right line but i'm just going to keep it so that it's easier that way otherwise we have to return a task.completed task so i'm just going to keep it as this and i'm going to run and this time we should see the console.writeline as well as the logging message if i go here just say hello must runs it again send a message and come here we can see got new message hello mass transit again and before that we got the hello mass transit again which is the console.writeline so it is working as expected so this will give you a basic understanding of how we can use mass transit for communicating through revit mq across multiple microservices using a consumer pattern and as you can see it is very straightforward the key to understand that the mapping of the exchanges is that whatever model you are going to use that is the base level fan out exchange which will be created which will be pumping the message into the other exchange that you are expecting it to be so it is critical that the object or the namespace at least is shared across both consumer and producer and that's the key so that is all i wanted to cover today in my next video i'll try to cover some of the advanced topic like saga and distributor transaction if you like this video please give me a thumbs up and if you are new to my channel and if you think you are getting value out of my channel please subscribe to my channel thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 43,908
Rating: undefined out of 5
Keywords: masstransit, masstransit rabbitmq, masstransit asp.net core, masstransit rabbitmq asp.net core, asp.net core masstransit rabbitmq, asp.net core masstransit
Id: 1IF4uu0ptk4
Channel Id: undefined
Length: 22min 10sec (1330 seconds)
Published: Mon Oct 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.