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 Settings 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 應用程式在指定的時間內未使用時,就可以根據 IdleTimeout 和 ShutdownTimeout 屬性從記憶體卸載。
建構函式
HostingEnvironmentSection() |
使用預設值初始化 HostingEnvironmentSection 類別的新執行個體。 |