0
我想使用LightInject与我当前的项目,但我一直得到一个空值。我在MVC 5中有一个Web应用程序,并附带了一个业务层。我已在我的Web项目中安装了LightInject.Mvc和LightInject.Web。我在我的商业项目中安装了LightInject.Mvc。ASP.NET MVC 5和LightInject
在业务层,我有一个compositionRoot.cs文件:
using LightInject;
using SimpleKB.Business.Commands;
namespace SimpleKB.Business
{
public class CompositeRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register<IRegisterUserCommand, RegisterUserCommand>();
serviceRegistry.Register<ICreateCategoryCommand, CreateCategoryCommand>();
serviceRegistry.Register<IUpdateCategoryCommand, UpdateCategoryCommand>();
}
}
}
接下来,在web项目,在Global.asax.cs中,我在app_start方法如下:
var serviceContainer = new ServiceContainer();
serviceContainer.RegisterFrom<Business.CompositeRoot>();
serviceContainer.EnableMvc();
最后,我的控制器代码如下所示:
public class AccountController : Controller
{
public IRegisterUserCommand RegisterUserCommand { get; set; }
[HttpGet]
[AllowAnonymous]
public ActionResult Login()
{
RegisterUserCommand.Execute();
return View();
}
}
我基本上得到在零例外代码试图使用时的控制器RegisterUserCommand。我认为LightInject在遇到接口时会自动注入代码。我错过了什么?