1

I previously used Unity dependency injection from the tutorial Dependency Injection in ASP.NET Web API 2.

However I wanted to use HierarchicalLifetimeManager so I installed Unity bootstrapper for ASP.NET Web API.

However this gave me an error I did not previously have in AccountController. From what I can tell it has something to do with the IUserStore but what I don't understand is why this error occurs after installing this NuGet. Unity has been installed and used before and I did not have to register types for AccountController.

UnityConfig.cs:

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<DbContext>(new HierarchicalLifetimeManager());

    container.RegisterType<IArticleRepository, ArticleRepository>(new HierarchicalLifetimeManager());
    container.RegisterType<ISupplierRepository, SupplierRepository>(new HierarchicalLifetimeManager());
    container.RegisterType<IContactRepository, ContactRepository>(new HierarchicalLifetimeManager());
    container.RegisterType<ICampaignRepository, CampaignRepository>(new HierarchicalLifetimeManager());
}

{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'AccountController'. 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.d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Resolution of the dependency failed, type = \"Project.Sales.Web.Controllers.AccountController\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserStore`2[HiQ.Repository.EntityFramework.Identity.BaseApplicationUser,System.Int32], 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 Project.Sales.Web.Controllers.AccountController,(none)\r\n Resolving parameter \"userManager\" of constructor Project.Sales.Web.Controllers.AccountController(Project.Sales.Web.ApplicationUserManager userManager, Microsoft.Owin.Security.ISecureDataFormat`1[[Microsoft.Owin.Security.AuthenticationTicket, Microsoft.Owin.Security, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] accessTokenFormat)\r\n Resolving Project.Sales.Web.ApplicationUserManager,(none)\r\n Resolving parameter \"store\" of constructor Project.Sales.Web.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore`2[[HiQ.Repository.EntityFramework.Identity.BaseApplicationUser, HiQ.Repository.EntityFramework, Version=0.1.2.0, Culture=neutral, PublicKeyToken=null],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] store)\r\n Resolving Microsoft.AspNet.Identity.IUserStore`2[HiQ.Repository.EntityFramework.Identity.BaseApplicationUser,System.Int32],(none)\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.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.IUserStore`2[HiQ.Repository.EntityFramework.Identity.BaseApplicationUser,System.Int32], 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.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.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.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)"}}}

4
  • if the AccountController receives a parameter of type IUserStore then you will have to register a implementation for this type too, or the unity will not be able to make an instance of the controller. Commented Jul 12, 2017 at 7:23
  • Please stop using Unity for new projects. Unity is dead github.com/unitycontainer/unity/issues. There will be no more releases for Unity. No bug fixes. No support. Commented Jul 12, 2017 at 16:45
  • @NightOwl888 What do you recommend instead? Commented Jul 14, 2017 at 22:16
  • @Ogglas - Pick one of the ~35 other ones. If you want something officially supported from MS, there is Microsoft.Extensions.DependencyInjection, but I can't attest to how good it is. Commented Jul 15, 2017 at 14:03

1 Answer 1

1

Original problem for this was that Unity tried to call the constructor with two parameters:

public AccountController(ApplicationUserManager userManager,
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
    UserManager = userManager;
    AccessTokenFormat = accessTokenFormat;
}

By adding the following line telling Unity to call the parameterless constructor everything worked again.

container.RegisterType<AccountController>(new InjectionConstructor());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.