I have a custom ASP.NET Web API message handler that for each request that comes it will save the request method and request uri to a custom database:
I'm trying to run the Save method async and I have done this so far. I just want to know if this is the right way to call Save method asynchronously? Is it correct to have await before Task.Run... ? Should I also add async on my save method: private async void Save(
Can somebody tell if I can use instead TaskCompletionSource<T>()
public class CustomHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
await Task.Run(() => Save(request), cancellationToken);
return response;
}
private void Save(HttpRequestMessage request)
{
var method = request.Method.Method;
var url = request.RequestUri.ToString();
// here is saved to custom db code removed
............................
}
}