簡體   English   中英

Autofac,解析操作結束

[英]Autofac, resolve operation ended

我剛開始使用 Autofac 並嘗試連接 Asp.Net Identity,但失敗了。 我以為我一切正常,但我卡住了,需要幫忙。

UserManager 通過構造函數注入。

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

此解析操作已結束。 使用 lambda 注冊組件時,無法存儲 lambda 的 IComponentContext 'c' 參數。 相反,要么從“c”再次解析 IComponentContext,要么解析基於 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));
        }
    }

我解決了我自己的問題,但老實說......我不知道我是如何解決這個問題的。 這只是反復試驗。 這是新的配置; 舊部分已注釋。

它甚至不需要 Owin 部分的最后一個 Register ......甚至評論說它現在看起來工作得很好。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM