WCF REST服务| WebFaultException处理

时间:2016-04-05 11:25:18

标签: wcf rest wcf-binding webhttpbinding

我正在测试WCF REST服务以抛出WebFaultException,其中包含错误详细信息(创建了一个ExceptionsView类来保存错误详细信息)和httpStatusCode.BadRequest。

从客户端我创建了一个代理来访问此WCF服务。但我无法将此WebfaultException从服务传递给客户端。

它在Catch {exception ex}语句中返回错误为" 404 Bad Request"

我使用了WebHttpBinding作为bindingType

以下是代码的详细信息,

ExceptionsView Class

[DataContract]
    public class ExceptionsView
    {
        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string ErrorId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string Reason { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string DetailedInformation { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string stackTrace { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string Message { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string Source { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        public string MethodName { get; set; }
    }

界面:

[WebInvoke(Method = "GET", UriTemplate = "/GenerateWebFaultException?id={id}", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string GenerateWebFaultException(string id);

*

此Inteface的实现

public string GenerateWebFaultException(string id)
{
    throw new WebFaultException<ExceptionsView>(new ExceptionsView { ErrorId = "123", MethodName = "GenerateWebFaultException", Message = "There was WebFault exception generated", Reason = "WebFaultException" }, HttpStatusCode.BadRequest);
return "exception";
}

上面我只是抛出一个WebFaultException,包含所有错误细节。

现在在客户端我使用Proxy访问此服务,如下所示

var exampleProxy = ServiceHelper.GetServiceChannel<IExample>();

try
{
    var serviceResult = exampleProxy.GenerateWebFaultException("1");
}
catch(Exception ex)
{
    // Here i am unable to get any error details.
    // 400 Bad Request error is returned in response.
   // Can you help me how to get the proper error details from the WCF REST Service to the client
}

如果有人对此有任何解决方案,请告诉我。 过去3天我一直在摸不着头脑。 请帮帮我们..

下面的图片显示了小提琴手的结果。

enter image description here

actual error in Business Class (Interface Implementation)

Error which is displayed on the client side

enter image description here

0 个答案:

没有答案