2011-06-16 24 views
2

我有麻煩注入當前loggedonuser到我的服務層,我想嘗試類似於代碼營服務器,但努力弄清楚爲什麼我的代碼不工作...使用Ninject注入當前用戶到我的域名

我的應用程序:UI層 - > conneced到域名服務 - >連接到回購層...

回購未連接到用戶界面,一切都進行檢查覈實,並通過從的DomainService層回來......

我的代碼:

//這是宣告我的域名服務

public interface IUserSession 
{ 
    UserDTO GetCurrentUser(); 
} 

Inside無我的web應用程序我想實現這個服務,然後將其注入我的服務層,(這是我在哪裏卡住):

public class UserSession : IUserSession 
{ 
    //private IAuthorizationService _auth; 
    public UserSession()//IAuthorizationService _auth) 
    { 
     //this._auth = _auth; 
    } 

    public UserDTO GetCurrentUser() 
    { 
     var identity = HttpContext.Current.User.Identity; 
     if (!identity.IsAuthenticated) 
     { 
      return null; 
     } 
     return null; 
     //return _auth.GetLoggedOnUser(identity.Name); 
    } 


} 

我想要做的是:得到認證服務的loggedonuser,但沒有工作,所以我掐滅代碼...

我綁定內Global.asax的一切都像:

protected override void OnApplicationStarted() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     // hand over control to NInject to register all controllers 
     RegisterRoutes(RouteTable.Routes); 
     Container.Get<ILoggingService>().Info("Application started"); 
     //here is the binding... 
     Container.Bind<IUserSession>().To<UserSession>(); 
    } 

首先:當我嘗試使用使用IUserSession的服務時發生異常,它表示請爲控制器x提供一個默認的無參數構造函數,但是如果我從域服務中刪除引用,則一切正常......

服務構造函數:

private IReadOnlyRepository _repo; 
    private IUserSession _session; 
    public ActivityService(IReadOnlyRepository repo, IUserSession _session) 
    { 
     this._repo = repo; 
     this._session = _session; 
    } 

有沒有實現這一種更好的方式/更簡單的方法?

UPATE從下面我的回覆幫助成功地完成這件事,我已經上傳到gituhub如果有人想知道我是怎麼做的?

https://gist.github.com/1042173

回答

0

我假設你使用NinjectHttpApplication,因爲你已經覆蓋了OnApplicationStarted?如果沒有,您應該是,因爲它會爲Ninject註冊控制器。如果這沒有發生,那麼你可以得到你所看到的錯誤。

這裏是我如何在我的應用程序做到了這一點:

ControllerBase:

[Inject] 
    public IMembershipProvider Membership { get; set; } 

    public Member CurrentMember 
    { 
     get { return Membership.GetCurrentUser() as Member; } 
    } 

IMembershipProvider:

public Member GetCurrentUser() 
    { 
     if (string.IsNullOrEmpty(authentication.CurrentUserName)) 
      return null; 

     if (_currentUser == null) 
     { 
      _currentUser = repository.GetByName(authentication.CurrentUserName); 
     } 
     return _currentUser; 
    } 
    private Member _currentUser; 

IAuthenticationProvider:

public string CurrentUserName 
    { 
     get 
     { 
      var context = HttpContext.Current; 
      if (context != null && context.User != null && context.User.Identity != null) 
       return HttpContext.Current.User.Identity.Name; 
      else 
       return null; 
     } 
    } 

讓我知道這是否沒有意義。

+0

感謝您的回覆,我正在使用應用程序替代來連接ninject,無論如何...我的問題的主要部分 - 我將如何將IMembershipProvider或IAuthenticationProvider連接到我的域圖層,這是一個單獨的類庫,它不參考我的Web應用程序? – Haroon 2011-06-19 22:36:55

+0

我在我的Core庫中有'IMembershipProvider',它沒有引用Web。我的控制器和服務也在Core中,並且在構造函數中注入了'IMembershipProvider'。當'NinjectControllerFactory'創建控制器時,它會向其中注入'IMembershipProvider'。然後,我的Web庫中只有一個Ninject模塊,它將接口綁定到具體類型「MembershipProvider」。所以你至少需要將接口放在與使用它的類相同的程序集中。 – 2011-06-20 13:55:45

+0

那就是我所做的,我有我的領域層中定義的接口,然後我在我的網絡應用程序內實現它,我用ninject手動綁定它在應用程序啓動,我想我會嘗試和綁定它內核Kernal,看看是否那工作... – Haroon 2011-06-22 10:53:45