在尝试将DI与Identity 2.0一起使用时,我遇到了Unity问题。我的项目是MVC5 Wep Api2 REST服务和AngularJS。
这是我迄今为止所尝试的内容:(请注意:MyExtendedGroup : IdentityRole)
.RegisterType<MySecurityContext>("MySecurityCtx", new PerResolveLifetimeManager())
.RegisterType<IRoleStore<MyExtendedGroup, string>, RoleStore<MyExtendedGroup, string, IdentityUserRole>>("MyRolesStore", new InjectionConstructor(new ResolvedParameter<MySecurityContext>("MySecurityCtx")))
.RegisterType<IGroupAsyncService, GroupAsyncService>("groupAsyncService", new InjectionConstructor(new ResolvedParameter<IRoleStore<MyExtendedGroup>>("MyRoleStore")))
.RegisterType<GroupsAsyncController>(new InjectionConstructor(new ResolvedParameter<IGroupAsyncService>("groupAsyncService")))
我被要求发布控制器的构造函数。这是:
public GroupsAsyncController(IGroupAsyncService service)
{
_service = service;
}
这是完整的堆栈跟踪:
HTTP/1.1 500 Internal Server Error
Content-Length: 5729
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?YzpcZGV2XENhbWJ1c2FBbmd1bGFyXENhbWJ1c2EuT3dpbi5Bbmd1bGFyXGFwaVxHcm91cHNBc3luYw==?=
X-Powered-By: ASP.NET
Date: Wed, 28 May 2014 12:32:56 GMT
{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'GroupsAsyncController'. 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.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Resolution of the dependency failed, type = \"My.Owin.Angular.Controllers.Configurazione.GroupsAsyncController\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IRoleStore`1[MySecurity.Entities.Models.MyExtendedGroup], is an interface and cannot be constructed. Are you missing a type mapping?\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving My.Owin.Angular.Controllers.Configurazione.GroupsAsyncController,(none)\r\n Resolving parameter \"service\" of constructor My.Owin.Angular.Controllers.Configurazione.GroupsAsyncController(MySecurity.Service.Interfaces.IGroupAsyncService service)\r\n Resolving MySecurity.Service.GroupAsyncService,groupAsyncService (mapped from MySecurity.Service.Interfaces.IGroupAsyncService, groupAsyncService)\r\n Resolving parameter \"roleStore\" of constructor MySecurity.Service.GroupAsyncService(Microsoft.AspNet.Identity.IRoleStore`1[[MySecurity.Entities.Models.MyExtendedGroup, MySecurity.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] roleStore)\r\n Resolving Microsoft.AspNet.Identity.IRoleStore`1[MySecurity.Entities.Models.MyExtendedGroup],MyRoleStore\r\n","ExceptionType":"Microsoft.Practices.Unity.ResolutionFailedException","StackTrace":" at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)\r\n at Microsoft.Practices.Unity.WebApi.UnityDependencyResolver.SharedDependencyScope.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)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"The current type, Microsoft.AspNet.Identity.IRoleStore`1[MySecurity.Entities.Models.MyExtendedGroup], is an interface and cannot be constructed. Are you missing a type mapping?","ExceptionType":"System.InvalidOperationException","StackTrace":" at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext )\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext )\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext )\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)"}}}
我被困的地方。在此先感谢您的帮助!
更新:基本上我不知道如何使用Unity DI: new UserManager(new UserStore(new MySecurityContext())); 要么 new RoleManager(new RoleStore(new MySecurityContext()));