我之前使用过教程Dependency Injection in ASP.NET Web API 2中的Unity依赖注入。
但我想使用HierarchicalLifetimeManager
,所以我安装了Unity bootstrapper for ASP.NET Web API。
然而,这给了我一个我之前没有AccountController
的错误。据我所知,它与IUserStore有关,但我不明白为什么在安装这个NuGet后会出现这个错误。 Unity已经安装和使用过,我没有注册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());
}
{&#34;消息&#34;:&#34;发生错误。&#34;,&#34; ExceptionMessage&#34;:&#34;错误 尝试创建类型的控制器时发生 &#39;的AccountController&#39 ;.确保控制器具有无参数 上市 。构造&#34;&#34; ExceptionType&#34;:&#34; System.InvalidOperationException&#34;&#34;堆栈跟踪&#34;:&#34; 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)\ r \ n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage 请求)\ r \ n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()&#34;&#34;的InnerException&#34; {&#34;消息&#34;:&#34;安 错误已经发生。&#34;,&#34; ExceptionMessage&#34;:&#34;解决依赖性 失败,输入= \&#34; Project.Sales.Web.Controllers.AccountController \&#34;, name = \&#34;(none)\&#34;。\ r \ n:while时发生异常 解析。\ r \ n \ nException是:InvalidOperationException - 当前 类型, Microsoft.AspNet.Identity.IUserStore`2 [HiQ.Repository.EntityFramework.Identity.BaseApplicationUser,System.Int32] 是一个接口,无法构造。你错过了一个类型吗? 映射吗?\ r \ n -------------------------------------------- --- \ r \ nAtt 异常时间,容器是:\ r \ n \ r \ n解析 Project.Sales.Web.Controllers.AccountController,(无)\ r \ n解析 参数\&#34; userManager \&#34;构造函数 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解析 Project.Sales.Web.ApplicationUserManager,(无)\ r \ n解析 参数\&#34; store \&#34;构造函数 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 解决 Microsoft.AspNet.Identity.IUserStore`2 [HiQ.Repository.EntityFramework.Identity.BaseApplicationUser,System.Int32],(无)\ r \ n&#34;&#34; ExceptionType&#34;:&#34;微软.Practices.Unity.ResolutionFailedException&#34;&#34;堆栈跟踪&#34;:&#34; 在Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object 现有的,String名称,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 []覆盖)\ r \ n at Microsoft.Practices.Unity.WebApi.UnityDependencyResolver.SharedDependencyScope.GetService(类型 serviceType)\ r \ n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,输入controllerType,Func`1&amp;激活者)\ r \ n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)&#34;,&#34; InnerException&#34;:{&#34; Message&#34;:&#34;错误有 发生了。&#34;,&#34; ExceptionMessage&#34;:&#34;当前类型, Microsoft.AspNet.Identity.IUserStore`2 [HiQ.Repository.EntityFramework.Identity.BaseApplicationUser,System.Int32] 是一个接口,无法构造。你错过了一个类型吗? ?映射&#34;&#34; ExceptionType&#34;:&#34; System.InvalidOperationException&#34;&#34;堆栈跟踪&#34;:&#34; 在 Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext 上下文\ r \ n在lambda_method(Closure,IBuilderContext)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext&LT;&GT; c__DisplayClass1.b__0(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\ r \ n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext 上下文\ r \ n在lambda_method(Closure,IBuilderContext)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext&LT;&GT; c__DisplayClass1.b__0(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\ r \ n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext 上下文\ r \ n在lambda_method(Closure,IBuilderContext)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext&LT;&GT; c__DisplayClass1.b__0(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文)\ r \ n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object 现有的,字符串名称,IEnumerable`1 resolverOverrides)&#34;}}}
答案 0 :(得分: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());