【问题标题】:How do you do dependency injection with AutoFac and OWIN?你如何使用 AutoFac 和 OWIN 进行依赖注入?
【发布时间】:2013-12-02 09:18:46
【问题描述】:

这是针对 MVC5 和新管道的。我在任何地方都找不到很好的例子。

public static void ConfigureIoc(IAppBuilder app)
{
    var builder = new ContainerBuilder();
    builder.RegisterControllers(typeof(WebApiApplication).Assembly);
    builder.RegisterApiControllers(typeof(WebApiApplication).Assembly);
    builder.RegisterType<SecurityService().AsImplementedInterfaces().InstancePerApiRequest().InstancePerHttpRequest();

    var container = builder.Build();
    app.UseAutofacContainer(container);
}

上面的代码没有注入。在尝试切换到 OWIN 管道之前,这工作正常。就是用 OWIN 找不到任何关于 DI 的信息。

【问题讨论】:

  • 问题是?顺便说一句,为什么在容器构建后注册“SecurityService”?
  • 编辑样本以将安全服务移至之前。这只是一个例子。我正在尝试了解 AutoFac 是否支持 OWIN 管道。
  • 自 2014 年 2 月起提供 Autofac OWIN 软件包。nuget.org/packages/Autofac.Owin 在撰写本文时,它仍处于预发布状态,因此如果您在VS 用户界面。

标签: c# dependency-injection asp.net-mvc-5 owin autofac


【解决方案1】:

更新:有一个官方的 Autofac OWIN nuget packagea page with some docs

原文:
有一个解决 IoC 和 OWIN 集成问题的项目,称为 DotNetDoodle.Owin.Dependencies,可通过 NuGet 获得。基本上Owin.Dependencies 是 OWIN 管道的 IoC 容器适配器。

示例启动代码如下所示:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        IContainer container = RegisterServices();
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute("DefaultHttpRoute", "api/{controller}");

        app.UseAutofacContainer(container)
           .Use<RandomTextMiddleware>()
           .UseWebApiWithContainer(config);
    }

    public IContainer RegisterServices()
    {
        ContainerBuilder builder = new ContainerBuilder();

        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterOwinApplicationContainer();

        builder.RegisterType<Repository>()
               .As<IRepository>()
               .InstancePerLifetimeScope();

        return builder.Build();
    }
}

RandomTextMiddleware 是来自 Microsoft.Owin 的 OwinMiddleware 类的实现。

“OwinMiddleware 类的 Invoke 方法将在每个请求上被调用,我们可以在那里决定是处理请求、将请求传递给下一个中间件还是两者都做。Invoke 方法获取一个 IOwinContext 实例,我们可以通过 IOwinContext 实例获取每个请求的依赖范围。”

RandomTextMiddleware 的示例代码如下:

public class RandomTextMiddleware : OwinMiddleware
{
    public RandomTextMiddleware(OwinMiddleware next)
        : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
        IServiceProvider requestContainer = context.Environment.GetRequestContainer();
        IRepository repository = requestContainer.GetService(typeof(IRepository)) as IRepository;

        if (context.Request.Path == "/random")
        {
            await context.Response.WriteAsync(repository.GetRandomText());
        }
        else
        {
            context.Response.Headers.Add("X-Random-Sentence", new[] { repository.GetRandomText() });
            await Next.Invoke(context);
        }
    }
}

如需了解更多信息,请查看original article

【讨论】:

  • 试图实现这一点,但无法弄清楚 RandomTextMiddleware 的目的是什么?我不想这样,只想通过注入注册我的依赖项。
  • @Shane RandomTextMiddleware 只是为了展示您可以通过管道并在任何地方获取依赖项的示例。所以,你可以省略它。
  • @tugberk 所以你说这个只是为了省略这个。使用线,它应该工作吗? app.UseAutofacContainer(container) .Use() .UseWebApiWithContainer(config);
  • @Shane 是的,从那里放下Use&lt;RandomTextMiddleware&gt;()。在管道中注册您想要的任何内容。
  • 什么是RegisterOwinApplicationContainer
猜你喜欢
  • 2016-09-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
相关资源
最近更新 更多