Hello i am trying to understand how can one create a LinkedTokenSource based on the Timeout property set using the typed HttpClient (using IHttpClientFactory) extension and any other CancellationToken(s):
Registration
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("someClient",(client)=>
{
client.Timeout=Timespan.FromSeconds(15);
});
}
Usage
public class SomeService
{
private IHttpClientFactory factory;
public SomeService(IHttpClientFactory factory)
{
this.factory = factory;
}
public async Task MethodWithAggreatedToken(CancellationToken token = default)
{
CancellationToken additionalToken = token;
if (additionalToken == default)
{
CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(Timespan.FromSeconds(10));
}
var client = factory.CreateClient("someService");
HttpRequestMessage msg = new HttpRequestMessage(.....);
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(additionalToken,[DI token])
{
var response = await client.SendAsync(msg, cts.Token);
//can i created a linked token?
//and if not who gets priority :
//1.the token set via the `Timeout` property set in Startup
//2.the one injected in the `SendAsync`
}
}
}
In the case above i want to be able to set a generic Timeout for my typed HttpClient which will be used in many methods.Now i want to know if :
- There is a way to add additional
Token(s) above this general condition thus creating aLinkedTokenSource - Who gets priority if i set a
CancellationTokenin theHttpClient.SendAsyncwhile also declaring theTimeoutproperty in theservices.AddHttpClient.
I want to know if i can do composition of tokens and also who gets overwritten (SendAsync or Timeout)