The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'handler' is not null

一笑奈何 提交于 2019-12-06 09:07:54

问题


I have a custom message handler that I add globally as follows:

public class CustomerHandler : DelegatingHandler
{
    public CustomHandler(HttpConfiguration httpConfiguration)
    {
        InnerHandler = new HttpControllerDispatcher(httpConfiguration); 
    }
}

config.MessageHandlers.Add(new CustomHandler(config));

However, I get the following error:

The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'CustomHandler' is not null. Parametername: handlers

So I changed the code to:

config.MessageHandlers.Add(new CustomHandler(config) { InnerHandler = null });

And I get an error:

ArgumentNullException

When I add the handler to the individual routing, it works fine.

var customHandler = new CustomHandler(config);

config.Routes.MapHttpRoute(
  name: "default",
  routeTemplate: "api/{controller}/{id}",
  defaults: new { id = RouteParameter.Optional },
  constraints: new { id = @"^[0-9]+$" },
  handler: customHandler
);

So it always throws an exception when the Innerhandler is set correctly or null.

What am I missing?

EDIT

This custom handler is used to check that a particular header is in the request and if found, gets the user details from a memory cache. Would it make more sense to change from a handler to a module?


回答1:


First you should read up on HTTP Message Handlers in ASP.NET Web API to get a better understanding of the handlers.

Message handlers are called in the same order that they appear in MessageHandlers collection. Because they are nested, the response message travels in the other direction. That is, the last handler is the first to get the response message.

Notice that you don't need to set the inner handlers; the Web API framework automatically connects the message handlers.

If intending to add the handler to the pipeline then you should not set the inner handler. the framework will do that based on the order the delegating handler was added to the pipeline.

When adding the handler to the route directly there is no need for the inner handler so that is why it works as you stated in the OP.




回答2:


Usually this error happens when you reuse the same "Handler" instance in a batch or a loop.

Disposing the HttpClient properly at the end of each request makes sure you always start fresh:

        using (var client = new HttpClient(
                HttpClientFactory.CreatePipeline(
                new HttpClientHandler
                {
                    CookieContainer = new CookieContainer(),
                    UseCookies = true
                },
                new DelegatingHandler[] { new CustomHandler() })))
        {

            var content = new StringContent(request, Encoding.UTF8,"text/xml");

            using (var response = await client.PostAsync(requestUrl, content))
            {
                // DO response processing
            }
        }

Hope this can help.



来源:https://stackoverflow.com/questions/41889742/the-delegatinghandler-list-is-invalid-because-the-property-innerhandler-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!