- Go to Solution and right click on it select Add and select New Project.
- Select Console App (.Net Core) and click on Next.
- Give a name to the service and click on Create.
- Create a new file called appsettings.json & add the Azure service connection string as below.
- First, we have to install the Azure Service Bus NuGet package.
- So this service needs to listen to messages from Azure Service bus we can create this as a Windows service. Along with that add the below code to read to the Appsettings from appsettings.json. So create a class called AppSettings.cs as mentioned below.
- To configure our service first we need to install the below packages.
- After that add the below code in `program. cs` to configure our service.
- Now the next part is to create Background service to listen to messages. So create a class called CraeteOrderHandler.cs and inherit it with class `BackgroundService`. Now override the `ExecuteAsync` and `StopAsync` method of BackgroundService class.
- ExecuteAsync() method calls when service starts so we have setup a queue client object and also registered Handle method to listen to messages.
- In StopAsync() method we close the queue client connection.
- In Handle() method we just extract body from messasge which contains our actual data and just print it in console.
- After that let's register our hosted service by adding below line into `ConfigureServices` method in Program.cs class.
- So let's build and run the service to listen to messages. Start order web api and send message to queue which our OrderService going to listen to.
No comments:
Post a Comment