程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> hostingEnvironment與宿主環境,hostingenvironment

hostingEnvironment與宿主環境,hostingenvironment

編輯:關於.NET

hostingEnvironment與宿主環境,hostingenvironment


定義用來控制應用程序宿主環境的行為的配置設置。

   

配置如下

<hostingEnvironment 
idleTimeout="HH:MM:SS" 
shadowCopyBinAssemblies="true|false" 
shutdownTimeout="number"
urlMetadataSlidingExpiration="HH:MM:SS"
/>

 

shadowCopyBinAssemblies:該值指示 Bin 目錄中的應用程序的程序集是否影像復制到該應用程序的 ASP.NET 臨時文件目錄中。但純看這句話我是一面懵懂的,幸虧看了一篇老外的文章經過自己實踐才明白其作用。平時我們更新bin文件夾的內容時,無需重啟網站則會生效,原因就是ASP.NET對bin文件夾進行監控,當發現bin文件夾時則會馬上更新網站,這個功能就是靠shadowCopyBinAssemblies來控制的,一但設成false,想在網站運行的時候就會把bin文件夾裡的文件進行鎖定,無法隨時更新,更不用說馬上生效。這個設置的使用場景在於更新bin文件的內容過多,導致影響了網站的性能。

idleTimeout:卸載不活動的應用程序前設置時間值,格式為HH:MM:SS 或"不限"。 默認值為"Infinite"。然而這裡有個不恰當的地方,格式並非是,因為如下圖

而給他設置的值實際是int值,單位是分鐘,這個idelTimeout特性的作用是關閉掉空閒時間達到設定值的分鐘值的appdomain。為此鄙人故意做了個實驗

在Global.asax中添加代碼,用於監控Application被關閉時候記錄時間和原因

protected void Application_End()

{

File.AppendAllText(@"E:\Application_End.txt",string.Format("{0}\t{1}\r\n",System.Web.Hosting.HostingEnvironment.ShutdownReason.ToString(),DateTime.Now) );

}

另外在Action方法中添加了一個緩存,緩存是封裝過的,大概是以myappdomainTest作為鍵值去緩存值,如果沒有緩存就執行委托,並把結果緩存起來,作用待會兒看結果時見分曉

object now = CacheHelper.GetFromCache<object>("myappdomainTest", () =>

{

System.IO.File.AppendAllText(@"E:\Application_End.txt", string.Format("write cache myappdomainTest {0}\r\n",DateTime.Now));

return DateTime.Now;

});

在View中以彈窗的形式輸出AppDomain的信息以證明他有被取代過

<script>

$(function () {

alert('@AppDomain.CurrentDomain.GetHashCode();');

});

</script>

配置加上

<hostingEnvironment idleTimeout="5"/>

訪問了一下頁面,appdomian的HashCode如下

看到輸出的文件內容如下

靜待6分鐘,因為這裡是空閒5分鐘,所以假設當前是10:33分,空閒5分鐘是需要到11:39分。

再看看輸出的文件多了一行

說明AppDomain在10:39的時候被回收掉了,此時再訪問一下網頁

確實發現AppDomain不一樣了。而再看輸出的文件

緩存已經沒有了,需要重新生成。

假如把idleTimeout="5"去掉,則AppDomain一直都不會被回收,訪問過站點後,無論閒置多久都不會觸發Application_End

shutdownTimeout:設置正常關閉引用程序的時間量(以秒為單位)。這段話我也看不懂,在網上也找不到相關了資料,最開始是看了一下源碼,在HostingEnvironment中的私有方法InitiateShutdownWorkItemCallback(Object state /*not used*/)找到這麼一段,下面的shutdonwTimeoutSeconds就是配置中設置的shutdownTimeout。

後來突發奇想才悟出這是特性的意思是關閉一個應用程序這個過程中,需要經歷shutdownTimeoutSeconds這麼多秒的一個時間過程。於是興奮地嘗試了一下,類似於上面的試驗idleTimeout那樣,調用了HostingEnvironment.InitiateShutdown()方法,結果得出的居然是

相差了兩秒,這兩秒,跟配置設置的值相差甚遠,後來再留意到進入休眠的另一個條件是_registeredObjects這個集合的元素需要大於0,難道是這個集合裡面沒有元素?這個registeredObjects是

而這個HostingEnvironment.InitiateShutdown()方法的作用是開始關閉與此宿主關聯的 Web 應用程序,並從系統中移除注冊對象。之所以需要設置shutdownTimeout是為了確保在關閉時有足夠是時間去注銷注冊到的IRegisteredObject對象。遺憾的是暫時無辦法去鑒別。

以上僅本人的推斷,如諸位有更好的看法或見解,請批評指正。下面附帶了網上找來的關於idleTimeout 和ShutdownTimeout的支離片碎的內容。

   

   

在stackoverflow上面 《Difference between idleTimeout and ShutdownTimeout》

關於idleTimeout在WCF中的描述

After modifying a .svc file, the application domain is also recycled. The hosting environment will try to close all the WCF services' open connections gracefully in a timely manner. When services somehow don't close in time, they will be forced to abort. Through the HostingEnvironmentSettingsconfiguration settings, you can influence the behavior of recycling, as you can see in Listing 5-8. The idleTimeout setting determines the amount of idle time in seconds for an application domain to be recycled. The shutdowntimeout setting determines the amount of time in seconds to gracefully shut down an application. After this time-out, it forces applications to shut down.

Listing 5-8. Web.config with hostingenvironment section for recycling settings

<system.web>
<hostingEnvironment idleTimeout="20"
shutdownTimeout="30"/>
</system.web>

When you are using WCF sessions, these recycling features are critical to understand. This is typically the case in the security and reliable messaging scenarios, as you will read in Chapters 6 and 8 of this book. By default, WCF stores session state in memory. This is a different implementation from ASP.NET session state and doesn't come with a configuration to switch over to persistent session state storage. However, you can, and should, in the security and reliable messaging scenarios benefit from the ASP.NET implementation. Using the ASP.NET compatibility features of WCF provides you with the SQL Server and state server implementations of ASP.NET session state to support enterprise-ready scenarios. In the next section, you will learn how to benefit from the WCF ASP.NET compatibility mode.

   

來自 <https://msdn.microsoft.com/en-us/library/bb332338.aspx>

   

下面的是譯文

修改 .svc 文件之後,還將回收應用程序域。承載環境將嘗試按時正常關閉所有 WCF 服務的打開連接。如果由於某種原因使服務無法按時關閉,系統將強制中止它們。通過 HostingEnvironmentSettings 配置設置,可以影響回收的行為,如列表 5-8 所示。idleTimeout 設置確定應用程序域在回收前的空閒時間長度(秒)。shutdowntimeout 設置確定正常關閉應用程序前的時間長度(秒)。發生此超時後,它將強制應用程序關閉。

使用 WCF 會話時,理解這些回收功能很重要。通常,在安全和可靠消息方案中有這種情況,本書的第 6 章和第 8 章將對此進行介紹。默認情況下,WCF 將會話狀態存儲在內存中。這是與 ASP.NET 會話狀態不同的實現,它沒有需要切換到持久會話狀態存儲的配置。但在安全和可靠消息方案中,您可以並且應當受益於 ASP.NET 實現。通過使用 WCF 的 ASP.NET 兼容性功能,可以獲得 ASP.NET 會話狀態的 SQL Server 和狀態服務器實現,以支持企業可用的方案。在下一節中,將介紹如何受益於 WCF ASP.NET 兼容性模式。

   

在《Programming Microsoft ASP.NET 4》書中關於hostingEnvironment的一段描述。

   

另外在MSDN中看到的HostingEnvironment類,這個類與本配置節表面的關聯不算多,看過源碼就發現裡面有些屬性還是用到了配置節的內容的,另外還發現了幾個有用的屬性。

例如之前有同學苦惱於獲取當前網站的路徑,其實也可以用Server屬性的MapPath也可。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved