1

I have this delegating handler in my api project:

class MyHandler : DelegatingHandler {
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, 
        CancellationToken cancellationToken) {
        var info = GrabSomeParametersFromHeader();
        var isValid = await Validate(info); // this is a very light database query
        if (!isValid) {
            Log(request, info, false);
            var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
            return response;
        }
        HttpContext.Current.SetMobileRequestInfo(info);
        var result = await base.SendAsync(request, cancellationToken);
        Log(request, info, result.IsSuccessStatusCode && 
        result.StatusCode == HttpStatusCode.OK);
        return result;
    }
}

HttpContext.Current.SetMobileRequestInfo simply adds the info object to http-items to use later in app.

The Validate is a very light database query. And the Log method is inserting request's data (such as URI and query-string etc.) to database.

Sometimes, I'm getting a weird behavior of the app: it simply goes down! There is no log, no error, nothing. Just server doesn't response the requests. I have to restart app (for example by making a fake change in web.config) to get it back to work. The app is a pretty simple app. I'm using ASP.NET Web API 2 on .NET 4.5 platform which is running on a IIS 7.5 machine. The only place I can think about is the mentioned delegating handler, which may cause the error. Do you have any idea? Does delegating handlers have any performance side effects which may cause the app to shut down?

amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • There is a bug in your code somewhere that causes this. Holding delegating handlers responsible for causing hangs is pointing fingers at random things. If this was true delegating handlers would be totally unusable under all circumstances. – usr Apr 16 '15 at 09:18
  • Thanks for the idea :) Do you mean this line: `var result = await base.SendAsync(request, cancellationToken);` ? actually there might be a bug in application. for example sometimes I get timeout error on db actions. Do you think that might cause the `SendAsync` to hang? – amiry jd Apr 16 '15 at 09:21
  • I have no idea what the problem is. Just pointing out that this problem is not caused by the mere presence of a delegating handler. – usr Apr 16 '15 at 09:22
  • My question is: in this line: `var result = await base.SendAsync(request, cancellationToken);` I'm awaiting for the actions get executed. Now, if there be an error in action executing (for example a timeout exception), what would happen in this line? – amiry jd Apr 16 '15 at 09:26

1 Answers1

0

The (very) short answer to your question is no. There is nothing inherent to DelegatingHandlers themselves that would cause your app to grind to a halt. It's far more likely that the issue is with what you're doing inside of it. Could be the database call, could be the logging. I'd recommend using a profiling tool to figure out where things are getting hung up.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173