简单的注入器 - ASP.NET 5身份和RavenDB的困难

时间:2015-12-27 17:03:25

标签: c# asp.net asp.net-identity simple-injector

我正在尝试在SimpleInjector应用程序上使用ASP.NET 5MVC,并且遇到一个主要问题,正确连线。

我运行一个RavenDB数据库,因此我有实例化实例的方法,这是我在注册接口时使用的,就像这样;

private void InitializeContainer(IApplicationBuilder app) {

    container.CrossWire<IUserStore<AppUser>>(app);
    container.CrossWire<UserManager<AppUser>>(app);
    container.CrossWire<SignInManager<AppUser>>(app);
    container.CrossWire<ILoggerFactory>(app);

    container.Register<IDocumentStore>(RavenDatabase.OpenDatabase);

    container.Register<IDocumentSession>(RavenDatabase.OpenSession);

    container.Register<IAsyncDocumentSession>(RavenDatabase.OpenAsyncSession);

    container.Register<ILookupNormalizer>(() => new LowerInvariantLookupNormalizer());

    container.Register<IPasswordHasher<AppUser>>(() => new PasswordHasher<AppUser>());
}

问题似乎与IUserStore有关。当我尝试加载应用程序时,我得到了这个例外..

  

尝试激活“App.Identity.UserStore”1 [App.Identity.AppUser]时,无法解析“Raven.Client.IAsyncDocumentSession”类型的服务。

但是,我真的不确定为什么会这样。我注册了IAsyncDocumentSession,为什么不能注入呢?我也试过这样......

container.Register<IUserStore<AppUser>>(RavenDatabase.UserStore);

使用定义为此类的方法......

public static UserStore<AppUser> UserStore() {
    // check to see if we even have a session factory to get a session from
    if (documentStore == null)
        OpenDatabase();

    return new UserStore<AppUser>(documentStore.OpenAsyncSession());
}

但我仍然继续得到例外。我在这里不知所措。这是我第一次使用Simple Injector。

堆栈跟踪

System.InvalidOperationException

Unable to resolve service for type 'Raven.Client.IAsyncDocumentSession' while attempting to activate 'App.Identity.UserStore`1[App.Identity.AppUser]'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet<Type> callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) 
at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet<Type> callSiteChain) 
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet<Type> callSiteChain) 
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet<Type> callSiteChain) 
at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet<Type> callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) 
at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet<Type> callSiteChain) 
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet<Type> callSiteChain) 
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet<Type> callSiteChain) 
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider) 
at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd<TKey, TValue, TArg>(ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg arg) 
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) 
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService<T>(IServiceProvider provider) 
.lambda_method(Closure ) 
at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance() 
at SimpleInjector.InstanceProducer.GetInstance()

SimpleInjector.ActivationException

Unable to resolve service for type 'Raven.Client.IAsyncDocumentSession' while attempting to activate 'App.Identity.UserStore`1[App.Identity.AppUser]'.
at SimpleInjector.InstanceProducer.GetInstance() 
at SimpleInjector.InstanceProducer.VerifyInstanceCreation()

System.InvalidOperationException

The configuration is invalid. Creating the instance for type LoginController failed. Unable to resolve service for type 'Raven.Client.IAsyncDocumentSession' while attempting to activate 'App.Identity.UserStore`1[App.Identity.AppUser]'.

System.Reflection.TargetInvocationException

Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 
at Microsoft.AspNet.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) 
at Microsoft.AspNet.Hosting.Startup.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder) 
at Microsoft.AspNet.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder) 
at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()

1 个答案:

答案 0 :(得分:1)

据我所知,这个问题与Simple Injector无关。从您的注册和异常信息我看到以下情况:

  • 您正在从Simple Injector解析LoginController
  • LoginController取决于Raven.Client.IAsyncDocumentSession
  • IAsyncDocumentSession使用CrossWire<IAsyncDocumentSession>从ASP.NET配置系统交叉连接到Simple Injector。
  • IAsyncDocumentSession已在ASP.NET配置系统中注册。
  • 在ASP.NET中注册的IAsyncDocumentSession的实现取决于UserStore<AppUser>
  • ASP.NET配置系统无法解析IAsyncDocumentSession注册并抛出异常说明:
  

无法解决类型&#39; Raven.Client.IAsyncDocumentSession&#39;尝试激活App.Identity.UserStore`1 [App.Identity.AppUser]&#39;。

换句话说,当您从ASP.NET配置系统解析LoginControllerIAsyncDocumentSession时,您会得到相同的异常。只需调用以下方法之一,您将看到相同的错误:

app.ApplicationServices.GetRequiredService<LoginController>();
// or
app.ApplicationServices.GetRequiredService<IAsyncDocumentSession>();

我认为IAsyncDocumentSession实施取决于UserStore<T>IUserStore<T>,但您尚未在ASP.NET配置系统中注册它。