在上次博客帖子中,我们讨论了客户端对web服务的使用。在这篇文章中我们将复习一下如何使用web服务的会话状态。
这是上一篇文章的延续。因此请迅速的回顾之前的文章以便有一个清晰的概念。
你可以这里阅读上篇文章。
在web服务中要用到ASP.NET中的会话对象,有2件事情需要做。
1.WebService 类需要继承System.Web.Services.WebService类 2.WebMethod中的EnableSession属性值应该设置为true
来看我们CalculatorWebService类,我们可以看到,它已经继承System.Web.Services.WebService类。但是,我们需要EnableSession属性值设置为true。
本文中,我们将试试在使用一个如下所示的GridView中的会话对象来展示最近的计算结果.

为了达成这个目的,首先要想下面这样,修改CalculatorWebService类的Add方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [WebMethod(EnableSession = true )]
public int Add( int firstNumber, int secondNumber)
{
List<string> calculations;
if (Session[ "CALCULATIONS" ] == null )
{
calculations = new List<string>();
}
else
{
calculations = (List<string>)Session[ "CALCULATIONS" ];
}
string strTransaction = firstNumber.ToString() + " + "
+ secondNumber.ToString()
+ " = " + (firstNumber + secondNumber).ToString();
calculations.Add(strTransaction);
Session[ "CALCULATIONS" ] = calculations;
return firstNumber + secondNumber;
}
|

|