如何提供在 Windows 和 Web 应用程序中使用的 Session/Host 对象?

发布于 2024-07-07 12:25:58 字数 472 浏览 15 评论 0原文

我有一个 Web 应用程序,它大量使用会话状态来存储有关当前用户的信息、他们的个人设置、记录他们的会话历史记录等。

我发现自己在业务层中检索此会话信息,如下所示:

((UserSession)HttpContext.Current.Session["UserSession"]).User.Info

这提出了一个问题 - 在将来的某个时候,我的应用程序将有一个 Windows 客户端,它显然无法引用 Web 会话状态。 因此,我需要一个可以在业务层中引用的主机或自定义会话类,该类与应用程序是在 Web 还是桌面上运行无关。 类似于:

IHost.User.Info

在幕后,Web 实现显然会利用会话状态来存储信息,但我需要将其隐藏在业务层之外。 有没有人解决了这个问题或者得到了关于如何最好地解决这个问题的任何实用建议?

帮助表示赞赏。

I have a web application that makes heavy use of the Session state to store information about the current user, their personal settings, record their session history and so on.

I have found myself retrieving this session information in my business layer, like so:

((UserSession)HttpContext.Current.Session["UserSession"]).User.Info

This poses a problem - at some point in the future my application will have a Windows client which obviously cannot reference the web Session state. So I need a host or customized session class that I can reference in my business layer that is agnostic of whether the application is running on the web or desktop. Something like:

IHost.User.Info

Behind the scenes, the web implementation will obviously utilize the Session state to store information, but I need to hide this away from my business layer. Has anyone solved this problem or got any practival advice on how best to approach this?

Help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭白衣梦中忆 2024-07-14 12:25:58

假设业务层是一个单独的 DLL,我永远不会添加对 System.Web 的引用,因此我永远不会直接使用 Session 对象。 这将导致业务层和向客户端(Web 或 winforms)公开的接口的不同设计。

也就是说,作为一种快速解决方法,我建议在业务层中编写一个包装类,以隐藏代码中的 Session 对象。 您的代码调用将如下所示:

((UserSession) DualContext.Current["UserSession"]).User.Info

包装器实现将如下所示(未完成和测试):

public class DualContext 
{
   private Dictionary<string, object> winFormsSession = new Dictionary<string, object>();
   private static readonly DualContext instance = new DualContext();

   public static DualContext Current
   {
      get {  return instance; }
   }

   public object this[string key]
   {
      get 
      {
         if (HttpContext.Current != null)
            return HttpContext.Current.Session[key];
         else
            return winFormsSession[key];
      }
      set 
      {
         if (HttpContext.Current != null)
            HttpContext.Current.Session[key] = value;
         else
            winFormsSession[key] = value;
      }
   }
}

Assuming that the business layer is a separate DLL, I would never add a reference to System.Web and in consequence I would never use the Session object directly. This would lead to a different design of the business layer and of the exposed interfaces to a client (either web or winforms).

That said, as a quick workaround I would suggest to write a wrapper class in your business layer that hides the Session object from your code. Your calls from code will be something like this:

((UserSession) DualContext.Current["UserSession"]).User.Info

and the wrapper implementation will be something like this (not completed and tested):

public class DualContext 
{
   private Dictionary<string, object> winFormsSession = new Dictionary<string, object>();
   private static readonly DualContext instance = new DualContext();

   public static DualContext Current
   {
      get {  return instance; }
   }

   public object this[string key]
   {
      get 
      {
         if (HttpContext.Current != null)
            return HttpContext.Current.Session[key];
         else
            return winFormsSession[key];
      }
      set 
      {
         if (HttpContext.Current != null)
            HttpContext.Current.Session[key] = value;
         else
            winFormsSession[key] = value;
      }
   }
}
帅哥哥的热头脑 2024-07-14 12:25:58

这将需要一些重新架构,但如果您从使用会话状态切换到用户配置文件,则可以使用客户端应用程序服务来共享信息。

http://msdn.microsoft.com/en-us/library/bb384297。 ASPX

It would take some re-architecting, but if you switch from using Session State to User Profiles you could then use Client Application Services to share the information.

http://msdn.microsoft.com/en-us/library/bb384297.aspx

且行且努力 2024-07-14 12:25:58

我猜你需要创建一个 Web 服务或 RESTfull 服务。 该服务将返回一个代表您的用户信息的 XML 文件。 您将能够从 Windows 或 Web 应用程序调用该服务。

I guess you need to create a webservice or RESTfull service. The service will return an XML file representing your user information. You will be able to invoke the service wither from you windows or web application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文