구성 관리자를 사용하여 응용 프로그램을 다시 시작하지 않고 구성을 다시 로드합니다.섹션 새로 고침
웹 애플리케이션에서 이것을 작동시킨 사람이 있습니까?
제가 무엇을 하든 제 appSettings 섹션(appSettings file=을 사용하여 web.config에서 삭제됨)이 나타납니다.\Site\site.config")는 다시 로드되지 않습니다.
애플리케이션을 다시 시작해야 하는 경우에 제가 운명인가요?저는 이 방법이 저를 좀 더 성과적인 해결책으로 이끌 수 있기를 희망했습니다.
업데이트:
다시 로드'란 구성 관리자를 새로 고치는 것을 의미합니다.ASP를 완전히 다시 시작할 필요 없이 AppSettings를 사용할 수 있습니다.NET 애플리케이션 및 일반적인 시작 지연 시간이 발생해야 합니다.
올바른 대/소문자 구분 값을 RefreshSection(즉, RefreshSection)에 전달하고 있는지 확인합니다.
ConfigurationManager.RefreshSection("appSettings");
앱 설정에 외부 구성 파일을 사용할 때 이는 결함(아마 버그)인 것 같습니다.configSource 특성을 사용하여 시도했지만 RefreshSection은 절대 작동하지 않습니다. 파일 특성을 사용할 때도 마찬가지일 것입니다.앱 설정을 web.config RefreshSection 안으로 다시 이동하면 완벽하게 작동하지만 그렇지 않으면 당신은 망한 것 같습니다.
웬일인지 그래.ConfigurationManager.RefreshSection("appSettings")
나를 위해 일하지 않았습니다.웹을 다시 로드하는 중입니다.구성 개체로 구성하는 것이 올바르게 작동하는 것 같습니다.다음 코드는 웹을 가정합니다.구성 파일이 실행(빈) 폴더 아래에 있는 하나의 디렉터리입니다.
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Uri uriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
string appPath = uriAssemblyFolder.LocalPath;
configMap.ExeConfigFilename = appPath + @"\..\" + "Web.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
다음과 같이 사용됩니다.
string webConfigVariable = config.AppSettings.Settings["webConfigVariable"].Value;
.AppSettings가 외부인 경우 RefreshSection()이 작동하지 않습니다.
그러나 다음을 사용하여 값을 변경할 수 있습니다.
ConfigurationManager.AppSettings.Set(key, value)
이렇게 하면 파일의 설정이 변경되지 않고 메모리에 로드된 값만 변경됩니다.
섹션 새로 고침을 사용하는 대신 다음 작업을 수행했습니다.
string configFile="path to your config file";
XmlDocument xml = new XmlDocument();
xml.Load(configFile);
foreach (XmlNode node in xml.SelectNodes("/appSettings/add"))
{
string key = node.Attributes["key"].Value;
string value= node.Attributes["value"].Value;
ConfigurationManager.AppSettings.Set(key, value);
}
앱 설정에 대한 후속 호출입니다.Get에는 업데이트된 값이 포함됩니다.
그러면 애플리케이션을 다시 시작할 필요 없이 앱 설정이 업데이트됩니다.
대안으로 당신은 당신 자신의 것을 쓸 수 있습니다.ConfigSection
세트restartOnExternalChanges="false"
.
그런 다음 섹션을 읽을 때ConfigurationManager.GetSection("yourSection")
응용 프로그램을 다시 시작하지 않으면 설정이 자동으로 실행됩니다.
또한 강력하게 입력된 설정이나 NameValueCollection을 구현할 수 있습니다.
예. iis를 다시 시작해야 합니다.
asp.net 4.0 및 iis 7.5에는 초기 시작이 제거되는 기능이 있습니다.
이것이 웹 앱에서 가능한지는 모르겠지만 데스크톱 앱에서 작동합니다.Configuration Manager 대신 Configuration Settings(구성 관리자)를 사용해 보십시오(오래된 클래스를 사용하면...). 그런 다음 모든 데이터를 클래스로 읽습니다.새로 고치려면 새 인스턴스를 만들고 이전 인스턴스에 대한 모든 참조를 삭제하기만 하면 됩니다.이것이 작동하는 이유에 대한 나의 이론(잘못되었을 수 있음): 실행하는 동안 내내 app.config 파일에 직접 액세스하지 않으면 응용 프로그램에 의해 파일 잠금이 해제됩니다.그런 다음 파일에 액세스하지 않을 때 편집할 수 있습니다.
앱.구성 설정은 응용 프로그램이 시작될 때 메모리에 캐시됩니다.이러한 이유로 응용 프로그램을 다시 시작하지 않고는 이러한 설정을 변경할 수 없을 것입니다.간단한 별도의 XML 구성 파일을 만들어 로드/캐시/재로드를 직접 처리하는 것이 좋습니다.
작성 방법은 다음과 같습니다.
DIM 구성을 시스템으로 설정합니다.배열.구성 = 시스템.웹.구성.웹 구성 관리자입니다.웹 구성 열기("~")
AddOrUpdateAppSetting(config, "YourSettingKey", "Your ValueFor")을 반환합니다.키")
캐시에 있는 값 대신 파일에 있는 값을 읽으려면 다음과 같이 읽습니다.
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~")
Return config.AppSettings.Settings("TheKeyYouWantTheValue").Value
전체 예:
Protected Shared Function AddOrUpdateAppSetting( _
ByVal Config As System.Configuration.Configuration _
, ByVal TheKey As String _
, ByVal TheValue As String _
) As Boolean</p>
Dim retval As Boolean = True
Dim Itm As System.Configuration.KeyValueConfigurationElement = _
Config.AppSettings.Settings.Item(TheKey)
If Itm Is Nothing Then
If Config.AppSettings.Settings.IsReadOnly Then
retval = False
Else
Config.AppSettings.Settings.Add(TheKey, TheValue)
End If
Else
' config.AppSettings.Settings(thekey).Value = thevalue
If Itm.IsReadOnly Then
retval = False
Else
Itm.Value = TheValue
End If
End If
If retval Then
Try
Config.Save(ConfigurationSaveMode.Modified)
Catch ex As Exception
retval = False
End Try
End If
Return retval
End Function
앱 설정을 자체 외부 파일에 저장해 본 적이 있습니까?
app.config/web.config에서:
<appSettings configSource="appSettings.config"></appSettings>
appSettings.config:
<?xml version="1.0"?>
<appSettings>
<add key="SomeKey" value="SomeValue" />
</appSettings>
appSettings.config에 대한 변경 사항은 즉시 반영되어야 합니다.더 많은 정보: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx
언급URL : https://stackoverflow.com/questions/179254/reloading-configuration-without-restarting-application-using-configurationmanage
'programing' 카테고리의 다른 글
Cocoapods + 'x'에 대한 기본 모듈을 로드할 수 없습니다. (0) | 2023.08.27 |
---|---|
도커 합성을 통해 항상 새로운 이미지에서 컨테이너를 다시 생성하는 방법은 무엇입니까? (0) | 2023.08.27 |
AJAX의 비동기적 특성을 추상화하기 위한 유창한 Javascript 인터페이스 설계 (0) | 2023.08.27 |
부트스트랩 4 navbar 버튼 아이콘 색상을 변경하려면 어떻게 해야 합니까? (0) | 2023.08.27 |
데이터를 지우지 않고 양식을 기본값으로 설정 (0) | 2023.08.27 |