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分钟,所以假设当前是1033分,空闲5分钟是需要到1139分。

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

说明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也可。

posted @ 2016-09-21 16:36  猴健居士  阅读(2609)  评论(0编辑  收藏  举报