From the course: Azure Microservices with .NET Core for Developers

Preparing and implementing the search service - Azure Tutorial

From the course: Azure Microservices with .NET Core for Developers

Start my 1-month free trial

Preparing and implementing the search service

- [Instructor] The orders, products, and customers microservices are now implemented. Let's go ahead and build the search microservice. This microservice will be based on the API composition patterning (unintelligible). We're going to implement the query operation by invoking those three microservices, because they own their data. Sounds good. Let's get started. I'm going to create a new folder here, named Models. And inside this folder, I'm going to create a new class named SearchTerm. This is going to be used as the payload when invoking this API. So it has this customer ID property that represents the customer ID that we want to search for. Next, we're going to create this new Interfaces folder. And inside this new folder, I'm going to create the ISearchService interface. This interface will have all the abstractions for this search microservice. In this case, I'm going to implement a single method named SearchAsync that is going to ask for a customer ID. This method will return a task and inside the task we're going to find a named (unintelligible) with a couple of items. The first one is IsSuccess, and the second one of type dynamic, it's going to be the SearchResults. Now, let's go back to Solution Explorer because we need to create the SearchController class inside the Controllers folder. This class will inherit from ControllerBase, and we're going to decorate this class with the APIController attribute. Also we're going to decorate this class with the Route attribute, and the route will be api/search. Excellent. Next, let's implement this constructor because we need to inject the IsearchService reference that we're going to store inside this class inside this field named SearchService. Let's go ahead and implement the following method here, public async task of type IActionResult, named SearchAsync, and we're going to expect a SearchTerm object right here. All the clients will pass this object inside the body. Because of that, we're going to decorate this method with the HttpPost attribute. Now, let's go ahead and invoke the searchService functionality right here. And we're going to use this CustomerId property value. Let's verify if this was successful, we're going to return Ok with the search results. Otherwise, we're going to return NotFound. Excellent. Now that we've implemented the SearchController class, let's go back to Solution Explorer and let's create a new folder named Services. Because we need a concrete implementation of the ISearchService interface. So let's create the SearchService class, and let's implement ISearchService right here. And I don't want to implement this logic yet, what I want to do is to return sample data. So I'm going to wait for one millisecond and we're going to return true and some dynamic data as the payload. So for example, Message = "Hello" Perfect. Now that we've implemented this SearchService class, we need to tell the dependency injection container that we're going to use the SearchService class as the concrete implementation of ISearchService. This is going to be implemented right here. Services.AdScoped, ISearchService, and the concrete implementation is SearchService. Excellent. Now, this search microservice needs to know where the orders, customers, and products microservices are. So in this particular case, customers, for example, it's in the local host port 58323. So we need to specify this in the search microservice. We're going to open the appsSettings.json file because we're going to create this Services section, and inside this section, let's go ahead and create the Customers value with this address and we need the same for the Orders and for the Products microservice. So let's go back here and open Orders and we're going to open this LaunchSettings.json file. And let's copy this address. And we're going to do the same with the Products microservice. Let's copy this, and paste this value in the Products item. Please be aware that these addresses could be different in your machine. Now we're going to configure the solution by right-clicking on the solution and selecting this Set Startup Projects. Because what we want to do is to make sure that all the projects are starting when we're debugging the search microservice. So I already did this, so we're good to go. So let's test this by clicking on the Start button here. And then we're going to open the Postman application. So we can invoke the search microservice. So here we are in Postman. My search microservice is running on port 51834. And let me write the route for this microservice, and let's change the verb to post, and we need to body. This body will be written in json format, so we can say here CustomerId value number one. Let's execute this and as we can see, we're receiving this response, with the message Hello. It looks like our search microservice is good to go, so join me in the next video, where we're going to invoke the orders microservice.

Contents