Sphyrnidae Common Library
2.0.1
Shared Utilities/Library
|
A Web Service is a call made from your server (business logic code) to another server's API endpoint. This could be SOA, microservices, or an external system. This is usually accomplished using API-to-API communication (eg. A token is provided for authentication). This allows you to design a microservice/SOA type solution instead of retrieving all information from the Data Access Layer.
The WebServiceBase class allows you to quickly and easily create multiple web service classes. It is recommended that you create a class per route type (eg. All actions against a controller). This is similar methodology to creation of your Data Access Layer, Services, etc.
The important things that WebServiceBase does for you:
None in this package, however it is recommended the following interface implementations utilize web services (despite the name provided, they don't actually have to perform web service calls)
These are all called by their respective settings.
// Create the model which will be returned by the web service call public class Widget { public int Id { get; set; } public string Name { get; set; } public int CustomerId { get; set; } } // Create the web service class public class WidgetWebService : WebServiceBase, IWidgetWebService { private static string _url; private string Url => _url ??= SettingsEnvironmental.Get(Env, "URL:Widget"); private IEnvironmentSettings Env { get; } private IApplicationSettings App { get; } public WidgetWebService( IHttpClientFactory factory, IHttpClientSettings settings, IIdentityHelper identity, ILogger logger, IEnvironmentSettings env, IApplicationSettings app ) : base(factory, settings, identity, logger) { Env = env; App = app; } public async Task<IEnumerable<Widget>> Get(int customerId) { const string name = "Widget_Get"; var url = new UrlBuilder(Url) .AddPathSegment(customerId.ToString()) // Could be path segment, querystring, etc .Build(); var response = await GetAsync(name, url); return await GetResult<IEnumerable<Widget>>(response, name); } // API-to-API communication. These headers need to be set protected override void AlterHeaders(HttpHeaders headers) { headers.Add(Constants.ApiToApi.Application, App.Name); headers.Add(Constants.ApiToApi.Token, Env.Get("ApiAuthorization:Widget")); } } // Call this to get widgets IWidgetWebService Widgets; // Should be injected var widgets = await Widgets.Get(1);