Ninject per session singleton?

前端 未结 1 1658
轻奢々
轻奢々 2021-02-06 14:49

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 IUserSessio

相关标签:
1条回答
  • 2021-02-06 15:33

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题