我一直在研究一個項目,使用OnionWebApiStarterKit作爲了解我的CQRS方法的基礎。Autofac - 請求的服務(lSomeDbContext)尚未註冊
雖然在上述OnionWebApiStarterKit項目的WebAPI StudentsController例如僅注入IMediator,我決定了,它會更容易也注入我的自定義IApplicationUserManager中的AccountController構造鑑於其對用戶操作的嚴重依賴:
public AccountsController(
IMediator mediator,
IApplicationUserManager userManager)
{
// and so on...
}
但是,這將產生以下錯誤:
"The requested service 'OnionWebApiStarterKit.Data.ISchoolDbContext' has not been registered."
完整堆棧跟蹤低於:
"message": "An error has occurred.",
"exceptionMessage": "An error occurred when trying to create a controller of type 'AccountsController'. Make sure that the controller has a parameterless public constructor.",
"exceptionType": "System.InvalidOperationException",
"stackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.<>c__DisplayClass2.<System.Web.Http.Dispatcher.IHttpControllerActivator.Create>b__0()\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)\r\n at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.System.Web.Http.Dispatcher.IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.<>c__DisplayClass2.<CreateController>b__0()\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)\r\n at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "The requested service 'OnionWebApiStarterKit.Data.ISchoolDbContext' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.",
"exceptionType": "Autofac.Core.Registration.ComponentNotRegisteredException",
"stackTrace": " at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)\r\n at OnionWebApiStarterKit.Bootstrapper.IdentityModule.<>c.<Load>b__2_0(IComponentContext b) in C:\\Projects\\OnionWebApiStarterKit\\OnionWebApiStarterKit.Bootstrapper\\App_Start\\IdentityModule.cs:line 33\r\n at Autofac.RegistrationExtensions.<>c__DisplayClass10`1.<Register>b__f(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Builder.RegistrationBuilder.<>c__DisplayClass1`1.<ForDelegate>b__0(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.<Execute>b__0()\r\n at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Registration.ExternalRegistrySource.<>c__DisplayClass8.<RegistrationsFor>b__3(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)\r\n at OnionWebApiStarterKit.Bootstrapper.IdentityModule.<>c.<Load>b__2_1(IComponentContext b) in C:\\Projects\\OnionWebApiStarterKit\\OnionWebApiStarterKit.Bootstrapper\\App_Start\\IdentityModule.cs:line 37\r\n at Autofac.RegistrationExtensions.<>c__DisplayClass10`1.<Register>b__f(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Builder.RegistrationBuilder.<>c__DisplayClass1`1.<ForDelegate>b__0(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.<Execute>b__0()\r\n at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Registration.ExternalRegistrySource.<>c__DisplayClass8.<RegistrationsFor>b__3(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()\r\n at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.<Execute>b__0()\r\n at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Registration.ExternalRegistrySource.<>c__DisplayClass8.<RegistrationsFor>b__3(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()\r\n at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Registration.ExternalRegistrySource.<>c__DisplayClass8.<RegistrationsFor>b__3(IComponentContext c, IEnumerable`1 p)\r\n at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters)\r\n at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)\r\n at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}
我從我的自舉啓動類註冊我的依賴關係:
IocConfig.RegisterDependencies(app);
我確認它得到通過設置斷點命中。下面是我註冊:
public static void RegisterDependencies(IAppBuilder app)
{
DbContextScopeExtensionConfig.Setup();
// Get your HttpConfiguration. In OWIN, you'll create one
// rather than using GlobalConfiguration.
var config = new HttpConfiguration();
// register Auto Mapper
AutoMapperConfig.Initialize();
// register api routing.
WebApiConfig.Register(config);
// Run optional steps, like registering filters,
// per-controller-type services, etc.
var builder = new ContainerBuilder();
// Register Web API controller in executing assembly.
builder.RegisterApiControllers(Assembly.Load("OnionWebApiStarterKit.WebApi"));
//Helper nuget for managing the DbContext lifetime in Entity Framework. Please see: http://mehdi.me/ambient-dbcontext-in-ef6/
builder.RegisterType<DbContextScopeFactory>().As<IDbContextScopeFactory>().SingleInstance();
builder.RegisterType<AmbientDbContextLocator>().As<IAmbientDbContextLocator>().SingleInstance();
// Registers our IMediator (abstraction for observer pattern, which lets us use CQRS)
builder.RegisterModule(new MediatorModule(Assembly.Load("OnionWebApiStarterKit.Services")));
// Registers our Fluent Validations that we use on our Models
builder.RegisterModule(new FluentValidationModule(Assembly.Load("OnionWebApiStarterKit.WebApi"), Assembly.Load("OnionWebApiStarterKit.Services")));
// Registers our AutoMapper Profiles
builder.RegisterModule(new AutoMapperModule(Assembly.Load("OnionWebApiStarterKit.WebApi"), Assembly.Load("OnionWebApiStarterKit.Services")));
// Registers our ASP.NET Identity custom classes.
builder.RegisterModule(new IdentityModule(Assembly.Load("OnionWebApiStarterKit.Data"), Assembly.Load("OnionWebApiStarterKit.Core")));
// Set the dependency resolver to be Autofac.
var container = builder.Build();
// helps view registered autofac dependencies.
// container.IsRegistered();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// Register the Autofac middleware FIRST, then the Autofac Web API middleware,
// and finally the standard Web API middleware.
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseCors(CorsOptions.AllowAll); // Enables crossdomain requests
app.UseWebApi(config);
}
在上面的文件,下面的模塊處理身份登記,這IApplicationUserManager是部分:
// Registers our ASP.NET Identity custom classes.
builder.RegisterModule(new IdentityModule(Assembly.Load("OnionWebApiStarterKit.Data"), Assembly.Load("OnionWebApiStarterKit.Core")));
這裏是怎麼我的身份的東西被註冊:
public IdentityModule(params System.Reflection.Assembly[] assembliesToScan)
: base()
{
_assembliesToScan = assembliesToScan;
}
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType(typeof(ApplicationUserManager)).As(typeof(IApplicationUserManager)).InstancePerRequest();
builder.RegisterType(typeof(ApplicationRoleManager)).As(typeof(IApplicationRoleManager)).InstancePerRequest();
builder.RegisterType(typeof(ApplicationIdentityUser)).As(typeof(IUser<int>)).InstancePerRequest();
builder.Register(b => b.Resolve<ISchoolDbContext>() as DbContext).InstancePerRequest();
builder.Register(b =>
{
var manager = IdentityFactory.CreateUserManager(b.Resolve<DbContext>());
if (Startup.DataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationIdentityUser, int>(
Startup.DataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}).InstancePerRequest();
builder.Register(b => IdentityFactory.CreateRoleManager(b.Resolve<DbContext>())).InstancePerRequest();
builder.Register(b => HttpContext.Current.Request.GetOwinContext().Authentication).InstancePerRequest();
}
正如您所看到的,ISchoolDbContext明確註冊在此行:
builder.Register(b => b.Resolve<ISchoolDbContext>() as DbContext).InstancePerRequest();
這是Autofac的堆棧跟蹤抱怨同一線路:
at OnionWebApiStarterKit.Bootstrapper.IdentityModule.<>c.b__2_0(IComponentContext b) in C:\Projects\OnionWebApiStarterKit\OnionWebApiStarterKit.Bootstrapper\App_Start\IdentityModule.cs:line 33
我有點難倒,爲什麼體內注射IApplicationUserManager告訴我,我還沒有註冊我的ISchoolDbContext?
謝謝。
UPDATE
它看起來像我沒有掃描我的傳入組件的IdentityModule。 所以,我已經更新了我的代碼相應:
// Register our UserManager
builder.RegisterAssemblyTypes(_assembliesToScan)
.As<IApplicationUserManager>().InstancePerRequest();
//// Register our RoleManager
builder.RegisterAssemblyTypes(_assembliesToScan)
.As<IApplicationRoleManager>().InstancePerRequest();
builder.RegisterAssemblyTypes(_assembliesToScan)
.As<IUser<int>>().InstancePerRequest();
謝謝,我編輯並添加了我的註冊文件。我正在通過大會註冊。正如你所看到的,數據程序集正在被註冊。 –
排序程序集問題後,我將DBContext添加到我的IdentityModule註冊中,並且解決了此問題。所以,我會相信你的答案,因爲這是你最初的建議。謝謝你的幫助。 –
很高興幫助! – Vinoth