ASP.Net Web 服務 – 如何使用會話狀態

oschina發表於2014-08-18

  在上次部落格帖子中,我們討論了客戶端對web服務的使用。在這篇文章中我們將複習一下如何使用web服務的會話狀態。

  這是上一篇文章的延續。因此請迅速的回顧之前的文章以便有一個清晰的概念。

  你可以這裡閱讀上篇文章。

  在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方法.

[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.

[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控制元件進去.

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

WebService11

  在檔案修改之後的程式碼之前,我們需要更新一下代理類。為此,在CalculatorService並選擇Update Service Reference.

WebService12

  此後,在btnAdd_Click事件程式碼段之中, 加入如下幾行程式碼.

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.

 接下來是什麼?

  在後續文章中,我們將會討論WebMethod特性及其屬性 有關的東西。

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

  原文地址:http://www.codeproject.com/Articles/807843/ASP-Net-Web-Services-How-to-use-session-state-in-a

相關文章