我正在尝试在SimpleInjector
应用程序上使用ASP.NET 5
和MVC
,并且遇到一个主要问题,正确连线。
我运行一个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。
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()
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()
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]'.
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()
答案 0 :(得分:1)
据我所知,这个问题与Simple Injector无关。从您的注册和异常信息我看到以下情况:
LoginController
。LoginController
取决于Raven.Client.IAsyncDocumentSession
。IAsyncDocumentSession
使用CrossWire<IAsyncDocumentSession>
从ASP.NET配置系统交叉连接到Simple Injector。IAsyncDocumentSession
已在ASP.NET配置系统中注册。IAsyncDocumentSession
的实现取决于UserStore<AppUser>
。IAsyncDocumentSession
注册并抛出异常说明:无法解决类型&#39; Raven.Client.IAsyncDocumentSession&#39;尝试激活App.Identity.UserStore`1 [App.Identity.AppUser]&#39;。
换句话说,当您从ASP.NET配置系统解析LoginController
或IAsyncDocumentSession
时,您会得到相同的异常。只需调用以下方法之一,您将看到相同的错误:
app.ApplicationServices.GetRequiredService<LoginController>();
// or
app.ApplicationServices.GetRequiredService<IAsyncDocumentSession>();
我认为IAsyncDocumentSession
实施取决于UserStore<T>
或IUserStore<T>
,但您尚未在ASP.NET配置系统中注册它。