Whats RestEase And How To Use RestEase On MicroService Architecture

Introduction

 
Before I start explaining RestEase, let me give a brief about how we used to communicate between two APIs. We use HTTP protocol and we create an instance of HTTP Client for sending requests and receiving responses.
 
Here are a few drawbacks of using the HTTPClient:
  • One call takes a lot of code lines, so you’ll probably end up with an additional wrapper class (because you follow DRY, don’t you?)
  • Adding more support for features like Authorization headers, query parameters cause the wrapper class to be even bigger which pulls you away from the original task
  • It’s the developer’s responsibility to dispose of the client.
  • It’s the developer’s responsibility to deserialize the response.
But RestEase will make our life simpler. Basically, RestEase is a little type-safe REST API client library for .NET Framework 4.5 and higher and .NET Platform Standard 1.1, which aims to make interacting with remote REST endpoints easy, without adding unnecessary complexity. It won't work on platforms that don't support runtime code generation, including .NET Native and iOS.

Almost every aspect of RestEase can be overridden and customized, leading to a large level of flexibility.

To use it, you define an interface(we can inject this service through the constructor which represents the endpoint you wish to communicate, where methods on that interface correspond to requests that can be made on it. RestEase will then generate an implementation of that interface for you, and by calling the methods you defined, the appropriate requests will be made.

RestEase is built on top of HttpClient and it is easy to gain access to the full capabilities of HttpClient, giving you control and flexibility, when you need it.

So let's see how we can implement RestEase in a microservice architecture.
 
Step 1
 
Add a blank solution:
 
Whats Restease And How We Can Use RestEase On MicroService Architecture
 
Step 2
 
Create two folders under the Blank solutions, Service1 and Service2.
 
Add an API project, MicroService1, into Service1 and MicroService2 into Service2.
 
Whats Restease And How We Can Use RestEase On MicroService Architecture
 
Step 3
 
Install RestEase on the MicroService1 project
 
Whats Restease And How We Can Use RestEase On MicroService Architecture
 
Once it's installed, add one interface, IPublishService.
  1. [SerializationMethods(Query = QuerySerializationMethod.Serialized, Body = BodySerializationMethod.Serialized)]  
  2. public interface IPublishService {  
  3.     [AllowAnyStatusCode]  
  4.     [Get("Subscriber/getweather")]  
  5.     Task < IEnumerable < WeatherForecast >> GetWeather();  
  6. }  
MicroService1 Controller will look like this:
  1. [ApiController]  
  2. [Route("[controller]")]  
  3. public class PublishController: ControllerBase {  
  4.     private readonly IPublishService _publishService;  
  5.     public PublishController() {  
  6.             _publishService = RestClient.For < IPublishService > ("http://localhost:5001");  
  7.         }  
  8.         [HttpGet]  
  9.     public async Task < IEnumerable < WeatherForecast >> Get() {  
  10.         var weather = await _publishService.GetWeather();  
  11.         return weather;  
  12.     }  
  13. }  
MicroService2 Controller will look like this:
  1. [ApiController]  
  2. [Route("[controller]")]  
  3. public class SubscriberController: ControllerBase {  
  4.     private static readonly string[] Summaries = new [] {  
  5.         "Freezing",  
  6.         "Bracing",  
  7.         "Chilly",  
  8.         "Cool",  
  9.         "Mild",  
  10.         "Warm",  
  11.         "Balmy",  
  12.         "Hot",  
  13.         "Sweltering",  
  14.         "Scorching"  
  15.     };  
  16.     private readonly ILogger < SubscriberController > _logger;  
  17.     public SubscriberController(ILogger < SubscriberController > logger) {  
  18.             _logger = logger;  
  19.         }  
  20.         [HttpGet]  
  21.         [Route("getweather")]  
  22.     public async Task < IEnumerable < WeatherForecast >> Get() {  
  23.         var rng = new Random();  
  24.         return Enumerable.Range(1, 5).Select(index => new WeatherForecast {  
  25.             Date = DateTime.Now.AddDays(index),  
  26.                 TemperatureC = rng.Next(-20, 55),  
  27.                 Summary = Summaries[rng.Next(Summaries.Length)]  
  28.         }).ToArray();  
  29.     }  
  30. }  
I have used ASP.NET Core 3.1 to create this project, and I am adding my whole project as a reference.
 
I hope this is helpful.
 
Please refer to this link for more information here.


Similar Articles