저는 C#을 사용하여 서비스 작업을 구축하고 있습니다. 무언가가 잘못 될 경우 클라이언트 측을 잡을 수있는 예외를 던지고 싶습니다.서비스 작업에 대한 예외적 인 예외
그러나 예외가 throw되면 클라이언트는 "400 : bad request"와 같은 일반적인 오류 만 가져올 수 있으며 예외 메시지에 액세스 할 수 없습니다. 내 서비스 운영에
, 나는이 함께 자세한 오류를 활성화 :[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
and
config.useVerboseErrors = true;
나는 또한 TargetInvocationException 풀고 대신이 기능으로 DataServiceException을 반환 :
protected override void HandleException(HandleExceptionArgs args)
{
// Handle exceptions raised in service operations.
if (args.Exception.GetType() == typeof(TargetInvocationException)
&& args.Exception.InnerException != null)
{
if (args.Exception.InnerException.GetType() == typeof(DataServiceException))
{
// Unpack the DataServiceException.
args.UseVerboseErrors = true;
args.Exception = args.Exception.InnerException as DataServiceException;
}
else
{
// Return a new DataServiceException as "400: bad request."
args.UseVerboseErrors = true;
args.Exception = new DataServiceException(400, args.Exception.InnerException.Message);
}
}
}
내가 브라우저를 사용 , 자세한 예외 메시지를 볼 수 있지만 프로그래밍 방식으로 시도 할 때 내부 예외가 null이며 "400 : 잘못된 요청"일반 오류 메시지를 참조하십시오.
이상하게도 400 대신 코드 200을 반환하면 답변 본문에 예외 메시지가 표시됩니다. 그러나 분명히 나는 이것을하고 싶지 않습니다.
그래서 서비스 작업에서 예외를 throw 할 때 예외 메시지 클라이언트 측을 가져 오는 방법이 있습니까?