我刚开始使用Autofac并试图连接 Asp.Net 身份,但我失败了。我以为我一切都可以正常工作,但我被卡住了,需要帮助。
用户管理器通过构造函数注入。
private readonly ApplicationUserManager UserManager;
private readonly ApplicationSignInManager SignInManager;
public UserController(IDocumentSession documentSession, ApplicationUserManager userManager, ApplicationSignInManager signInManager) : base(documentSession) {
UserManager = userManager;
SignInManager = signInManager;
}
我的用户控制器.cs在我到达此异步调用的那一刻遇到问题。
var user = await UserManager.FindAsync(model.Email, model.Password);
此解析操作已结束。注册组件时 使用 lambdas,将 IComponentContext 'c' 参数转换为 lambda 无法存储。相反,请再次从 "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>))
.AsImplementedInterfaces()
.InstancePerRequest()
.OnRelease(x =>
{
x.Dispose();
});
b.Register<IdentityFactoryOptions<ApplicationUserManager>>(c =>
new IdentityFactoryOptions<ApplicationUserManager>() {
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
b.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
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部分的最后一个注册......甚至评论它现在似乎工作得很好。
// ASP.Net Identity Registrations
/*
b.Register(context => new UserStore<User>(context.Resolve<IDocumentSession>))
.AsImplementedInterfaces()
.InstancePerRequest()
.OnRelease(x =>
{
x.Dispose();
});
b.Register<IdentityFactoryOptions<ApplicationUserManager>>(c =>
new IdentityFactoryOptions<ApplicationUserManager>() {
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
b.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
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();