공유 DLL에서 구성 도우미를 사용하고 프로젝트를 편집하고 설정 탭을 설정하여 Settings.Properties.Default 물건을 사용하는 DLL에서 app.config 파일을 사용했습니다. 다시 컴파일하지 않고 app.config (dll에 있음)를 프로젝트 설정과 다시 동기화하지 않으면 값이 읽히지 않는 것으로 보입니다.
나를 위해 작동합니다. 나는 영감을 얻은 곳을 기억하지 않는다. 나는이 클래스를 공유 된 프로젝트에 어딘가에 포함시킨다. 모든 DLL이 dllFile.dll.config 항목을 변경할 수있는 자체 설정을 호출 할 수있게합니다. 나는 이것을 연결 문자열에 사용한다. 주의해야 할 점은이 방법에서 연결 문자열은 특수 연결 문자열이 아닌 형식 문자열이어야한다는 것입니다.
using System;
using System.Configuration;
namespace Shared
{
public static class ConfigurationHelper
{
public static string GetConfigValue(string keyName)
{
string codebase = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
Uri p = new Uri(codebase);
string localPath = p.LocalPath.ToLowerInvariant();
string executingFilename = System.IO.Path.GetFileNameWithoutExtension(localPath);
string sectionGroupName = "applicationSettings";
string sectionName = executingFilename + ".Properties.Settings";
string configName = localPath + ".config";
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = configName;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConfigurationSectionGroup group = config.GetSectionGroup(sectionGroupName);
ClientSettingsSection section = null;
foreach (ClientSettingsSection sect in group.Sections)
{
if (sect.SectionInformation.Name.Equals(sectionName, StringComparison.InvariantCultureIgnoreCase))
{
section = sect;
break;
}
}
SettingElement elem = section.Settings.Get(keyName);
if (elem == null)
return "";
else
return elem.Value.ValueXml.InnerText.Trim();
}
}
}
//in DLL
void foo()
{
var str = ConfigurationHelper.GetSetting("ConnectionStringProd");
}
맞춤 섹션을 사용하면 작동합니까? 아니면 configSource 속성을 지원하기 위해 커스텀 섹션 코드에서 뭔가를해야합니까? – Andrey
+1 - 좋은 답변, 나를 이길! –
@Andrey : .NET 2.0 이상의 모든 ** ConfigurationSection에서 작동합니다. –