我正在一个项目中使用 ApiControllers 和 Controllers(不是 webapi)和 Castle(来自 Nuget)
internal class WebWindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component
.For<RepositoryFactories>()
.ImplementedBy<RepositoryFactories>()
.LifestyleSingleton());
container.Register(Component
.For<IRepositoryProvider>()
.ImplementedBy<RepositoryProvider>()
.LifestylePerWebRequest());
container.Register(Component
.For<IProjUow>()
.ImplementedBy<ProjUow>()
.LifestylePerWebRequest());
container.Register(Classes
.FromAssemblyContaining<Api.CategoriesController>()
.BasedOn<IHttpController>()
.If(t => t.Name.EndsWith("Controller"))
.LifestylePerWebRequest());
container.Register(Classes
.FromAssemblyContaining<CategoriesController>()
.BasedOn<IController>()
.If(t => t.Name.EndsWith("Controller"))
.LifestylePerWebRequest());
}
}
这在 global.asax (Application_Start)
var container = new WindsorContainer().Install(new WebWindsorInstaller());
GlobalConfiguration.Configuration.DependencyResolver = new WindsorDependencyResolver(container);
这是 CategoriesController 的 Controller 构造函数
public class CategoriesController : ControllerBase
{
public CategoriesController(IProjUow uow)
{
Uow = uow;
}
}
而ControllerBase继承自Controller
ApiController 定义如下:
public abstract class ApiControllerBase : ApiController
{
protected IProjUow Uow { get; set; }
}
public class CategoriesController : ApiControllerBase
{
public CategoriesController(IProjUow uow)
{
Uow = uow;
}
}
ApiController 工作正常,但另一个说:
没有为此对象定义无参数构造函数。
为什么?
提前致谢!吉列尔莫。