NInject Singletons - 它们是在应用程序级别还是会话级别创建的?
我在 ASP.NET MVC 应用程序中使用 NInject,但我不能 100% 确定单例在创建对象上下文时如何工作。
我的问题是:
使用下面的代码将会有一个 每个用户会话的 ObjectContext 或将 有一个是共享的 整个应用程序?我想要每个用户 一次只有一种上下文, 但每个用户必须有自己的 实例。
我应该考虑 InRequestScope()
吗?
我也对 WCF 服务做了同样的事情,我认为两者的答案是相同的。
我的 Global.asax:
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Change", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected override void OnApplicationStarted()
{
// Ninject Code
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
var modules = new INinjectModule[] { new ContextModule() };
return new StandardKernel(modules);
}
public class ContextModule : NinjectModule
{
public override void Load()
{
Bind<ObjectContext>().To<ChangeRoutingEntities>().InSingletonScope();
Bind<IObjectContext>().To<ObjectContextAdapter>();
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
}
I'm using NInject in my ASP.NET MVC application and I'm not 100% sure how the singleton is working when creating my Object context.
My Question is:
Using the code below will there be one
ObjectContext per user session or will
there be one that is share for the
entire application? I want each user
to have only one context at one time,
but each user must have their own
instance.
Is InRequestScope()
something I should be considering?
I also do the same thing with a WCF service and I assume the answer will be same for both.
My Global.asax:
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Change", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected override void OnApplicationStarted()
{
// Ninject Code
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
var modules = new INinjectModule[] { new ContextModule() };
return new StandardKernel(modules);
}
public class ContextModule : NinjectModule
{
public override void Load()
{
Bind<ObjectContext>().To<ChangeRoutingEntities>().InSingletonScope();
Bind<IObjectContext>().To<ObjectContextAdapter>();
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ISingletonScope 是一个应用范围广泛的范围。
InRequestScope 仅适用于当前请求。
您需要一个会话范围。请参阅http://iridescent.no/post/Session-Scoped- Bindings-With-Ninject-2.aspx 用于实现此类作用域的方法。
ISingletonScope is an application wide scope.
InRequestScope is only for the current request.
You need a session scope. See http://iridescence.no/post/Session-Scoped-Bindings-With-Ninject-2.aspx for a way to implement this type of scope.