- Common approach: shared methods and a common fallback for gateways
var response = await _gateway.GetCase(caseId);
We have always had need to use Gateways within our applications and had so many times we were copying and pasting the same files over to allow the same functionality. Finally enough was enough and we decided to encapsulate all of this functionality into A shared NuGet package with a set default for our applications
Using a micro service architechture we wanted an easy way for services to expose their endpoints to any of our other services. In place or remembering the exact URL for a specific endpoint these gateways provide that information for the developer.
Within your application configuration you define the base URL for your service, allowing you to overide it per environment if you want to point to a test system.
This config is then used within the Gateway pacakge to route the request to the correct endpoint.
"IGatewayConfig": {
"BaseUrl": "http://service.stockport.gov.uk/",
"AuthToken": "TestToken",
"EnablePollyPolicies": "false",
"ProxyUrl": "127.0.0.1", (Optional)
"ProxyPort":8080 (Optional)
}
Adding resilient clients (i.e. with circuit breaker) is now done via the gateways package (rather than through polly. To enable this behaviour add the following to startup.
services.AddHttpClient<IGateway, Gateway>(Configuration);
To use a gateway simply inject that gateway into the consumer.
public HomeController(IYourGatewayName gateway)
New gateways are constructed slightly differently, the following pattern is a gateway and example get request
public class MyNewGateway : Gateway, IMyNewGateway
{
public MyNewGateway(HttpClient httpClient) : base(httpClient)
{
}
public async Task<HttpResponseMessage> DoSomethingAsync(string data)
{
var url = $"/api/v1/endpoint/{data}";
return await GetAsync(url);
}
$ dotnet add package StockportGovUK.NetStandard.Gateways