Resolving IOwinContext in MVC5 application using Autofac

前端 未结 1 460
无人及你
无人及你 2021-02-04 21:45

I have trouble using MembershipReboot with the new ASP MVC5 template and Autofac. I have used the default MVC5 template to set up the site and then tri

相关标签:
1条回答
  • 2021-02-04 22:35

    There's a newer sample that does DI with AutoFac for MVC:

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

    See if this helps.

    If you don't want to use HttpContext.Current you could do something like this:

    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();
        }
    }); 
    

    This is what we're doing internally for some of our apps. You'd need to wire up your Web API service resolver to look for "idsrv:AutofacScope". Tugberk has a post on this:

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

    0 讨论(0)
提交回复
热议问题