asp.net-mvc-3 – MVC3,Unity Framework和Per Session Lifetime
发布时间:2020-12-16 10:01:43 所属栏目:asp.Net 来源:网络整理
导读:简单来说,我尝试在我的MVC3项目中使用Http Session为Unity框架创建Lifetime管理器.我的终身经理的示例实现是: public class UnityPerSessionLifetimeManager : LifetimeManager { private string sessionKey; private HttpContext ctx; public UnityPerSess
简单来说,我尝试在我的MVC3项目中使用Http Session为Unity框架创建Lifetime管理器.我的终身经理的示例实现是:
public class UnityPerSessionLifetimeManager : LifetimeManager { private string sessionKey; private HttpContext ctx; public UnityPerSessionLifetimeManager(string sessionKey) { this.sessionKey = sessionKey; this.ctx = HttpContext.Current; } public override object GetValue() { return this.ctx.Session[this.sessionKey]; } public override void RemoveValue() { this.ctx.Items.Remove(this.sessionKey); } public override void SetValue(object newValue) { this.ctx.Session[this.sessionKey] = newValue; } } 在我的global.asax.cs中,我用自己的UnityControllerFactory替换了默认控制器工厂 public class UnityControllerFactory : DefaultControllerFactory { private IUnityContainer container; public UnityControllerFactory(IUnityContainer container) { this.container = container; this.RegisterServices(); } protected override IController GetControllerInstance(RequestContext context,Type controllerType) { if (controllerType != null) { return this.container.Resolve(controllerType) as IController; } return null; } private void RegisterServices() { this.container.RegisterType<IMyType,MyImpl>(new UnityPerSessionLifetimeManager("SomeKey")); } } } 我在UnityPerSessionLifetimeManager类的每个函数上设置了断点,我注意到当控制器工厂尝试解决我的控制器的依赖关系时,HttpContext.Session实际上是null,因此代码无法从会话检索或保存到会话. 知道为什么会话在这个阶段为空? 解决方法
我的错误,我应该改变UnityPerSessionLifetimeManager类的代码
public class UnityPerSessionLifetimeManager : LifetimeManager { private string sessionKey; public UnityPerSessionLifetimeManager(string sessionKey) { this.sessionKey = sessionKey; } public override object GetValue() { return HttpContext.Current.Session[this.sessionKey]; } public override void RemoveValue() { HttpContext.Current.Session.Remove(this.sessionKey); } public override void SetValue(object newValue) { HttpContext.Current.Session[this.sessionKey] = newValue; } } 因为当调用构造函数来注册类型时,会话状态尚未就绪,我已经将该时间的http上下文分配给变量.但在以后的Get / Set函数中,会话状态已准备就绪. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 在单独的配置文件中移出web.config
- .net – CS0012:类型’System.Data.Linq.DataContext’在未
- ASP.NET随机丢失会话值
- 你可以在ASP.NET MVC的页面上有两个表单吗?
- asp.net-mvc – ASP.NET MVC会话超时,绝对还是滑动?
- IIS“SendResponse”状态下的请求已停留很长时间;慢IIS 7.5
- asp.net-mvc – ASP.NET MVC:与非移动视图一起使用的移动布
- ASP.NET站点缓慢,直到回收
- 如何在ASP.NET Core MVC 1.0中创建和管理插件?
- asp.net – 即使在IIS的web.config中使用标签后,也会出现重
推荐文章
站长推荐
- 从主页面注入的客户端脚本块中回弹ASP.NET后运行
- ASP.NET Core 2.0 in Docker on Wind
- asp.net – FormsAuthentication.SetAuthCookie(
- asp.net-core-mvc – IN MVC6返回Json(行,JsonRe
- 如何编写没有Try/Catch的程序
- 在asp.net mvc中添加JavaScript文件
- asp.net-mvc – 如何动态添加到ASP.NET MVC Rout
- asp.net – MVC Controller返回Chunked内容编码
- asp.net – .Net ADO连接类
- asp.net – AntFarm反模式 – 避免的策略,解毒帮
热点阅读