【问题标题】:WCF Service rest error handling using WebFaultException使用 WebFaultException 处理 WCF 服务休息错误
【发布时间】:2014-08-07 06:40:08
【问题描述】:
我需要处理 WCF 服务应用程序中的异常。
但在 Windows 应用程序中,我无法收到错误消息。它只显示为Bad Request。
在 WCF 服务中抛出以下异常。
throw new WebFaultException<string>(string.Format("Invalid Client ID.", clientID), HttpStatusCode.BadRequest);
【问题讨论】:
标签:
wcf
exception
bad-request
【解决方案1】:
在您的 Windows 应用程序(客户端)中,您必须捕获异常并获取错误的详细信息
try
{
client.YourServiceMethod();
}
catch (FaultException<string> ex)
{
MessageFault messageFault = ex.CreateMessageFault();
Console.WriteLine(messageFault.GetDetail<string>());
}