【问题标题】:Resolving IOwinContext in MVC5 application using Autofac使用 Autofac 解析 MVC5 应用程序中的 IOwinContext
【发布时间】:2014-02-11 13:30:32
【问题描述】:

我无法将MembershipReboot 与新的 ASP MVC5 模板和Autofac 一起使用。我使用了默认的 MVC5 模板来设置站点,然后尝试连接 MembershipReboot 框架作为模板附带的 ASP 身份框架的替代品。

我遇到的这个问题是试图从Autofac 容器中解决IOwinContext。这是我在 Startup 课程中的接线(简化为基础知识)。这是MembershipReboot Owin 应用程序示例中使用的接线(除了他使用 Nancy)。

public partial class Startup
 {
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());

        builder.Register(c => new DefaultUserAccountRepository())
            .As<IUserAccountRepository>()
            .As<IUserAccountQuery>()
            .InstancePerLifetimeScope();

        builder.RegisterType<UserAccountService>()
            .AsSelf()
            .InstancePerLifetimeScope();

        builder.Register(ctx =>
        {
            **var owin = ctx.Resolve<IOwinContext>();** //fails here
            return new OwinAuthenticationService(
                MembershipRebootOwinConstants.AuthenticationType,
                ctx.Resolve<UserAccountService>(),
                owin.Environment);
        })
            .As<AuthenticationService>()
            .InstancePerLifetimeScope();

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        ConfigureAuth(app);
        app.Use(async (ctx, next) =>
        {
            using (var scope = container.BeginLifetimeScope(b =>
            {
                b.RegisterInstance(ctx).As<IOwinContext>();
            }))
            {
                ctx.Environment.SetUserAccountService(() => scope.Resolve<UserAccountService>());
                ctx.Environment.SetAuthenticationService(() => scope.Resolve<AuthenticationService>());
                await next();
            }
        });
    }

这是我的控制器,它具有在控制器构造函数中指定的依赖项。

public class HomeController : Controller
{
    private readonly AuthenticationService service;

    public HomeController(AuthenticationService service)
    {
        this.service = service;
    }

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

看来我需要将 Autofac 容器包装在 AutofacDependencyResolver 中,以便 MVC 框架使用容器来解析组件。这是与 Nancy Owin 示例和我在 MVC5 中使用的唯一主要区别。

当我这样做时,看起来(从我的跟踪中)好像在没有首先通过 OWIN middleware 堆栈的情况下解决了依赖关系,因此永远不会注册 IOwinContext

我在这里做错了什么?

更新:

Brock,当我将配置迁移到我的项目时,您的新示例运行良好。就我的理解而言,您的新示例中的这一行似乎将当前的 OwinContext 注册到容器中,而这正是之前所缺少的。

builder.Register(ctx=>HttpContext.Current.GetOwinContext()).As<IOwinContext>();

是这样的

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 autofac owin membershipreboot


    【解决方案1】:

    有一个更新的示例使用 AutoFac for MVC 进行 DI:

    https://github.com/brockallen/BrockAllen.MembershipReboot/blob/master/samples/SingleTenantOwinSystemWeb/SingleTenantOwinSystemWeb/Startup.cs

    看看这是否有帮助。

    如果你不想使用HttpContext.Current,你可以这样做:

    app.Use(async (ctx, next) =>
    {
        // this creates a per-request, disposable scope
        using (var scope = container.BeginLifetimeScope(b =>
        {
            // this makes owin context resolvable in the scope
            b.RegisterInstance(ctx).As<IOwinContext>();
        }))
        {
            // this makes scope available for downstream frameworks
            ctx.Set<ILifetimeScope>("idsrv:AutofacScope", scope);
            await next();
        }
    }); 
    

    这是我们在内部为我们的一些应用所做的。您需要连接您的 Web API 服务解析器以查找“idsrv:AutofacScope”。 Tugberk 对此有一个帖子:

    http://www.tugberkugurlu.com/archive/owin-dependencies--an-ioc-container-adapter-into-owin-pipeline

    【讨论】:

    • 没有System.Web(静态HttpContext.Current)怎么可能做到这一点
    • 这让我从 Alex 和他的类似 implementation 那里找到了这个 post
    • 是的,他会是最好的跟随者。我上面说的是他更新 Autofac 之前的内容。
    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多