在 wcf 中使用会话 [英] Using session in wcf

查看:32
本文介绍了在 wcf 中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将我的服务实例设置为 Per Session 或 Single,我可以在会话中的服务实例之间发送一些数据吗?它应该在 Asp.net 会话中完成 - HttpContext.Current.Session或 wcf 有自己的会话?

If I set my servis instance as Per Session or Single can I send some data between services instance in session? It should be done in Asp.net session - HttpContext.Current.Session or wcf have own session ?

推荐答案

正如我所说 - WCF 不是 ASP.NET,它的会话处理有很大不同.虽然 ASP.NET 会话和 WCF 会话的名称相同,但它们的用途和用途却大不相同.

As I said - WCF is not ASP.NET and its session handling is vastly different. While ASP.NET sessions and WCF sessions are called the same - they are vastly different in their purpose and usefulness.

阅读 MSDN 页面在 WCF 中使用会话了解更多详细信息.

Read the MSDN page Using Sessions in WCF for more details.

一句话是:没有与 WCF 会话相关联的通用数据存储. - 所以答案是否定的 - WCF 中的会话用于数据存储.

One sentence reads: There is no general data store associated with a WCF session. - so the answer is no - sessions in WCF are not meant for data storage.

WCF 会话只是将多条消息捆绑"到一个对话中.默认情况下,使用每次调用"模型,每个 WCF 服务请求将获得自己的、新实例化的服务类实例来处理请求,并且在返回答案后将释放该服务类实例.使用会话可以避免这种情况 - 处理会话第一次调用的服务类实例将在服务器端保持活动状态(因此也会占用服务器上的内存),并将处理同一会话中的所有后续请求.

WCF sessions are merely to "tie together" several messages into a conversation. By default, with the "per-call" model, each WCF service request would get its own, freshly instantiated service class instance to handle the request, and that service class instance will be freed after returning the answer. Using sessions avoids this - the service class instance handling the first call of a session will stay alive on the server side (and thus also taking up memory on the server) and will handle all subsequent requests within the same session.

WCF 和 Web 服务通常最好是无状态,因此会话在适当的 SOA 环境中有点古怪的架构 - 这很可能就是为什么 WCF 中的会话也不像因为 ASP.NET 会话适用于 Web 应用程序.

WCF and web services in general should however preferably be stateless, so sessions are a bit of an oddball architecture in a proper SOA environment - and that's most likely why sessions in WCF are also not nearly as useful as ASP.NET sessions are for web apps.

为了保持无状态并支持每次调用方法(首选的最佳实践),如果您需要在调用之间存储数据,请将其存储在持久存储(例如数据库)中,并在以后需要时从那里取回.

To remain stateless and support the per-call method (the preferred best practice), if you need to store data between calls, store it in a persistent store (e.g. a database) and fetch it back from there when needed later on.

这篇关于在 wcf 中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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