我在Web api 2.0中使用Microsoft Unity进行依赖注入。它运作正常;但有时它会抛出依赖失败错误。已发布端口以及本地项目中的相同问题。我在DDD概念中使用Repository模式...我在unit.config中的依赖代码如下所示。
var container = new UnityContainer();
container.RegisterType<ILogger, Log4NetLogger>();
container.RegisterType<NHibernateFactory, NHibernateFactory>();
container.RegisterType<INhibernateUnitOfWork, NHUnitOfWork>();
container.RegisterType(typeof(IRepository<,>), typeof(NHRepository<,>));
container.RegisterTypes(AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.Hierarchical);
GlobalConfiguration.Configuration.DependencyResolver =
new UnityDependencyResolver(container);
我收到以下错误
2017-03-24 09:47:12,426 [12] ERROR Main - System.InvalidOperationException:尝试创建“ProductController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。 ---&GT; Microsoft.Practices.Unity.ResolutionFailedException:依赖项的解析失败,type =“Solver.ERP.WebAPI.HMS.Controllers.V1.Masters.ProductController”,name =“(none)”。 在解决时发生异常。
异常是:InvalidOperationException - 当前类型Solver.ERP.Application.Services.IProductService是一个接口,无法构造。你错过了类型映射吗?
在例外时,容器是:
解决Solver.ERP.WebAPI.HMS.Controllers.V1.Masters.ProductController,(无)
解析构造函数Solver.ERP.WebAPI.HMS.Controllers.V1.Masters.ProductController的参数“PService”(Solver.ERP.Application.Services.IProductService PService,Solver.ERP.Infrastructure.Utility.Logging.ILogger Nlogger)
解决Solver.ERP.Application.Services.IProductService,(无)
---&GT; System.InvalidOperationException:当前类型Solver.ERP.Application.Services.IProductService是一个接口,无法构造。你错过了类型映射吗?
在Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)
在lambda_method(Closure,IBuilderContext)
在Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext。&lt;&gt; c__DisplayClass1.b__0(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)
在Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)
在lambda_method(Closure,IBuilderContext)
在Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext。&lt;&gt; c__DisplayClass1.b__0(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object existing,String name,IEnumerable 1 resolverOverrides)
--- End of inner exception stack trace ---
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable
1 resolverOverrides)
在Microsoft.Practices.Unity.UnityContainer.Resolve(Type t,String name,ResolverOverride [] resolverOverrides)
在Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer容器,Type t,ResolverOverride []覆盖)
在Unity.WebApi.UnityDependencyScope.GetService(类型serviceType)
在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage请求,类型controllerType,Func`1&amp; activator)
在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)
---内部异常堆栈跟踪结束---
在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)
在System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage请求)
在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
下面的代码用于将类映射到接口
container.RegisterTypes(AllClasses.FromLoadedAssemblies(),WithMappings.FromMatchingInterface,WithName.Default,WithLifetime.Hierarchical);
答案 0 :(得分:0)
查看您提供的错误消息。
错误Main - System.InvalidOperationException:尝试创建“ProductController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。 ---&GT;
错误很常见。你错过了实例化你的控制器。
我不确定使用Unity
块的语法。但我知道CastleWindsor
DI容器。
请在班级中添加相应的Unity
配置。
container.Register(Classes.FromThisAssembly()
.BasedOn(typeof(Controller))
.If(t => t.Name.EndsWith("Controller"))
.LifestyleTransient());
//LifestylePerWebRequest or per thread decide as per your need.
希望有所帮助:)