2016-10-20 26 views
0

私は現在のプロジェクトでLightInjectを使用しようとしていますが、引き続きnull値を取得しています。 MVC 5にはWebアプリケーションがあり、ビジネス層にはそのアプリケーションが付属しています。私はLightInject.MvcLightInject.WebをWebプロジェクトにインストールしました。私はビジネスプロジェクトにLightInject.Mvcをインストールしました。ASP.NET MVC 5とLightInject

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メソッドで次があります:

ビジネス層で

は、私がcompositionRoot.csファイルを持っています
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(); 
     } 
    } 

私は基本的にはnull例外を取得しますコードが使用しようとしているときのコントローラRegisterUserCommand。私はLightInjectがインターフェイスに遭遇したときに自動的にコードを注入すると仮定しました。私は何が欠けていますか?

答えて

0

私はLigtInject.Mvcプロジェクトのコードを見て、それを理解しました。基本的に、私のglobal.asax.csのコードは以下のようになります:

var serviceContainer = new ServiceContainer(); 
serviceContainer.RegisterFrom<Business.CompositeRoot>(); 
serviceContainer.RegisterControllers(); 
serviceContainer.EnableMvc()