2014-03-25 33 views
0

我的解決方案中有兩個項目:BusinessLibrary和Web(asp.net MVC 4)。我正在使用autofac進行DI,看起來在一些不活動之後,控制器中的注入對象變爲空。我做錯了autofac中的生命週期配置嗎?Autofac:閒置後我得到「對象引用未設置爲對象的實例」

Web項目

的AccountController從BusinessLibrary這樣引用服務類:

public class AccountController : Controller 
{ 
    private readonly IAccountService accountService; 

    public AccountController(){} 

    public AccountController(IAccountService accountService) 
    { 
     this.accountService = accountService; 
    } 

    [HttpPost] 
    public ActionResult Logon(User model) 
    { 
     if (accountService.AuthenticateUser(model.Username, model.Password)) 
     { 
      //... some logic here 
     } 
    } 
} 

這是Global.asax.cs中:在IoCConfig

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     //some other logic here 
     IoCConfig.RegisterDependencies(); 
    } 
} 

最後,autofac配置:

public static void RegisterDependencies() 
    { 
     var builder = new ContainerBuilder(); 

     // register services 
     builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()) 
      .Where(t => t.Name.EndsWith("Service")) 
      .AsImplementedInterfaces() 
      .InstancePerHttpRequest(); 

     // register controllers 
     builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerHttpRequest(); 

     var container = builder.Build(); 
     DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 
    } 

BusinessLibrary項目

帳戶服務等級:

public interface IAccountService 
{ 
    bool AuthenticateUser(string username, string password); 
} 

public class AccountService : IAccountService 
{ 
    private readonly IAuthenticationService authenticationService; 

    public AccountService(IAuthenticationService authenticationService) 
    { 
     this.authenticationService = authenticationService; 
    } 

    public bool AuthenticateUser(string username, string password) 
    { 
     //some logic that uses authenticationService 
    } 
} 

的AccountService接受IAuthenticationService的實例,這是我從一個被引用的DLL,它做了一些AD認證邏輯得到。

問題

當我在VS2012重建後,我的本地機器(IIS 7.5)上運行它,我可以登錄,一切工作正常。 現在,如果我將該選項卡打開並執行其他操作,則在我進入頁面並嘗試執行任何操作後,出現異常:對象引用未設置爲對象實例。這裏的帳戶服務是空的在這裏:

public class AccountController : Controller 
{ 
    // constructors... 
    [HttpPost] 
    public ActionResult Logon(User model) 
    { 
     if (accountService.AuthenticateUser(model.Username, model.Password)) // accountService is null 

任何幫助將不勝感激。

PS。我在SO上檢查了類似的問題,但是這些都是在私有構造函數或web.config文件中的雙重autofac配置。

+0

請問,如果你刪除'builder.RegisterAssemblyTypes它的工作...'和手動註冊這樣'builder.RegisterType <帳戶服務>()。作爲()。InstancePerHttpRequest();' – Win

+0

@Win,我可以在BusinessLibrary中註冊'AccountService as IAccountService',但我無法註冊'AuthenticationService as IAuthenticationService',它在BusinessLibrary中引用。但是你的回答實際上讓我想到Asp.web不應該負責註冊BL使用的類。我會嘗試重新設計這種關係,因爲這裏有一些根本性的錯誤。 – mai

回答

0

根據您的最新評論,您的網站項目需要引用所有服務項目。

基本上,你應該可以這樣註冊 -

builder.RegisterType<AccountService>().As<IAccountService>() 
    .InstancePerHttpReq‌​uest(); 
builder.RegisterType<AuthenticationService>().As<IAuthenticationService>() 
    .InstancePerHttpReq‌​uest(); 
+0

謝謝贏!贏得FTW!儘管我希望autofac自動完成所有註冊,但似乎這是唯一可行的解​​決方案。我還將ADAuthenticationService引用添加到Web項目中,以便正確執行註冊。 – mai

相關問題