變更外部檔案範例中的組態
更新:2007 年 11 月
透過啟用或停用應用程式的重新啟動功能,可以使用外部組態檔來擴充應用程式組態設定,並控制在這些設定變更時是否保留狀態資訊。重新啟動功能是由 restartOnExternalChanges 屬性 (Attribute) 所控制,該屬性套用至不同的組態檔區段。
範例 Web 應用程式檔案
下列區段包括可建置範例 Web 應用程式的程式碼,其中有一個自訂區段,其 configSource 屬性會指向外部組態檔而且它的 restartOnExternalChanges 屬性起初會設定為 true。
執行 Web 應用程式會顯示外部組態檔有所變更時,全域回傳計數器會發生什麼變化,而所提供的程式碼範例則示範當 restartOnExternalChanges 分別設定為 true、設定為預設值及設定為 false 時,會發生什麼變化。
Global.asax
範例 Web 應用程式必須包括這個 Global.asax 網頁。下列程式碼範例會定義具有全域回傳計數器的網頁。計數器會追蹤重新整理應用程式之 Default.aspx 網頁所產生的回傳要求 (稍後在此主題中定義)。
' Code that runs on application startup.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' This counter is set to 0 the first time
' the application starts and, thereafter, every
' time it restarts.
Dim postBackCount As Integer = 0
Dim appState As HttpApplicationState = Application.Contents
appState.Add("postBackKey", postBackCount)
End Sub 'Application_Start
// Code that runs on application startup.
void Application_Start(object sender, EventArgs e)
{
// This counter is set to 0 the first time
// the application starts and, thereafter, every
// time it restarts.
int postBackCount = 0;
HttpApplicationState appState = Application.Contents;
appState.Add("postBackKey", postBackCount);
}
Default.aspx
Default.aspx 網頁會包含可在 Web.config 檔中建立自訂區段、將項目加入 External.config 檔,並顯示回傳計數器值的程式碼。請注意網頁必須包含 Label 控制項才能顯示回傳計數器的目前值。下列程式碼範例會顯示要定義此控制項的一種可能方式。
<asp:Label ID="ResultId" style="font-weight:bold; color:Red;"/>
下列程式碼範例會定義 Default.aspx 網頁。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim extConfig As ExternalConfiguration = Nothing
If Not IsPostBack Then
' Create an instance of the custom section.
extConfig = New ExternalConfiguration()
End If
Dim postBackCount As Integer = _
Fix(HttpContext.Current.Application("postBackKey"))
'Display current counter value.
ResultId.Text = postBackCount.ToString()
HttpContext.Current.Application("postBackKey") = _
Fix(HttpContext.Current.Application("postBackKey")) + 1
extConfig.UpdateAppSettngs()
End Sub 'Page_Load
protected void Page_Load(object sender, EventArgs e)
{
ExternalConfiguration extConfig = null;
if (!IsPostBack)
{
// Create an instance of the cusom section.
extConfig =
new ExternalConfiguration();
}
int postBackCount =
(int) HttpContext.Current.Application["postBackKey"];
ResultId.Text = postBackCount.ToString();
HttpContext.Current.Application["postBackKey"] =
(int)HttpContext.Current.Application["postBackKey"] + 1;
extConfig.UpdateAppSettngs();
}
Web.config
Web.config 檔會定義 MyAppSettings 自訂區段,如下列程式碼範例中所示。此範例使用標準型別來處理此區段,但您可以建立自己的型別。如需詳細資訊,請參閱 ConfigurationElement 和 ConfigurationSection。
<section
name="MyAppSettings"
type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
restartOnExternalChanges="true"
requirePermission="false" />
External.config
External.config 檔包含了 MyAppSettings 自訂區段的詳細資料。在第一次執行應用程式之前,您必須在應用程式根目錄中手動建立此檔案。如需詳細資訊,請參閱 Section 項目繼承的一般屬性中的 configSource 屬性。針對此範例,請確定您的程式碼包含下列空區段。
<MyAppSettings></ MyAppSettings>.
ExternalConfiguration.dll
ExternalConfiguration.dll 檔包含了會更新 MyAppSettings 自訂區段的程式碼。您可以將原始程式碼放在應用程式的 App_Code 目錄中。當需要使用您的應用程式時,ASP.NET 會編譯這個範例。您也可以將範例提供者當做程式庫編譯,再將它放在 Web 應用程式的 Bin 目錄中。在下列程式碼範例中,示範了如何從命令列編譯範例。
vbc /out: ExternalConfiguration.dll /t:library ExternalConfiguration.vb /r:System.Web.dll /r:System.Configuration.dll
csc /out: ExternalConfiguration.dll /t:library ExternalConfiguration.cs /r:System.Web.dll /r:System.Configuration.dll
下列程式碼範例會建置 ExternalConfiguration .dll 檔。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration
Imports System.Web.Configuration
Namespace Samples.AspNet
Public Class ExternalConfiguration
' Instantiate the ExternalConfiguration type.
Public Sub New()
' Access the root Web.config file.
Dim config As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration("/configTarget")
' Get the custom MyAppSettings section.
Dim appSettings As AppSettingsSection = _
CType(config.GetSection("MyAppSettings"), AppSettingsSection)
' Perform the first update.
UpdateAppSettings()
End Sub 'New
' Update the custom MyAppSettings section.
' Note , if the section restartOnExternalChanges attribute
' is set to true, every update of the external
' configuration file will cause the application
' to restart.
Public Sub UpdateAppSettings()
Dim config As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration("/configTarget")
Dim appSettings As AppSettingsSection = _
CType(config.GetSection("MyAppSettings"), AppSettingsSection)
Dim count As Integer = appSettings.Settings.Count
appSettings.Settings.Add("key_" + count.ToString(), _
"value_" + count.ToString())
config.Save()
End Sub 'UpdateAppSettngs
End Class 'ExternalConfiguration
End Namespace
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;
namespace Samples.AspNet
{
public class ExternalConfiguration
{
// Instantiate the ExternalConfiguration type.
public ExternalConfiguration()
{
// Access the root Web.config file.
System.Configuration.Configuration config =
WebConfigurationManager.OpenWebConfiguration(
"/configTarget");
// Get the custom MyAppSettings section.
AppSettingsSection appSettings =
(AppSettingsSection)config.GetSection("MyAppSettings");
// Perform the first update.
UpdateAppSettings();
}
// Update the custom MyAppSettings section.
// Note , if the section restartOnExternalChanges attribute
// is set to true, every update of the external
// configuration file will cause the application
// to restart.
public void UpdateAppSettings()
{
System.Configuration.Configuration config =
WebConfigurationManager.OpenWebConfiguration(
"/configTarget");
AppSettingsSection appSettings =
(AppSettingsSection)config.GetSection("MyAppSettings");
int count = appSettings.Settings.Count;
appSettings.Settings.Add(
"key_" + count.ToString(),
"value_" + count.ToString());
config.Save();
}
}
}
使用範例 Web 應用程式
第一次執行應用程式時,它會將項目加入 External.config 檔。
每當您重新整理 Default.aspx 網頁,回傳計數器就會重設為零。這是因為 MyAppSettings 區段中的 restartOnExternalChanges 屬性預設已設定為 true。
如果您在 Web.config 檔中將 restartOnExternalChanges 屬性設定為 false,則會注意到不管 External.config 檔中是否有變更,回傳計數器都會在您重新整理網頁時遞增。這是因為您已停用應用程式定義域重新啟動。
請參閱
工作
HOW TO:使用 ConfigurationSection 建立自訂組態區段