[ACCEPTED]-Web API response time-asp.net-web-api

Accepted answer
Score: 11

If you want to implement Web Api Monitoring, you 6 can create a custom DelegatingHandler to 5 track action duration and status.

Here is 4 a very basic example to measure operation 3 duration. The duration is added to response 2 (quite useless) ; it's better to store this 1 kind of data to a dedicated repository.

public class MonitoringDelegate : DelegatingHandler
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
        CancellationToken cancellationToken)
        var watcher = Stopwatch.StartNew();
        var response = await base.SendAsync(request, cancellationToken);

        //store duration somewheren here in the response header
        response.Headers.Add("X-Duration", watcher.ElapsedMilliseconds.ToString());

        return response;
Score: 2

You could start a Stopwatch on your client and stop 1 it when you recive your awnser

More Related questions