简体   繁体   English

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

[英]Simple Injector - difficulty with ASP.NET 5 Identity and RavenDB

I am attempting to use SimpleInjector with ASP.NET 5 on an MVC application, and am running into a major problem getting it wired up right. 我正在尝试在MVC应用程序SimpleInjectorASP.NET 5一起使用,并且遇到了将其正确连接的主要问题。

I run a RavenDB database, and as such I have methods for instantiating instances to it, which is what I use when registering the interfaces, like this; 我运行一个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>());
}

The problem seems to be with IUserStore . 问题似乎出在IUserStore When I try to load up the application, I get this exception.. 当我尝试加载应用程序时,出现此异常。

Unable to resolve service for type 'Raven.Client.IAsyncDocumentSession' while attempting to activate 'App.Identity.UserStore`1[App.Identity.AppUser]'. 尝试激活“ App.Identity.UserStore`1 [App.Identity.AppUser]”时,无法解析类型为“ Raven.Client.IAsyncDocumentSession”的服务。

I'm really uncertain why this is, though. 我确实不确定为什么会这样。 I've registered IAsyncDocumentSession , so why can't it inject it? 我已经注册了IAsyncDocumentSession ,那么为什么不能注入它呢? I've also tried it like this... 我也这样尝试过...

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

With a method defined as such... 用这样定义的方法...

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

But I still continue to get the exception. 但是我仍然继续得到例外。 I'm at a loss, here. 我不知所措,在这里。 This is my first time using Simple Injector. 这是我第一次使用Simple Injector。

Stack Trace 堆栈跟踪

System.InvalidOperationException 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 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 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 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()

As far as I can see, this problem is not related to Simple Injector. 据我所知,此问题与简单注入器无关。 From your registrations, and the exception information I see the following is the case: 从您的注册以及例外信息中,我看到了以下情况:

  • You are resolving a LoginController from Simple Injector. 您正在从Simple Injector解析LoginController
  • This LoginController depends on Raven.Client.IAsyncDocumentSession . LoginController取决于Raven.Client.IAsyncDocumentSession
  • IAsyncDocumentSession is cross-wired from the ASP.NET configuration system to Simple Injector using CrossWire<IAsyncDocumentSession> . IAsyncDocumentSession从ASP.NET配置系统使用跨接线到简单注射器CrossWire<IAsyncDocumentSession>
  • IAsyncDocumentSession is registered in the ASP.NET configuration system. IAsyncDocumentSession在ASP.NET配置系统中注册。
  • The implementation of IAsyncDocumentSession that is registered in ASP.NET depends on UserStore<AppUser> . 在ASP.NET中注册的IAsyncDocumentSession的实现取决于UserStore<AppUser>
  • The ASP.NET configuration system is unable to resolve that IAsyncDocumentSession registration and throws the exception stating: ASP.NET配置系统无法解析IAsyncDocumentSession注册,并引发异常说明:

Unable to resolve service for type 'Raven.Client.IAsyncDocumentSession' while attempting to activate 'App.Identity.UserStore`1[App.Identity.AppUser]'. 尝试激活“ App.Identity.UserStore`1 [App.Identity.AppUser]”时,无法解析类型为“ Raven.Client.IAsyncDocumentSession”的服务。

In other words, you would get the same exception when you would either resolve LoginController or IAsyncDocumentSession from the ASP.NET configuration system. 换句话说,当您从ASP.NET配置系统解析LoginControllerIAsyncDocumentSession时,将遇到相同的异常。 Just call one of the following methods and you will see the same error: 只需调用以下方法之一,您将看到相同的错误:

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

I think that the IAsyncDocumentSession implementation depends on UserStore<T> or IUserStore<T> , but you haven't registered it in the ASP.NET configuration system. 我认为IAsyncDocumentSession实现取决于UserStore<T>IUserStore<T> ,但是您尚未在ASP.NET配置系统中注册它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM