I have an ASP.NET Core application which needs to cyclically call a web service.
I instantiated a HostedService:
public class MyHostedService : IHostedService
{
public MyHostedService(IMyWebServiceClient MyWebServiceClient)
{
...
}
public Task StartAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var result = await myWebServiceClient.CallSomeWebApi();
await Task.Delay(5000, cancellationToken);
}
return Task.CompletedTask;
}
}
which calls some function from a WebServiceClient class. The class looks like this
public class MyWebServiceClient: IMyWebServiceClient
{
private readonly IHttpClientFactory httpClientFactory;
public MyWebServiceClient(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory;
}
public async Task CallSomeWebApi()
{
using (var httpClient = httpClientFactory.CreateClient())
{
var requestMessage = new HttpRequestMessage(...);
var response = await httpClient.SendAsync(requestMessage);
}
}
}
The startup code is this:
public void ConfigureServices(IServiceCollection services)
{
....
services.AddHttpClient();
services.AddSingleton<IMyWebServiceClient, MyWebServiceClient>();
services.AddHostedService<MyHostedService>();
....
}
Can somebody tell me if it is a good practice calling cyclically
var httpClient = httpClientFactory.CreateClient()
(for instance every 5 seconds)?
Could some side effects occur because of this? I am testing with some Web Server and quite often getting an exception
The request was canceled due to the configured
HttpClient.Timeoutof 100 seconds elapsing
Could this be caused by the implementation above, or is this definitely a server problem?