configurationsettings.appsettings가 비어 있고 Null Exception이 발생합니다
-
05-07-2019 - |
문제
나는 다음과 같은 수업이 있습니다.
public class RxNormFolderMgr
{
// properties
public string RxNormFolder { get { return ConfigurationSettings.AppSettings["rootFolder"].ToString(); } }
}
내가 이것을 사용하려고 할 때 :
public class TestRxNormFolderManager : ColumnFixture
{
public string RxNormFolder()
{
RxNormFolderMgr folderMgr = new RxNormFolderMgr();
return folderMgr.RxNormFolder;
}
}
"System.Reflection.targetInvocationException : 호출 대상에 의해 예외가 발생했습니다. ---> System.NullReferenceException : 객체 참조가 개체 인스턴스로 설정되지 않습니다." AppSettings의 AllKeys 속성은 길이가 1을 기대하는 제로 길이의 배열입니다.
프로젝트의 내 app.config 파일은 다음과 같습니다.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rootFolder" value ="C:\RxNorm" />
<!-- Root folder must not end with slash. -->
</appSettings>
</configuration>
configurationsettings.appsettings는 더 이상 사용되지 않아야하며 configurationManager.AppSettings를 사용해야하지만 컴파일 할 수는 없습니다. System.Configuration (C : Windows microsoft.net framework v2.0.50727 system.configuration.dll)에 대한 프로젝트에 참조가 있습니다.
피트니스를 사용하여 코드를 테스트하고 있으며 오류가 발생합니다. 내가 한 테스트 픽스처 프로젝트의 bin> debug 폴더에 app.config 파일의 사본을 배치해야한다는 것은 내 이해입니다. 그래서 왜이 오류가 아직 발생하는지 모르겠습니다.
도와주세요.
해결책
Fitnesse로 테스트 할 때 실제 실행 파일 실행은 "Fitserver.exe"이므로 AppSettings는 Fitserver.exe Lives의 디렉토리에서 "fitserver.exe.config"를 찾고 있습니다. 따라서 빠르고 더러운 솔루션은 App.Config를 거기에 복사하여 이름을 바꾸는 것입니다.
더 나은 솔루션은 여기에 설명 된대로 앱 구성을 지정하는 것입니다. http://www.syterra.com/fitnessedotnet/applicationconfigurationfile.html
또는 FitSharp (Fitnesse.net의 향상)를 사용하는 경우 : http://www.syterra.com/fit/appconfigfiles.html
다른 팁
또한: 사용해보십시오 ConfigurationManager
"configurationsettings"대신 클래스 :
먼저 null이 아닌 수표를 사용하십시오.
public class RxNormFolderMgr
{
// properties
public string RxNormFolder
{
get
{
if(ConfigurationManager.AppSettings["rootFolder"] != null)
{
return ConfigurationManager.AppSettings["rootFolder"].ToString();
}
return string.Empty;
}
}
}
이것은 클래스 라이브러리 어셈블리 내부입니까? 이들은 자신의 app.config를 사용하지 않고 대신 호스트 앱의 app.config (클래스 라이브러리를 사용하는 앱)를 사용합니다.
마크
appsettings에 넣지 마십시오. 사용u003CconnectionStrings>
예시:
u003CappSettings/>
u003CconnectionStrings>n u003Cadd name="NORTHWNDConnectionString" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|NORTHWND.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
u003C/connectionStrings>
문자열 cnstr = configurationManager.connectionStrings [ "NorthwndConnectionString"]. toString ();