2017-02-03 2 views
2

私はAutofacを使い、Asp.Net IDを結びつけようとしていますが失敗しています。私はすべてが働いていると思ったが、私は立ち往生して手を必要とした。Autofac、解決操作が終了しました

UserManagerは、コンストラクタを介して注入されます。

この非同期呼び出しに到達すると、私のUserController.csが問題を抱えています。

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

このresolveは、すでに終了しています。ラムダを使用してコンポーネント を登録すると、ラムダ のIComponentContext 'c'パラメータを保存できません。かわりに、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)); 
     } 
    } 

答えて

2

私は自分の問題を解決しましたが、正直なところ...私はこれをどのように修正したのか分かりません。それは試行錯誤だった。これは新しい設定です。古い部分はコメントされています。

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(); 
+0

私は1つの提案をすることはできますか? 'InstancePerRequest'を' InstancePerLifetimeScope'にスワップすると、要求外のスコープで作業を始めたときに、頭痛の種がたくさんあります。 [Documentation](http://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-lifetime-scope) – Nico

+1

@Nico、ありがとう、非常に良い提案。私はちょうどそれについての情報を読んで、新生児の予想登録は子供のために失効する可能性があります。 –

関連する問題