本文是使用Castle Windsor进行Web API的一个很好的起点,但是如果我创建一个简单的MVC控制器呢?只有在注入没有依赖性时它才有效.
添加这个,即:
public class HomeController : Controller
{
private readonly IValuesRepository repository;
public HomeController(IValuesRepository repository)
{
this.repository = repository;
}
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
为此对象定义的无参数构造函数
有没有办法在使用Castle Windsor的同一个应用程序中使用MVC和Web API逻辑?
设置完成后DependencyResolver.SetResolver(...)的application_start,我没有注意到我的应用程序有任何改善.
如你看到的.

实现服务定位器:
internal sealed class WindsorDependencyResolver
: ServiceLocatorImplBase, IDependencyResolver
{
private readonly IWindsorContainer container;
public WindsorDependencyResolver(
IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type t)
{
return this.container.Kernel.HasComponent(t) …Run Code Online (Sandbox Code Playgroud)