如何使用会话状态(ASP.Net Web 服务)


本文摘自PHP中文网,作者Y2J,侵删。

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

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

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

WebService1

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

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

Purpose

  为了达成这个目的,首先要想下面这样,修改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;

        }

WebService2

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[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"];

            }

        }

WebService3

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

WebService4

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

WebService5

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

WebService6

WebService7

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

WebService8

WebService9

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

WebService10

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

1

2

3

4

5

6

<tr>

    <td>

        <asp:GridView ID="gvCalculations" runat="server">

        </asp:GridView>

    </td>

</tr>

WebService11

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

WebService12

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

1

2

3

4

gvCalculations.DataSource = client.GetCalculations();

            gvCalculations.DataBind();

 

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

WebService13

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

WebService14

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

WebService15

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

WebService16

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

WebService17

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

  • 如果Web服务被修改了,客户端应用程序的代理类就要被更新. 为此,在Service Reference夹下面的服务上点击右键,并选择Update Service Reference项.

  • 将allowCookies属性设置成true,以便让客户端应用程序接受从ASMX Web服务返回的cookie,并将其复制到未来所有项Web 服务发起的请求中去. 这就确保了客户端和Web服务之间是维护的同一个Session.

以上就是如何使用会话状态(ASP.Net Web 服务)的详细内容!

相关阅读 >>

分享两种asp.net网站发布时的遇到的问题及解决方案

解决asp.net网站第一次访问慢的方法

asp.net web api 异常处理经验总结

asp.net core中用户登录验证实现最低配置的示例代码

如何使用会话状态(asp.net web 服务)

利用razor引擎生成静态页(asp.net mvc)实例详解

asp.net 获取 客户端ip

分享asp.net学习笔记(9)webpages图表

分享19个asp脚本语言的基本技巧

asp.net中healthmonitor属性的用法教程

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...