hostingEnvironment与宿主环境

定义用来控制应用程序宿主环境的行为的配置设置。 ios

   

配置以下 web

<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被关闭时候记录时间和缘由 session

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做为键值去缓存值,若是没有缓存就执行委托,并把结果缓存起来,做用待会儿看结果时见分晓app

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的信息以证实他有被取代过dom

<script>

$(function () {

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

});

</script>

配置加上ide

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

相关文章
相关标签/搜索