Does SimpleInjector's WebAPIRequest lifetime include Message Handlers?

≯℡__Kan透↙ 提交于 2019-12-21 20:29:30

问题


I'm new to SimpleInjector and working through examples using it with WebAPI. I used the SimpleInjector.Integration.WebApi.WebHost.QuickStart nu-get package, then registered a simple type for my tests, like so:

container.RegisterWebApiRequest<SimplePOCO>();

From inside an ApiController method, I am able to request an instance. So far, so good. I wanted to expand my test to earlier in pipeline, specifically a Message Handler. So I created a simple DelegatingHandler like :

protected override Task<HttpResponseMessage> SendAsync(
                                           HttpRequestMessage request,
                                           CancellationToken cancellationToken) {
    Task<HttpResponseMessage> response;

    var container =  SimpleInjectorWebApiInitializer.container;
    var rc = container.GetInstance<SimplePOCO>();
    response = base.SendAsync(request, cancellationToken);
    response.ContinueWith((responseMsg) =>  {   });

    return response;
}

Calling GetInstance<SimplePOCO>() errors with the following message:

The registered delegate for type SimplePOCO threw an exception. The SimplePOCO is registered as 'Web API Request' lifestyle, but the instance is requested outside the context of a Web API Request.

Am I doing something wrong? Are Message Handlers outside the lifetime of a WebAPI request? This seems odd, considering how integral they are. If message handlers are outside the lifetime is there a longer lifetime that encompasses the message handlers?


回答1:


Are Message Handlers outside the lifetime of a WebAPI request?

Well, as a matter of fact, they are. Unless you trigger the creation of the IDependencyScope explicitly, the IDependencyScope gets created (by calling request.GetDependencyScope()) inside the DefaultHttpControllerActivator.Create method.

To make sure your code runs within a dependency scope, all you have to do is call request.GetDependencyScope() explicitly inside your handler:

protected override Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request, CancellationToken cancellationToken) {

    // trigger the creation of the scope.
    request.GetDependencyScope();

    Task<HttpResponseMessage> response;

    var container =  SimpleInjectorWebApiInitializer.container;
    var rc = container.GetInstance<SimplePOCO>();
    response = base.SendAsync(request, cancellationToken);
    response.ContinueWith((responseMsg) =>  {   });

    return response;
}


来源:https://stackoverflow.com/questions/22311361/does-simpleinjectors-webapirequest-lifetime-include-message-handlers

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