HostingEnvironmentSection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义用来控制应用程序宿主环境的行为的配置设置。 此类不能被继承。
public ref class HostingEnvironmentSection sealed : System::Configuration::ConfigurationSection
public sealed class HostingEnvironmentSection : System.Configuration.ConfigurationSection
type HostingEnvironmentSection = class
inherit ConfigurationSection
Public NotInheritable Class HostingEnvironmentSection
Inherits ConfigurationSection
- 继承
示例
此示例演示如何为 hostingEnvironment Element (ASP.NET 设置 Schema) 节的多个属性以声明方式指定值,该属性也可以作为类的成员HostingEnvironmentSection进行访问。
以下配置文件示例演示如何以声明方式为 hostingEnvironment 元素 (ASP.NET 设置架构) 部分指定值。
<system.web>
<hostingEnvironment
idleTimeout="20"
shutdownTimeout="30"
/>
</system.web>
下面的代码示例演示如何使用该 HostingEnvironmentSection 类。
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
#endregion
namespace Samples.Aspnet.SystemWebConfiguration
{
class UsingHostingEnvironmentSection
{
static void Main(string[] args)
{
try
{
// Set the path of the config file.
string configPath = "";
// Get the Web application configuration object.
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
// Get the section related object.
HostingEnvironmentSection configSection =
(HostingEnvironmentSection)config.GetSection("system.web/hostingEnvironment");
// Display title and info.
Console.WriteLine("ASP.NET Configuration Info");
Console.WriteLine();
// Display Config details.
Console.WriteLine("File Path: {0}", config.FilePath);
Console.WriteLine("Section Path: {0}", configSection.SectionInformation.Name);
// Display IdleTimout property
Console.WriteLine("Idle Timeout: {0}", configSection.IdleTimeout);
// Set IdleTimout property
configSection.IdleTimeout = TimeSpan.FromMinutes(40);
// Display ShutdownTimeout property
Console.WriteLine("Shutdown Timeout: {0}", configSection.ShutdownTimeout);
// Set ShutdownTimeout property
configSection.ShutdownTimeout = TimeSpan.FromSeconds(60);
// Update if not locked.
if (!configSection.SectionInformation.IsLocked)
{
config.Save();
Console.WriteLine("** Configuration updated.");
}
else
{
Console.WriteLine("** Could not update, section is locked.");
}
}
catch (Exception e)
{
// Unknown error.
Console.WriteLine(e.ToString());
}
// Display and wait
Console.ReadLine();
}
}
}
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration
Imports System.Web
Imports System.Web.Configuration
Namespace Samples.Aspnet.SystemWebConfiguration
Class UsingSiteMapSection
Public Shared Sub Main()
Try
' Set the path of the config file.
Dim configPath As String = ""
' Get the Web application configuration object.
Dim config As System.Configuration.Configuration = _
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(configPath)
' Get the section related object.
Dim configSection As System.Web.Configuration.HostingEnvironmentSection = _
CType(config.GetSection("system.web/hostingEnvironment"), System.Web.Configuration.HostingEnvironmentSection)
' Display title and info.
Console.WriteLine("ASP.NET Configuration Info")
Console.WriteLine()
' Display Config details.
Console.WriteLine("File Path: {0}", config.FilePath)
Console.WriteLine("Section Path: {0}", configSection.SectionInformation.Name)
' Display the IdleTimout property
Console.WriteLine("Idle Timeout: {0}", configSection.IdleTimeout)
' Set the IdleTimout property
configSection.IdleTimeout = TimeSpan.FromMinutes(40)
' Display the ShutdownTimeout property
Console.WriteLine("Shutdown Timeout: {0}", configSection.ShutdownTimeout)
' Set the ShutdownTimeout property
configSection.ShutdownTimeout = TimeSpan.FromSeconds(60)
' Update if not locked.
If Not configSection.SectionInformation.IsLocked Then
config.Save()
Console.WriteLine("** Configuration updated.")
Else
Console.WriteLine("** Could not update, section is locked.")
End If
Catch e As Exception
' Unknown error.
Console.WriteLine(e.ToString())
End Try
' Display and wait
Console.ReadLine()
End Sub
End Class
End Namespace
注解
该HostingEnvironmentSection类提供了一种以编程方式访问和修改配置文件中节的值HostingEnvironmentSection的方法。当 ASP.NET 应用程序在指定时间内未使用时,可以根据和ShutdownTimeout属性从内存IdleTimeout中卸载该应用程序。
构造函数
HostingEnvironmentSection() |
使用默认设置初始化 HostingEnvironmentSection 类的新实例。 |