剛好看到這一篇,因為開發系統的時候,也常常會將資料暫存在App_Data目錄中。所以看到標題的時候,有稍微嚇到我。不過還好,確定是除非刪除了子目錄,才會影響。還好我大部分都只是針對檔案的讀寫,比較少動到目錄..!!
http://blog.darkthread.net/post-2011-02-15-app-data-change-and-webapp-restart.aspx
我曾介紹過App_Data好用的隱身特性,今天同事傳給我一則MSDN討論,提到App_Data的檔案更動會造成Web Application重新啟動,讓我大吃一驚,因為手邊有幾個專案會在App_Data寫入Log檔,時機還挺頻繁的,若一變動檔案就重啟,網站應該早就爆炸身亡了才對?
爬了文,結論是: "更動App_Data**有可能**導致AppDomain重啟! 如果App_Data下有子目錄被刪除的話"。主要參考來源是Lady Debugger, Tess, 的
:An application domain will unload when any one of the following occurs:
- Machine.Config, Web.Config or Global.asax are modified
- The bin directory or its contents is modified
- The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the <compilation numRecompilesBeforeAppRestart=/> setting in machine.config or web.config (by default this is set to 15)
- The physical path of the virtual directory is modified
- The CAS policy is modified
- The web service is restarted
- (2.0 only) Application Sub-Directories are deleted (see Todd’s bloghttp://blogs.msdn.com/toddca/archive/2006/07/17/668412.aspx for more info)
為了安心起見,我還是寫了個程式驗證: (ASP.NET 3.5 on IIS7)
排版顯示純文字
<%@ Page Language="C#" %>
<script runat="server">
const string SESS_KEY = "MySessVal";
const string DIR_NAME = "TestDir";
const string FILE_NAME = "TestFile.txt";
void btnRead_Click(object sender, EventArgs e)
{
lblSessValue.Text = (string)Session[SESS_KEY];
}
protected void btnSave_Click(object sender, EventArgs e)
{
Session[SESS_KEY] = Guid.NewGuid().ToString();
btnRead_Click(sender, e);
}
protected void btnCreateDir_Click(object sender, EventArgs e)
{
System.IO.Directory.CreateDirectory(
Server.MapPath("~/App_Data/" + DIR_NAME));
btnRead_Click(sender, e);
}
protected void btnDelDir_Click(object sender, EventArgs e)
{
System.IO.Directory.Delete(
Server.MapPath("~/App_Data/" + DIR_NAME));
btnRead_Click(sender, e);
}
protected void btnWriteFile_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllText(
Server.MapPath("~/App_Data/" + FILE_NAME), "AAA");
btnRead_Click(sender, e);
}
protected void btnDelFile_Click(object sender, EventArgs e)
{
System.IO.File.Delete(Server.MapPath("~/App_Data/" + FILE_NAME));
btnRead_Click(sender, e);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>App_Data重啟WebApp測試</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label Text="" ID="lblSessValue" runat="server" EnableViewState="false" />
<hr />
<asp:Button ID="btnSave" runat="server" Text="存入Session"
onclick="btnSave_Click" />
<asp:Button ID="btnRead" runat="server" Text="讀取Session"
onclick="btnRead_Click" />
<asp:Button ID="btnCreateDir" runat="server" Text="建立目錄"
onclick="btnCreateDir_Click" />
<asp:Button ID="btnDelDir" runat="server" Text="刪除目錄"
onclick="btnDelDir_Click" />
<asp:Button ID="btnWriteFile" runat="server" Text="寫入檔案"
onclick="btnWriteFile_Click" />
<asp:Button ID="btnDelFile" runat="server" Text="刪除檔案" style="height: 26px"
onclick="btnDelFile_Click" />
</form>
</body>
</html>
按下【存入Session】會產生一個GUID存入Session中,之後可以按【建立目錄】、【刪除目錄】、【寫入檔案】、【刪除檔案】,接著按【讀取Session】可以觀察Session保存的值是否消失。
實驗證明了只有【刪除目錄】會導致Session消失,特此筆記。
留言列表