使用ASP.NET会话终身管理(统一) [英] Using ASP.NET Session for Lifetime Management (Unity)

查看:130
本文介绍了使用ASP.NET会话终身管理(统一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的统一管理自定义用户类实例的生命周期考虑。我计划用自定义ASP.NET会话管理器扩展LifetimeManager。我希望能够做的是存储和检索用户对象当前登录从我的自定义类,并有统一从会话对象获取用户的实例在ASP.NET中,或(在Win32项目时)检索静态或从当前线程

I am considering using Unity to manage the lifetime of a custom user class instance. I am planning on extending the LifetimeManager with a custom ASP.NET session manager. What I want to be able to do is store and retrieve the currently logged in user object from my custom classes, and have Unity get the instance of User from the session object in ASP.NET, or (when in a Win32 project) retrieve it statically or from the current thread.

到目前为止,我的最好的解决方法是建立在启动时我统一容器的静态实例,并使用Resolve方法,从我的每一个班得到我的User对象。然而,这似乎在我的其他类来创建统一容器上的依赖。什么是实现这一目标的更团结的方式?我希望能够从任何类读/替换当前的用户实例。

So far my best solution is to create a static instance of my Unity container on startup, and use the Resolve method to get my User object from each of my classes. However, this seems to create a dependency on the unity container in my other classes. What is the more "Unity" way of accomplishing this goal? I would like to be able to read/replace the current User instance from any class.

推荐答案

您会得到最好的邦与团结与ASP.Net MVC,而不是普通的老ASP.Net项目中使用。 ASP.Net MVC允许你使用一个容器像统一管理用户对象,控制器,模型等。如果可能的话,使用MVC框架,而不是ASP.net Web窗体为您的项目。

You'd get the best bang with Unity when used with ASP.Net MVC rather than the plain old ASP.Net project. ASP.Net MVC allows you to use a container like Unity to manage the user objects, controllers, models, etc. If possible, use MVC rather than ASP.net web forms for your projects.

如果我正确地理解你的问题,你想使用统一的对象的生命周期维持到每个会话。您需要实现扩展LifetimeManager一个SessionLifetimeManager。在code是pretty简单,沿着这些线路有云:

If I understand your question correctly, you would want to use Unity to maintain the lifetime of the object to per session. You need to implement a SessionLifetimeManager that extends LifetimeManager. The code is pretty simple and goes along these lines:

public class SessionLifetimeManager : LifetimeManager
{
    private string _key = Guid.NewGuid().ToString();

    public override object GetValue()
    {
          return HttpContext.Current.Session[_key];
    }

    public override void SetValue(object value)
    {
          HttpContext.Current.Session[_key] = value;
    }

    public override void RemoveValue()
    {
          HttpContext.Current.Session.Remove(_key);
    }
}

您也可以写一个类似一个PerWebRequest生命周期管理。

You could also write a similar one for PerWebRequest lifetime management.

这篇关于使用ASP.NET会话终身管理(统一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆