如何提供在 Windows 和 Web 应用程序中使用的 Session/Host 对象?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设业务层是一个单独的 DLL,我永远不会添加对 System.Web 的引用,因此我永远不会直接使用 Session 对象。 这将导致业务层和向客户端(Web 或 winforms)公开的接口的不同设计。
也就是说,作为一种快速解决方法,我建议在业务层中编写一个包装类,以隐藏代码中的 Session 对象。 您的代码调用将如下所示:
包装器实现将如下所示(未完成和测试):
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 theSession
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:and the wrapper implementation will be something like this (not completed and tested):
这将需要一些重新架构,但如果您从使用会话状态切换到用户配置文件,则可以使用客户端应用程序服务来共享信息。
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
我猜你需要创建一个 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.