【问题标题】:Simple Injector - difficulty with ASP.NET 5 Identity and RavenDB简单注入器 - ASP.NET 5 Identity 和 RavenDB 的困难
【发布时间】:2015-12-28 03:28:51
【问题描述】:

我试图在 MVC 应用程序上使用 SimpleInjectorASP.NET 5,但在正确连接时遇到了一个主要问题。

我运行一个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()

【问题讨论】:

  • 请发布完整的堆栈跟踪以及异常和所有内部异常的异常消息。
  • 好的,我已经添加了所有这些。

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


【解决方案1】:

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

  • 您正在从 Simple Injector 解析 LoginController
  • 这个LoginController 依赖于Raven.Client.IAsyncDocumentSession
  • IAsyncDocumentSession 使用 CrossWire&lt;IAsyncDocumentSession&gt; 从 ASP.NET 配置系统交叉连接到 Simple Injector。
  • IAsyncDocumentSession 在 ASP.NET 配置系统中注册。
  • 在 ASP.NET 中注册的IAsyncDocumentSession 的实现依赖于UserStore&lt;AppUser&gt;
  • ASP.NET 配置系统无法解析IAsyncDocumentSession 注册并抛出异常说明:

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

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

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

我认为IAsyncDocumentSession的实现依赖于UserStore&lt;T&gt;IUserStore&lt;T&gt;,但是你还没有在ASP.NET配置系统中注册。

【讨论】:

  • 我已经尝试更改顺序,但不幸的是我仍然得到相同的结果。
  • @Ciel,我没有说要更改订单。我说 ASP.NET 配置中的注册可能丢失。所以你可能需要添加services.AddTransient&lt;IUserStore&lt;AppUser&gt;&gt;(RavenDatabase.UserStore); 或类似的东西。
  • 好吧,我做到了,现在它在 UserManager&lt;AppUser&gt; 犹豫不决,需要 IUserStore&lt;AppUser&gt;。我只是迷路了,这对我来说没有任何意义。在微软添加所有这些内置内容之前,这要容易得多。
  • @Ciel “在微软添加所有这些内置内容之前,这要容易得多。”。是的,我同意。
  • 我迷路了。我以为我不能同时使用 Identity 和第三方 IoC?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
相关资源
最近更新 更多