The WebClientService package provides a robust and flexible web client component designed to be used as a service within .NET Core applications. It leverages the power of 'IHttpClientFactory' to facilitate efficient and scalable interactions with web services. This package supports dependency injection, making it easy to integrate into your existing .NET Core applications.
- Dependency Injection: Seamlessly integrates with the .NET Core dependency injection framework.
- HTTP Client Management: Utilizes 'IHttpClientFactory' for robust HTTP client instantiation and management.
- Result Handling: Comprehensive handling of HTTP response results with detailed status and error information.
- Extensible Base Classes: Abstract base classes for implementing services that interact with web endpoints, returning various data types including strings and streams.
- Configurable Requests: Supports various HTTP methods, custom headers, bearer token authentication, and more.
- Consuming RESTful APIs
- Fetching data from web services
- Handling HTTP responses with rich error information
- Streaming data from endpoints
- Installation: Install the package via NuGet Package Manager.
dotnet add package WebClientService
- Configuration: Configure IHttpClientFactory in your Startup.cs.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddScoped<WebClientStringDataService>();
services.AddScoped<WebClientStreamDataService>();
}
- Usage: Inject the service into your controllers or services and start making web requests.
public class MyService
{
private readonly WebClientStringDataService _webClient;
public MyService(WebClientStringDataService webClient)
{
_webClient = webClient;
}
public async Task<string> GetDataAsync(Uri requestUri)
{
var result = await _webClient.InvokeRequest(requestUri);
if (result.Result == RequestInvocationResultTypes.Ok)
{
return result.Data;
}
else
{
// Handle error
throw new Exception(result.Message);
}
}
}