简体   繁体   English

简单的注入器可以在没有BeginExecutionContextScope,Web Api和OWIN的情况下工作

[英]Simple Injector works without BeginExecutionContextScope, Web Api and OWIN

I have a Console application that hosts WebApi(OWIN) project with Simple Injector, and according to documentation I have to add a few code lines of code to make it work. 我有一个Console应用程序,该应用程序承载带有Simple Injector的WebApi(OWIN)项目,并且根据文档,我必须添加一些代码行来使其工作。

But in my case it works even without that configuration, so the question is: What I am doing wrong and why it works? 但就我而言,即使没有这种配置也可以使用,所以问题是:我做错了什么,为什么会起作用?

as for me there are two option: I thing that something was fixed recently and documentation is not up to date OR Actually I did something wrong and it might cause a lot of problems in the future. 对于我来说,有两个选择:我认为最近已修复某些问题,并且文档不是最新的,或者实际上我做错了什么,将来可能会引起很多问题。

Here is my code: 这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        var disposable = WebApp.Start<Startup>("http://localhost:8085");

        Console.WriteLine("Server Started; Press enter to Quit");
        Console.ReadLine();
    }
}

public class Startup
{
    // This method is required by Katana:
    public void Configuration(IAppBuilder app)
    {
        var container = new Container();

        //app.Use(async (context, next) => {
        //    using (container.BeginExecutionContextScope())
        //    {
        //        await next();
        //    }
        //});

        container.RegisterWebApiRequest<ITestRepo, TestRepo>();

        //container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

        container.Verify();

        var config = ConfigureWebApi();

        config.DependencyResolver =
          new SimpleInjectorWebApiDependencyResolver(container);

        //WebApiConfig.Register(config);
        app.UseWebApi(config);
    }

    private HttpConfiguration ConfigureWebApi()
    {
        var config = new HttpConfiguration();
        HttpRouteCollectionExtensions.MapHttpRoute(config.Routes, "DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
        return config;
    }

Controller: 控制器:

public class CompaniesController : ApiController
{
    private readonly ITestRepo _repo;

    public CompaniesController(ITestRepo repo)
    {
        _repo = repo;//i get it here...
    }
}

Is it Ok that it works Thanks Ihor 行不行,可以吗?

Simple Injector's SimpleInjectorWebApiDependencyResolver will ensure that an execution context scope is started when IDependencyResolver.BeginScope is called by the Web API pipeline. 当Web管道调用IDependencyResolver.BeginScope时,Simple Injector的SimpleInjectorWebApiDependencyResolver将确保启动执行上下文范围。 This ensures that a ApiController is resolved within a scope and this allows scoped instances to be resolved and injected. 这样可以确保在范围内解析ApiController,并允许解析和注入范围内的实例。

When integrating Web API in OWIN however, you'll often find yourself wanting to execute certain logic in the OWIN pipeline, before or after executing an Web API Controller. 但是,将Web API集成到OWIN中时,您常常会发现自己想要在执行Web API控制器之前或之后在OWIN管道中执行某些逻辑。 But because this code runs either before or after the Web API pipeline, there is no active scope at that point. 但是,因为此代码在Web API管道之前或之后运行,所以此时没有活动范围。

If in case you need to run logic that requires scoped instances, you will need to start an execution context scope explicitly using the app.Use(async (context, next) call that you commented out. 如果需要运行需要作用域实例的逻辑,则需要使用app.Use(async (context, next)注释掉的调用。

Since Simple Injector v2.8, the SimpleInjectorWebApiDependencyResolver will ensure that when IDependencyResolver.BeginScope is called in case it is already running in the context of an existing execution context scope (because for instance you start a scope in the OWIN pipeline, it will not start a new scope itself, but reuse that existing scope. This ensures that all scoped instances will be created just once per request (which is what you normally want). 从Simple Injector v2.8开始, SimpleInjectorWebApiDependencyResolver将确保在IDependencyResolver.BeginScope (如果它已在现有执行上下文作用域中运行)(例如,在OWIN管道中启动作用域,则它将不会启动)一个新的作用域本身,但重用该现有作用域,以确保每个请求仅创建一次所有作用域实例(这通常是您想要的)。

Long story short, what you are doing has always worked; 长话短说,您所做的始终有效; you only need to register the execution context scope explictly when you are resolving instances in the OWIN pipeline. 在解析OWIN管道中的实例时,只需要显式注册执行上下文作用域。 What has changed is that now Web API ensures that it won't run its own scope, which is often much more convenient. 发生的变化是,现在Web API确保它不会运行自己的作用域,这通常更加方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM