小编lsf*_*era的帖子

Castle Windsor ASP.NET MVC 4和Web API相同的应用程序.只有MVC控制器没有依赖关系?

本文是使用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,我没有注意到我的应用程序有任何改善.

如你看到的.

WebApiScopedLifetimeDependencyResolverSample.Windsor.WindsorDependencyResolver类型似乎没有实现

实现服务定位器:

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)

c# asp.net castle-windsor asp.net-mvc-4 asp.net-web-api

5
推荐指数
1
解决办法
2986
查看次数