Ninject per session singleton?

走远了吗. 提交于 2019-12-03 04:41:28

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];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!