Question

I am having trouble injecting the current loggedonuser into my service layer, I am trying something similar to code camp server but struggling to figure out why my code does not work...

My app: UI layer -> conneced to Domain Service -> connected to Repo layer...

Repo is not connected to UI, everything is checked verified and passed back from DomainService layer...

My code:

//This is declared inside my domain service

public interface IUserSession
{
    UserDTO GetCurrentUser();
}

Inside my web application I want to implement this service then inject it into my service layer so (this is where I am stuck):

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);
    }


}

What I WANT to do is: get the loggedonuser from the authentication service, however that did not work so I stubbed out the code...

I bind everything inside global.asax like:

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>();
    }

Firstly: I am getting an exception when I try and consume a service which uses IUserSession, it says please provide a default parameterless constructor for controller x, however if I remove the reference from the domain service everything works...

Service ctor:

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

Is there a better way/ simpler way to implement this?

UPATE with help from the reply below I managed to get this done, I have uploaded onto gituhub if anyone wants to know how I did it...

https://gist.github.com/1042173

Was it helpful?

Solution

I'm assuming you're using the NinjectHttpApplication since you have overridden OnApplicationStarted? If not, you should be, since it will register the Controllers for you with Ninject. If that's not happening, then you can get the error you are seeing.

Here's how I have done this in my application:

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;
        }
    }

let me know if this doesn't make sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top