ASP.Net Web 服务 – 如何使用会话状态 【已翻译100%】

在上次博客帖子中,我们讨论了客户端对web服务的使用。在这篇文章中我们将复习一下如何使用web服务的会话状态。

这是上一篇文章的延续。因此请迅速的回顾之前的文章以便有一个清晰的概念。

你可以这里阅读上篇文章。

在web服务中要用到ASP.NET中的会话对象,有2件事情需要做。

1.WebService 类需要继承System.Web.Services.WebService类

2.WebMethod中的EnableSession属性值应该设置为true

image

来看我们CalculatorWebService类,我们可以看到,它已经继承System.Web.Services.WebService类。但是,我们需要EnableSession属性值设置为true。

本文中,我们将试试在使用一个如下所示的GridView中的会话对象来展示最近的计算结果.

image

为了达成这个目的,首先要想下面这样,修改CalculatorWebService类的Add方法.

[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;
        }

image

然后再引入另外一个公共方法来返回所有的计算结果. 要使用WebMethod特性来修饰这个方法,并且将EnableSession属性设置为true.

[WebMethod(EnableSession = true)]
        public List<string> GetCalculations()
        {
            if (Session["CALCULATIONS"] == null)
            {
                List<string> calculations = new List<string>();
                calculations.Add("You have not performed any calculations");
                return calculations;
            }
            else
            {
                return (List<string>)Session["CALCULATIONS"];
            }
        }

image

现在就可以构建我们的解决方案了,并能在浏览器中查看到我们的Web服务.

image

Web服务会列出两个方法——Add和GetCalculations.

image

点击Add方法。让我们输入两个数字,比如20和30,然后点击Invoke按钮,我们会得到50这个结果.

image

image

让我们来做另外一次计算,比如30和70。然后点击Invoke按钮,我们将会得到结果为100.

image

image

现在让我们回头来测试一下我们的GetCalculation方法。然后点击Invoke方法,现在回展示出我们之前所做的所有计算。它们会以一个字符串数组的形式返回.

image

如此我们的Web服务就这样按照预期运作了。现在让我们来试试在我们的Web应用程序中使用这些方法。为此,在 Webform1.aspx 中, 让我们往其中拽一个GridView控件进去.

<tr>
    <td>
        <asp:GridView ID="gvCalculations" runat="server">
        </asp:GridView>
    </td>
</tr>

image

在文件修改之后的代码之前,我们需要更新一下代理类。为此,在CalculatorService并选择Update Service Reference.

image

此后,在btnAdd_Click事件代码段之中, 加入如下几行代码.

gvCalculations.DataSource = client.GetCalculations();
            gvCalculations.DataBind();

            gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";

gvCalculations.DataSource = client.GetCalculations();

        gvCalculations.DataBind();

        gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";

构建我们的解决方案,并在浏览器中查看这个web窗口.
image

让我们继续加入两个数字,比如20和30. 而我们会看到虽然我们已经执行了一次计算, You have not performed any calculations 这样的消息还是将会显示出来.

image

这基本上是因为web应用程序并没有像Web服务发送相同的SessionId。为此,将web.config文件中的allowCookie设置成true.

image

现在我们再来运行这个web窗口并添加一些数字。现在我们就可以看到它按照预期运行了.

image

因此,这下面有几点要深入思考:

  • 如果Web服务被修改了,客户端应用程序的代理类就要被更新. 为此,在Service Reference夹下面的服务上点击右键,并选择Update Service Reference项.
  • 将allowCookies属性设置成true,以便让客户端应用程序接受从ASMX Web服务返回的cookie,并将其复制到未来所有项Web 服务发起的请求中去. 这就确保了客户端和Web服务之间是维护的同一个Session.

接下来是什么?

在后续文章中,我们将会讨论WebMethod特性及其属性 有关的东西.

引用: Arun Ramachandran (http://BestTEchnologyBlog.Com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值