Are message handlers included in the life of SimpleInjector WebAPIRequest?

I am new to SimpleInjector and working with examples using it using WebAPI. I used the SimpleInjector.Integration.WebApi.WebHost.QuickStartnu-get package , then registered a simple type for my tests, for example:

container.RegisterWebApiRequest<SimplePOCO>();

From inside the ApiController method, I can request an instance. So far, so good. I wanted to expand my test earlier than in the pipeline, in particular the message handler. So I created a simple DelegatingHandler, for example:

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:

A registered delegate for type SimplePOCO throws an exception. SimplePOCO is registered as a Web API Request style, but an instance is requested outside the context of the Web API request.

- ? WebAPI? , , . , , ?

+3
1

WebAPI?

, , . IDependencyScope, IDependencyScope ( request.GetDependencyScope()) DefaultHttpControllerActivator.Create.

, , , , request.GetDependencyScope() :

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;
}
+7

Source: https://habr.com/ru/post/1531661/


All Articles