MVC5 Identity 2.0 Inyección de dependencia con Unity

I'm having trouble with Unity while trying to use DI with Identity 2.0. My project is MVC5 Wep Api2 REST services and AngularJS.

That's what I've tried so far: (note that: 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")))

I've been asked to post the controller's constructor. Here it is:

public GroupsAsyncController(IGroupAsyncService service)
{
    _service = service;
}

That's the complete stacktrace:

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)"}}}

That's where I'm stuck. Thanks in advance for your help!

Update: basically I don't know how to use Unity DI on: new UserManager(new UserStore(new MySecurityContext())); or new RoleManager(new RoleStore(new MySecurityContext()));

preguntado el 28 de mayo de 14 a las 13:05

What is the constructor on the GroupsAsyncController? -

Hi David. Updated the post with the ApiController consructor. Thanks -

This post helped me a lot tech.trailmax.info/2014/09/… -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.