Unity,向控制器注入实例会给出看似无关的异常

时间:2014-06-10 12:04:43

标签: c# unity-container asp.net-mvc-5.1

这是我想要做的(将接口传递给控制器​​)​​:

public class TestController : Controller
{
    // GET: Test
    [HttpGet]
    public ActionResult Index(ITestService service)
    {
        var test = new TestModel();
        test.Greeting = "yo" + service.GetString();
        test.Name = "nils";

        return View(test);

    }
}

这是我在Application_Start()中放入Global.asax.cs以尝试使其工作的原因:

        // Create a new Unity dependency injection container
        var unity = new UnityContainer();


        unity.RegisterType<ITestService,TestService>();

        // Finally, override the default dependency resolver with Unity
        DependencyResolver.SetResolver(new IoCContainer(unity));

如您所见,我还创建了一个IoCContainer类,如下所示:

public class IoCContainer : IDependencyResolver
{
    private readonly IUnityContainer _container;
    public IoCContainer(IUnityContainer container)
    {
        _container = container;
    }

    public object GetService(Type serviceType)
    {
        if (_container.IsRegistered(serviceType))
            return _container.Resolve(serviceType);

        return null;

    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        if (_container.IsRegistered(serviceType))
            return _container.ResolveAll(serviceType);

        return new List<object>();
    }

    public void Dispose()
    {
        _container.Dispose();
    }
}

当我尝试访问"http://humptidumptiurl/Test"时,它告诉我:

A public action method 'Login' was not found on controller 'Companyname.Product.Web.Controllers.TestController'.

现在......我认为它应该解决ITestService ..不用担心完全不同的控制器?其他不使用Unity的控制器,按照他们一直以来的方式工作....

关于如何实现我想要的解决方案的输入将非常适合

修改

谢谢!当然它通过构造函数注入...我应该想到这个......但现在它给了我这个错误信息:

{"An error occurred when trying to create a controller of type 'Stimline.Xplorer.Web.Controllers.TestController'. Make sure that the controller has a parameterless public constructor."}

已编辑的testController:

public class TestController : Controller
{
    private readonly ITestService _testService;

    public TestController(ITestService service)
    {
        _testService = service;
    }

    // GET: Test
    [HttpGet]
    public ActionResult Index()
    {
        var test = new TestModel();
        test.Greeting = "yo" + _testService.GetString();
        test.Name = "nils";

        return View(test);
    }
}

2 个答案:

答案 0 :(得分:2)

您将依赖项注入您的操作方法。

以这种方式使用IDependencyResolver时,您倾向于将依赖项注入构造函数中。

尝试将控制器更改为如下所示:

public class TestController : Controller
{
    private readonly ITestService service;

    public TestController(ITestService service)
    {
        this.service = service;
    }

    // GET: Test
    [HttpGet]
    public ActionResult Index()
    {
        var test = new TestModel();
        test.Greeting = "yo";
        test.Name = "nils";

        // TODO do something with ITestService
        // this.service.DoSomethingCool()

        return View(test);
    } 

}

答案 1 :(得分:1)

声明如下:

public class TestController : Controller
    {

        private ITestService service;
        public TestController(ITestService service)
        {
            this.service = service;
        }

        // GET: Test
        [HttpGet]
        public ActionResult Index()
        {
            var test = new TestModel();
            test.Greeting = "yo";
            test.Name = "nils";

            return View(test);
        }
    }

请在构造函数中注入依赖项。你错误地将它传递给你的行动方法。