2016-07-26 2 views
0

app.config 파일에 key가 없을 때 컴파일 오류가 발생하도록 PostSharp 애스펙트를 만들었지 만 CompileTimeValidate 메소드에서 Checking 할 때 ConfigurationManager.AppSettings.HasKeys()가 "false"를 반환합니다. 나를 도와 줄 수 있니? 감사!Postsharp - App Settings 구성 확인

[Serializable] 
public class ConfigurationValueRequiredAttribute : MethodInterceptionAspect 
{ 
    // Method invoked at build time. It validates that the aspect has been applied to an acceptable method. 
    public override bool CompileTimeValidate(MethodBase method) 
    { 
     bool valid = true; 

     if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["Test"])) 
     { 

      Message.Write(SeverityType.Error, "CUSTOM02", "Test not exists!"); 
      valid = false; 
     } 

     return valid; 
    } 
} 
+0

키를 확인하면 어떻게됩니까? app.config가 컴파일 시간에로드 될 것이라고 생각하지 않습니다. app.config를 수동으로로드하거나 app.config를 XML로로드하고 키가 존재하는지 확인하기 위해 일부 xpath를 수행 할 수 있습니다. – Michael

답변

1

PostSharp 컴파일 타임 검사는 별도의 프로세스입니다. 응용 프로그램 컨텍스트 내에서 실행되지 않고 app.config를로드하거나 읽지 않습니다.

확인하려면 테스트 클래스에서 특성을 사용하고 "테스트가 없습니다!"라는 오류 메시지가 나타납니다.

가 그럼 난 C이 추가 : \ 경우 ProgramData \ PostSharp \ 4.2.29 \ bin.Release \ postsharp-net40-86-srv.exe.config

<appSettings> 
    <add key="test" value="123"/> 
</appSettings> 

... 다시 구축하고, 오류가 사라졌습니다. 그것이 .config 파일입니다.