在 WCF 中使用 session 的目的是什么 [英] What is the purpose of using session in WCF

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

问题描述

我读了一篇文章,他们说WCF 确实支持会话,是的;但它们不像 ASP.NET 会话.会话用于按顺序传递消息

i read a write up and they said WCF does support sessions yes; but they are not like ASP.NET sessions. Sessions are there to deliver messages in order

所以我不明白会话在 wcf 中的作用.在 asp.net 中,许多人对用户进行身份验证并在会话中存储一些值,然后检查每个安全页面访问是否该值在该会话变量中可用?

so i do not understand what session does in wcf. in asp.net many people authenticate user and store some value in session and later check for every secure page access that the value is available in that session variable or not?

如果没有,则将用户重定向到登录页面.但我想了解 wcf 中的会话是什么?如果在 wcf 中未启用会话,那么将无法实现.

if not then redirect user to login page. but i like to understand what session does in wcf? if session is not enable in wcf then what will not be possible.

请用小例子解释,因为我能理解.谢谢

please explain with small example as a result i can understand. thanks

推荐答案

WCF 会话和 ASP.Net 会话状态之间几乎没有共同之处 - MSDN.基本上,ASP.Net 会话围绕识别用户与网站的交互,并允许存储与此用户会话相关联的 Session State.

There is little in common between WCF session and ASP.Net session state - the differences are clearly highlighted in MSDN. Basically, ASP.Net sessions revolve around identification of the user's interaction with a web site, and allow for storage of Session State which is associated with this user session.

WCF 会话在多个调用中保持状态,例如服务器将能够使用来自同一会话的先前方法调用的状态信息恢复会话.WCF 会话中没有开箱即用的状态持久性,通常,WCF 会话的持续时间比 ASP.Net 会话的持续时间短.

A WCF session retains state across multiple calls, e.g. the server will be able to resume a conversation with state information from the previous method calls made by the same session. There is no out-of-the-box persistence for state in a WCF session, and typically, WCF sessions will be of shorter duration than an ASP.Net session.

在 Session 期间调用的 WCF Session 方法可能需要按特定顺序(IsInitiatingIsTerminating).

WCF Session methods invoked during the Session may need to be in a specific order (IsInitiating, IsTerminating).

计算器示例是一个很好的示例:

The calculator example is a good one:

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required)]
public interface ICalculatorSession
{
    [OperationContract(IsOneWay=true, IsInitiating=true, IsTerminating=false)]
    void Clear();
    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
    void AddTo(double n);
    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
    void SubtractFrom(double n);
    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
    void MultiplyBy(double n);
    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
    void DivideBy(double n);
    [OperationContract(IsInitiating = false, IsTerminating = true)]
    double Equals();
}

从上面的界面:

  • 客户端必须通过调用 Clear() 来发起一个会话(即在调用这个方法之前它不能开始做任何计算)
  • 客户端然后可以调用任意数量的DivideByMultiplyBySubtractFromAddTo 方法.在任何时候,服务器都会记住之前的状态,即之前计算的结果,就像计算器中的寄存器一样.
  • 客户端调用 Equals 来获得最终结果,这也结束了 Session.
  • The client MUST initiate a session by calling Clear() (i.e. it can't start doing any calcs until this method is called)
  • The client can then call any number of DivideBy, MultiplyBy, SubtractFrom, AddTo methods. At all points, the server will remember the previous state, i.e. the result of the previous calculations, like the register in a calculator.
  • The client calls Equals to get a final result, which also ends the Session.

编辑我已将一个示例上传到 GitHub 此处,其中包含 VS2010 的客户端和服务器位.

Edit I've uploaded a sample to GitHub here, with client and server bits, for VS2010.

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

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