Ninject per session singleton?
Asked Answered
B

1

5

So I'm trying to introduce the concept of a user to my application and have got my own set of custom login routines etc. working fine. In my module, I'm binding my IUserSession to my implementation and InSingletonScope.

Now I suspect this was the case and have been able to prove that this is not the right thing to do, if I try and login with two users against the same site, I get only one set of data.

If I implement a MembershipProvider, do I avoid such a restriction. I know that if I implement a membership provider, I don't have to inject everything, but my login isn't just a username/password, how do go about logging in with additional data"?

Buhler answered 14/1, 2011 at 3:25 Comment(0)
J
11

InSingletonScope is shared across the entire application not limited per user session. Nothing you do will change that. You need to use something else like InRequestScope but that's only shared per actual request...

Try this site: http://iridescence.no/post/Session-Scoped-Bindings-With-Ninject-2.aspx

public static class NinjectSessionScopingExtention {
    public static void InSessionScope<T>(this IBindingInSyntax<T> parent) {
        parent.InScope(SessionScopeCallback);
    }

    private const string _sessionKey = "Ninject Session Scope Sync Root";

    private static object SessionScopeCallback(IContext context) {
        if (HttpContext.Current.Session[_sessionKey] == null) {
            HttpContext.Current.Session[_sessionKey] = new object();
        }

        return HttpContext.Current.Session[_sessionKey];
    }
}
Jealous answered 14/1, 2011 at 4:39 Comment(6)
Is there a better suited method to user sessions in MVC?This fits my need perfectly to get the software working, but for future reference, do you know of any examples of customized membership providers?Buhler
Off the top of my head I can't think of any ways that a customized membership provider will get around the fact that you still need to store your information in a session. Your membership provider would be doing the injection - if I understand you correctly.Jealous
This code will give you problems if HttpContext.Current is null, or if HttpContext.Current.Session is null (such as in inside an HttpModule).Headspring
You should modify the InSessionScope method to return IBindingNamedWithOrOnSyntax<T> i.e. return parent.InScope(SessionScopeCallback); to make the chaining work.Pardew
SessionScopeCallback needs to return null when HttpContext.Current is null, or it breaks mex endpoints in wcf services.Player
Note that the URL in the post no longer exists.Erda

© 2022 - 2024 — McMap. All rights reserved.