Autofac,解决操作结束

时间:2017-02-03 22:02:15

标签: c# model-view-controller asp.net-identity owin autofac

我刚开始使用Autofac并尝试连接Asp.Net Identity而我失败了。我以为我的一切都在运转,但我卡住了,需要一只手。

通过构造函数注入Us​​erManager。

private readonly ApplicationUserManager UserManager;
        private readonly ApplicationSignInManager SignInManager;
        public UserController(IDocumentSession documentSession, ApplicationUserManager userManager, ApplicationSignInManager signInManager) : base(documentSession) {
            UserManager = userManager;
            SignInManager = signInManager;
        }

我的UserController.cs在我到达此异步调用时遇到问题。

var user = await UserManager.FindAsync(model.Email, model.Password);
  

此解决操作已经结束。注册组件时   使用lambdas,IComponentContext' c' lambda的参数   无法存储。相反,要么从中再次解析IComponentContext   ' c'或解析Func<>基于工厂创建后续组件   从

问题是我得到了一个非常明确的信息,但我不知道如何继续使用它;我应该以不同的方式注册我的ApplicationUserManager吗?

我已经像这样设置了我的容器。我希望有人可以看看。

public static void RegisterContainer(IAppBuilder app)
        {
            // Autofac container .. 
            var builder = new ContainerBuilder();

            // Register all MVC Controllers
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            // Register all services that implement the Module interface
            builder.RegisterAssemblyModules(BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray());

            // Register the DocumentStore instance
            builder.Register(context => new RavenDocumentStoreFactory()
                .FindOrCreate())
                .As<IDocumentStore>()
                .SingleInstance();

            // Multi tenancy part      
            var tenantIdStrategy = new TenantIdStrategy();
            var multitenantContainer = new MultitenantContainer(tenantIdStrategy, builder.Build());
            var tenantIds = new[]
            {
                "subdomain1.tickets",
                "localhost"
            };

            foreach (var tenantId in tenantIds)
            {
                var databaseName = $"ticketTenant-{tenantId.Replace('.', '_')}";



                multitenantContainer.ConfigureTenant(tenantId, b =>
                {
                    // Init RavenDB 
                    b.Register(context => new RavenDocumentSessionFactory(databaseName))
                        .InstancePerTenant()
                        .AsSelf();

                    // Session per request
                    b.Register(context => context.Resolve<RavenDocumentSessionFactory>()
                        .FindOrCreate(context.Resolve<IDocumentStore>()))
                        .As<IDocumentSession>()
                        .InstancePerRequest()
                        .OnRelease(x =>
                        {
                            x.SaveChanges();
                            x.Dispose();
                        });

                    //  ASP.Net Identity Registrations
                    b.Register(context => new UserStore<User>(context.Resolve<IDocumentSession>))
                        .AsImplemented‌​Interfaces()
                        .Instanc‌​ePerRequest()
                        .OnRelease(x =>  
                        {  
                            x.Dispose();
                        });

                    b.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => 
                        new IdentityFactoryOptions<ApplicationUserManager>() { 
                            DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionPr‌​ovider("ApplicationName") 
                    }); 

                    b.RegisterType<ApplicationUserManager>().AsSelf().Inst‌​ancePerRequest();
                    b.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
                    b.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
                    b.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();


                });
            }

            // Register in Owin
            app.UseAutofacMiddleware(multitenantContainer);
            app.UseAutofacMvc();

            // Dependency Resolver to Autofac
            DependencyResolver.SetResolver(new AutofacDependencyResolver(multitenantContainer));
        }
    }

1 个答案:

答案 0 :(得分:2)

我解决了自己的问题,但说实话......我还没弄清楚我是如何解决这个问题的。这只是反复试验。这是新配置;旧的部分被评论。

它甚至不需要Owin部分的最后一个注册...甚至评论它现在似乎工作得很好。

//  ASP.Net Identity Registrations
                    /*
                    b.Register(context => new UserStore<User>(context.Resolve<IDocumentSession>))
                        .AsImplemented‌​Interfaces()
                        .Instanc‌​ePerRequest()
                        .OnRelease(x =>  
                        {  
                            x.Dispose();
                        });

                    b.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => 
                        new IdentityFactoryOptions<ApplicationUserManager>() { 
                            DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionPr‌​ovider("ApplicationName") 
                    }); 

                    b.RegisterType<ApplicationUserManager>().AsSelf().Inst‌​ancePerRequest();
                    b.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
                    b.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
                    b.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();
                    */

                    b.Register(c => new UserStore<User>(c.Resolve<IDocumentSession>())).As<IUserStore<User>>().InstancePerRequest()
                        .OnRelease(x =>  
                        {  
                            x.Dispose();
                        });
                    b.RegisterType<ApplicationUserManager>().InstancePerRequest();
                    b.RegisterType<ApplicationSignInManager>().InstancePerRequest();
                    b.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
                    b.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();