Publishing and consuming messages in Azure Service Bus using .NET Core | Azure Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody manikin let's meet I'm gonna show you how you can start consuming and publishing service bust messages in queues and topics in dotnet core if you don't know what service buses and you want to learn more about it you can check my previous video by clicking on the top right corner of your screen right now just so you can catch up with something that you might not know all the codes for this video will be found description down below this video is part of my azrou series if you don't wanna miss any episode please subscribe the solidification well to get notified when I upload a new video so first things first let's take a look at the application that I have here in fact there are two applications here we have a service bus consumer and a service bus producer as the name implies this consumer one is supposed to consume the messages we'll publish in a cure topic and the producer is supposed to create them and if we take a deeper look we have a messaging controller which only tries to do is imitate realistic behavior so we have a request for example to create a new customer and then a service would create a customer in the database and then we might be publishing that message so somebody can listen to it and do something with it and we have the same thing for orders so we can create the order for like a product or something it doesn't really matter but just imitates the behavior and then we publish again into a queue or a topic and I'm going to show you how you can do both in this video and then on the consumer side of things I have a just a background service it's not an API it's nothing this is a console app and even here we're gonna put the code that consumes only the customers even though we're gonna have orders as well so that's what we're gonna be focusing on in the in the message control again we don't actually have an implementation for this interface the iMessage publisher so we're gonna write this together but before we can actually write any code we have to go to as read create the entities that we need so first I have a resource group in north Europe and I'm gonna click Add here and I'm gonna type service bus so service bus it is we're gonna select that one I'm gonna say create then I'm gonna give it a name Nick YouTube Eun that's probably not taken yes and then we're gonna select this standard pricing I can take here and show you the reason why I go with a standard is be and the topics to show you both depending on your use case you might not need topic so you might need topics I'm gonna explain here why we're doing topics but I also explained that in the previous video so you can watch that as well in more detail and then you can see that it can be very cheap or very expensive depending on your use case so let's just close that and I'm gonna keep everything as default and select north Europe and that's it I'm gonna say create I'm going to wait until this finish is deploying and that was successfully successfully created so if we go back and refresh here we go and I can click into that and I'm gonna show you around so the first thing you might see is the shared access policies and this is where we're gonna get our connection string into the top-level entity now fusion topics can also have the role and shared access policies and you can specify that you want something to be just for a listener or just for a sender or also have manage permissions so you can go very granular in how you want to allow users to interact with this I'm not gonna touch upon that on this video security might be coming in a different video for now we're gonna focus on the functions I think so publishing and consuming and the first thing I'm gonna do is I'm gonna create a queue and I'm gonna name this just example queue I'm not gonna change anything I explained some of these in the previous video so you can watch that we don't need any of them and the queue size can stay as one gigabyte this is only for the messages that we currently have alive into the queue so it should be enough and that was created so as you can see here we have nothing in here just an empty queue and what are we gonna do is we're gonna start publishing into that queue now I'm gonna go to the service bus here and the connection string because I'm gonna need that in a second that's where you're gonna get it from this one the primary one but for now let's go back to the code and actually implement the message publisher so this doesn't have an implementation so I'm gonna expand the services folder and I'm gonna say add class message publisher and this is gonna implement the ayahs publisher interface and we're gonna implement the missing members one of them is just taking a string and publishes that string into the cue of the topic and the other one is taking an object we're gonna see realize that - a string and then we can publish it into the queue because the queue is accepting messages before I can actually publish anything though I need to include the nougat package and that nougat package is the Microsoft Azure service persnickety packets latest version will do so let's go ahead and include that into a producer project so let's close that and now we have access into an interface called private read-only I cue client and this new client is what we're gonna use in order to publish send messages into a queue and the code would look something like this let's implement the string one because the string will be easier ultimately we need a message and that message will be a new message and this message class is coming from that package that we just added and as you can see it's accepting a byte array as the body it can also accept many other things but what we care about in this scenario is just the body so what I'm gonna do is I'm gonna say encoding utf-8 dot get bytes and I'm gonna get the raw text and I'm gonna get a message out of that raw text and this will be a JSON string basically and then all I'm going to do is return cue client dot send async and the message and that's it I don't need to do anything else the rest will be done by the SDK similarly enough I'm just gonna copy that first part and we obviously don't have a row but what we do have is the object as text and we're gonna say JSON convert dot serialize object and then the object that we get I'm gonna use the object as text here and this is enough for us to publish now you might be thinking why am I not awaiting this and it will be awaited when I automatically await this method so don't worry about this it's just that it's not really needed in this scenario so now we had an implementation for this i q-- client let's go to the startup dot CS and under our out controllers I'm gonna say services dot add singleton IQ client and then the implementation will look something like this new queue client and we're gonna get the connection string so this will be connection string and then let me make this new line so you can actually see what's going on so connection string and entity path and the entity path will be basically the cue name so let's put those in configuration so we can retrieve them so let's make an object service bus and then first connection string and we're gonna place the connection string here and the other one is the two name now we're going to get the cue name so let's go back here this is the primary connection string we're going to copy that into the clipboard and we're gonna paste it here and that's it and then we're gonna go and get the cue name oops you don't want to show you that yet and the cue name what I named this example cue yes example Q so let's copy that and paste it here and now we're gonna need to get them from the configuration so we say configuration dot get value string I could make a class and map it to the class but I am lazy so I'm not gonna do that but you can and then I'm also gonna copy at the actual value here connection string correct and then let's new line this basically copy the whole thing and say you name here and that's it now we have our new client configured in dependency injection and we can inject that as an interface and it will be used by our messaging controller to publish messages into that queue so let's see that in action I'm going to go ahead and debug the producer and assuming I did everything right oh I don't think I did because I never registered the I'm message publisher so I also need to message that to register that as well so services dot add singleton and these need to be signaled by the way there's no point in having them transient or scoped and just do message publisher and that's it and now we can just simply run this and let's test it down let's see if it works so this is running when I go to postman and I'm gonna publish text here and I'm gonna create a jason body it's gonna be just prop and then the value will be value yeah I know so this is running it says okay let's see the code no exception so it should have been published let's go back to the queue and click on the queue as you can see the active message count is now one since nobody's consuming it the message is in the queue now how can we take a look just to make sure that it is what it should be well I'm personally using a package called auto service bus Explorer which is an open-source package in github that you can use as well and all I need for that I'm gonna show you exactly what is all I need is the connection string so I can connect to the namespace so let's copy that and this is what it looks like and I'm gonna do connect and I'm gonna select enter connection string and paste the connection string and I'm gonna say ok and now it should connect as you can see I can see my accusin later my topics and this is the cute we have it this one is the active message count so all I need to do now is click messages and say peek if you say receive and delete it's gonna get the message and delete it you don't want to do that if you just want to see what it looks like so you're gonna say peek and as you can see this is the body of the message and these are all the system properties of the message so I can see that it's been published successfully and somebody will eventually consume this now I don't really want this here but this is how you publish something into the queue now the problem with having these queue here in this specific scenario is that if I keep using the queue to publish everything I can actually go to postman and publish a customer created event or message sorry and this has been published and then no the created message and this has also been published and if I go back and i refresh as you can see I have three active messages and if I again go to messages and say receive big I can see all of them here the problem with that is that anybody consuming that will get all three but they might only be interested in customers created this is where the topic comes in and the subscription so I'm not going to spend any more time with the queues it's very straightforward the way you publish into Q is the same way as you publish in a topic and the same way you consume from a queue and the topic but it's the concept and the idea that you want to show you and I also want to show you filters so I'm gonna skip over the consumption of the queues assume that everything I show on topic consumption is exactly the same as the to consumption well except for the subscription but let's forget about that so what we can do here now is just purge this queue because we don't want any messages to be here anymore and what I'm gonna do now is I'm gonna create a topic and for the rest of the video I'm gonna use a topic and the idea of a topic is that you can have a centralized location where you publish messages as a producer and then people can create their own subscriptions and only get notified on messages that they are interested in or even all of them if they're interested in all of them so that's the main idea I'm going to name this example topic again I'm gonna use the default and as you can see how subscription counts 0 but now I'm gonna create two subscriptions here please note that if you just start publishing into topic without subscriptions the messages will be automatically deleted so don't like that mistake I'm I didn't know what was happening in the beginning and now we win a couple of subscriptions at first I'm gonna say orders oh basically just name it orders really it does really matter but you can have the team name that interested in this subscription or who created this this up to you and the max delivery count is how many times the message will be resent to the consumer before it's dead lettered which basically means that it won't be pushed anymore and you have to manually go in and see what's happening so we're gonna set this to five it doesn't really matter for this video and we don't need anything else here any configuration here is up to you and your use case just gonna say that orders subscription is now being created and I'm also going to create another one called customers and again let's just set this to five so both subscriptions have been created let's go back to our code instead of using a queue name now we are going to use a topic name and the topic name is example topic and the producer whoever creates the messages doesn't know anything about the subscriptions all they know is that publishing into a centralized location for anybody to consume this means that the code looks very very similar in fact if I copy this topic name thing and I go to the startup and I change the IQ client to an AI topic client and then the topic client implementation then all I need to do is change the cue name to topic name I should have stopped this pathway the topic name and this will still be working completely fine obviously I need to change this to a topic client but and obviously rename this because this is not longer queue client it's a topic client yep let's rename that yep okay but the rest of the code is exactly the same because as far as this is aware it's just another entity that it's publishing to it's the consumption part that's the interesting part and that's it from here now let me just run this as it is right now and show you what's happening when I publish something into a queue let's say I have a new customer manage capsules and I create that that's perfect customer created and now let's see that the order has also been created so both of them should have gone into the topic and if i refresh this here I should be able to see two subscriptions correct and then they both have the exact same messages so both messages were published to both subscriptions because the topic will essentially forward the messages to every single subscription however if you're only interested in some of them it's up to you in the subscription level to filter them and filtering is very important in a very useful tool essentially what's happening and let me go to the interface is you can go into the customers for example the customer subscriptions and say yeah here you can see the filters you can say odd filter and now you can have either a sequel filter or a correlation filter the difference is that this is using a language like sequel so you can say something like a property the message can be used so you could say prop equals value well we probably need single quotes here but yeah prop equals value and property is a property in your object or you can use system properties so correlation at the content type label message to reply to any early era and then put the value here or custom properties and this is what I'm gonna use what I'm going to say here is I'm gonna say that the customer subscriptions is only interested in message type customer created and this in my code is the name of the contract I'm publishing into the queue or the topic so this is what it is here and I'm gonna save the changes and then orders have the order created so I'm gonna copy that go back to Azure of course I need the name for my filter so I'm gonna say only customers and save the changes and I'm gonna go to the orders and I'm gonna say add the filter by default it's accepting everything by the way obviously and then I'm gonna say orders only correlation filter message type and when I said the value is order created and I'm gonna save that now obviously how am i setting this user property because if I just keep publishing now that will get notified for both I'm not doing any filtering currently because there is no filter in my object or the message that I'm publishing let me go back and just purge both of these subscriptions so now they have no messages within them and now in the code I'm gonna go to the publisher and in both scenarios I'm gonna say under my message you just stop this I'm gonna say message dot and there's a property call these user properties and I'm gonna set the message type to be the type of the t object that this is accepting the generic dot name and this is the name of my contract and because the contract everything in this should be version so it should not really break compatibility or it should be always forward compatible if you keep adding properties but never break well if you contract my negotiation within your company or your team is done correctly and then for just publishing string I'm just gonna set this to raw now this will fail if you have a class called row but why would you do that that way did you get the point so now if ID bug this I can go to postman and create again my new customer and my new order and now if I go to this app and I refresh each one of them has one message customers only have one message and if we take a look it will be the customer created message and then the orders one if we take a pic it only has the odd one so on the topic level we only sent what the subscriber is interested into this is just great functional it's me I love it I really like it and it really makes our life way way easier when we're dealing with these scenarios now this is fine on the publishing side but how are we consuming we're gonna show you how we consuming now in this bottle consumer I'm gonna again add the same nougat package because it's not there and again the SDK team has done a great job it's very easy to consume as well now we're using the background service of dot Nicole and in case you don't know this will just keep running in the background of your application so you just spin up a console app and these keeps consuming constantly as messages are coming in and all I need to do to consume is I need again a private read only this time and I subscription client and I'm gonna inject it from dependency injection so this is testable and then what I'm gonna say is we're going to keep that completed task here I'm gonna say subscription dot client subscription client dot register message Handler and then I'm gonna use this lambda and you can say in here basically in here this will be called every time you have a new message so this message right here this contains everything your consumer needs so since this is only going to be consuming the customer events I can safely visualize this into the object that I want and this in order to do that all I need to do is say customer created equals and coding dot utf-8 dot get string from the message dot body and then I also need to do JSON convert dot and of course I need to import that dot deserialize object from that string and the object will be a object type since this is a contract I can use it so it's customer created how is it not included let's just include it here we go and now here you have your actual object because this is a contract we can reuse it and you can do whatever you want looking at this I'm gonna say console dot write line and we're gonna just log something in the console but here's where you're gonna do something with your database potentially or any logging and we're gonna say new customer with name and then we can say customer created dot full name and that's it really we can also say end ID and I'm gonna do that so we can see the different messages coming with different IDs and they just say return task completed async of course nothing stops you from doing acing here and I'm waiting in here if you need to do so again this is up to you up to your use case now the reason why this complaining is because we also need another thing here and that's the a missus hundred options and we also need specify how we're going to handle exceptions all I'm gonna do here is a task completed task because I'm not gonna handle exceptions in this scenario with any complex way but if you need to do that you could just extract that and say args dot exception or exception receive context and do something fancy with it but I don't need to do it and then in terms of other settings in these options you can say autocomplete and I guess it falls because I'm only one I autocomplete when I tell them that it's completed and I'm also going to say max concurrent calls one and now since I talked about this sort of completing thing I'm not gonna return task completed here because if I do that the message is not actually completed what I'm gonna do is we're gonna say subscription client dot complete async and manually complete this message and I'm gonna do that by saying message dot system properties dot lock tokens so I'm gonna use the lock token the lock token is used to essentially tell the client and the server that this is being currently locked by a single consumer who is consuming this so don't send it to anybody else yet else we're gonna have duplicate processing so that is purpose but we only but we also use it to complete the message manually and really that's it here there's nothing else I need to actually show you now let's go ahead and register this I subscription client in the startup table CS and in here I'm going to do is say services dot add singleton I subscription client and in a very similar fashion we're gonna have a bunch of settings a new subscription client and these needs three things is a connection string topic path and subscription name I'm gonna be lace again and copy that and then just rename a few things so we're gonna paste that here in the settings of the consumer and this is no longer queue name same connection string and now subscription name and this was customers and then in the startup we're gonna copy one and then copy the others so connection string and this is topic name not M coma and a subscription name which I'm gonna copy because I won't type here you go so that's all in your consumer as well this will now be a long-running service that's gonna keep consuming these messages out there coming in let me just run this consumer in the background so I'm gonna start debugging this so this is now listening and as you can see it actually got notified with the message that we published before and if i refresh now it's no longer grabbed either and let's run the producer as well and let's make some messages and let's see that it only actually consumes the customers not the orders so go here I'm gonna make this smaller so you can see them being consumed in real time essentially and you can see I'm creating a few orders nothing is being written here but if I go to the customer and I create a few customers you can see that the customer or students John as you can see all these messages are coming in as the being published so this is how you create a long-running consumer and a producer as well that's all I have for you for today thank you very much for watching special thanks to all my github sponsors for making these as possible if you wanna support me as really gonna find the negative description down below leave a like if you like this video subscribe for more content like this ring the bell as well together defy the one uploading your video and I'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 31,333
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, asp.net, .netcore, dot net, core, C#, how to code, tutorial, asp.net core, javascript, csharp, rest, rest api, development, lesson, software engineering, dev, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, asp.net core 3, pub/sub, pub sub, service bus core, azure service bus, rabbitmq, nservicebus, service bus tutorial, pub sub service bus, service bus consumer, service bus publisher, messaging, how to use service bus, azure, cloud messaging, dotnet, .net
Id: gQ5P8WVpj30
Channel Id: undefined
Length: 26min 53sec (1613 seconds)
Published: Mon Mar 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.