Program Tip

WCF에 대한 코드에서 IncludeExceptionDetailInFaults를 true로 설정합니다.

programtip 2020. 11. 4. 08:17
반응형

WCF에 대한 코드에서 IncludeExceptionDetailInFaults를 true로 설정합니다.


App.Config를 사용하지 않고 코드에서 IncludeExceptionDetailInFaults를 어떻게 설정합니까?


예, 물론입니다-서비스 호스트를 열기 전에 서버 측에서. 그러나 이렇게하려면 WCF 서비스를 자체 호스팅해야합니다. IIS 호스팅 시나리오에서는 작동하지 않습니다.

ServiceHost host = new ServiceHost(typeof(MyWCFService));

ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();

// if not found - add behavior with setting turned on 
if (debug == null)
{
    host.Description.Behaviors.Add(
         new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{  
    // make sure setting is turned ON
    if (!debug.IncludeExceptionDetailInFaults)
    {
        debug.IncludeExceptionDetailInFaults = true;
    }
}

host.Open();

IIS 호스팅에서 동일한 작업을 수행해야하는 경우 이러한 사용자 지정 서비스 호스트를 인스턴스화 MyServiceHost하는 고유 한 사용자 지정 하위 항목과 적합한 항목 을 만들고 MyServiceHostFactory* .svc 파일에서이 사용자 지정 서비스 호스트 팩토리를 참조해야합니다.


인터페이스를 상속하는 클래스 선언 위의 [ServiceBehavior] 태그에서 설정할 수도 있습니다.

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}

참고 URL : https://stackoverflow.com/questions/2483178/set-includeexceptiondetailinfaults-to-true-in-code-for-wcf

반응형