ASP.NET的Application詳解

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

 

ASP.NET中的Application

1、 Application是用於儲存所有使用者共有的資訊。在ASP時代,如果要儲存的資料在應用程式生存期內不會或者很少改變,那麼使用Application是理想的選擇。但是在ASP。NET開發環境中,程式員通常把類似的配置資料放在Web.config中。如果要使用Application,要注意所有的寫操作度都要在Global.asax檔案中的Application_OnStart事件中完成。

  //下面的代碼是在Global.asax檔案中設定

  Application.Lock();

  Application[“UserId”]=”Hello kitty”;

  Application.UnLock();

  以下是在頁面中調用Application

  String UserId=Application[“UserId”].ToString();

2、Application的特性:

 1、資訊量大小為任意大小

 2、應用於整個應用程式/所有使用者

 3、儲存在伺服器端

 4、範圍和儲存時間是在整個應用程式的生命期

3、如果在應用程式中使用Application對象,一個需要考慮的問題就是任何寫操作都要在Global檔案中的Application_OnStart事件中完成。儘管使用了Application.Lock()和Application.UnLock()方法來避免同步操作,但是由於它序列化了對Application對象的請求,當網站訪問量大的時候會產生嚴重的效能瓶頸,因此最好不要在此對象中儲存大的資料集合。

4、下面使用Application實現線上使用者統計,記錄同時現在人數和網站總訪問人數。

 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel;using System.IO;using System.Web.SessionState; /// <summary>///Global 的摘要說明/// </summary>public class Global:System.Web.HttpApplication{    private System.ComponentModel.IContainer component = null;//必要的設計器變數    private FileStream fileStream;    private StreamReader reader; //讀字元流    private StreamWriter writer;//寫字元流       public Global()       {              }    /// <summary>    /// 網站程式啟動    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Application_Start(object sender,EventArgs e)    {        Application["currentUser"] = 0;//初始化線上使用者        //負載檔案流,如果檔案不存在則建立檔案        fileStream = new FileStream(Server.MapPath("~/count.txt"),FileMode.OpenOrCreate);        reader = new StreamReader(fileStream);//使用流讀取檔案        Application["AllUser"] = Convert.ToInt32(reader.ReadLine());//將檔案中的記錄存入總訪問數中        reader.Close();//關閉流    }    /// <summary>    /// 會話開始 ,當使用者訪問網站時,線上使用者+1,總訪問數+1    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Session_Start(object sender, EventArgs e)    {        Application.Lock();//鎖定,防止同時錄入        Application["currentUser"] = (int)Application["currentUser"] + 1;        Application["AllUser"] = (int)Application["AllUser"] + 1;        fileStream = new FileStream(Server.MapPath("~/count.txt"), FileMode.OpenOrCreate, FileAccess.Write);        //寫入流        writer = new StreamWriter(fileStream);        //把總訪問數再次寫入count.txt檔案中        writer.WriteLine(Application["AllUser"].ToString());        writer.Close();//關閉寫入流        Application.UnLock();//解開鎖定    }    /// <summary>    /// 會話結束    /// </summary>

 

ASP.NET的Application詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.