WCF RESTful Service - POST- WebFaultException complete sample

二次信任 提交于 2019-12-13 03:21:21

问题


I searched so many links but none actually works and most of them doesnot give correct client code. There is something I am missing.

I have below exception class

[DataContract]
 public class MyException
    {
        public MyException(Exception ex)
        {
            Message = ex.Message;
            StackTrace = ex.StackTrace;
            ExceptionType = ex.GetType();
        }

        [DataMember]
        public string Message { get; set; }

        [DataMember]
        public string StackTrace { get; set; }

        //[DataMember]
        // This is having issues. On client side WebException.Response is null
        //public Type ExceptionType { get; set; }

    }

My interface is like below

 [OperationContract]
 [WebInvoke(
    Method = "POST",
    UriTemplate = "/Documents/getallunprinteditems",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json
 )]
 MyResponse MyMethod(MyRequest request);

My implementation is like below

public MyResponse MyMethod(MyRequest request) 
 {
     try
     {
         ... 
         // for testing created a dividebyzero exception
     }  
     catch(Exception ex)
     {
         MyException ex = new MyException(ex);
         throw new WebFaultException<MyException>(ex,
         System.Net.HttpStatusCode.HttpVersionNotSupported);
     }

 }

I have below code at my client side but it always goes to catch block of Exception. What I am missing?

try
 {
   MyRequest upRequest = new MyRequest();
   ....
   MyResponse upResponse = new MyResponse();
   string url = "http://localhost:2023/myservice/getdata";
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   request.Method = "POST";
   request.ContentType = "application/json";
   byte[] data = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(upRequest));
   request.ContentLength = data.Length;
   Stream requestStream = request.GetRequestStream();
   requestStream.Write(data, 0, data.Length);
   requestStream.Close();

   WebResponse response = request.GetResponse();
   Stream respStream = response.GetResponseStream();
   StreamReader reader = new StreamReader(respStream);
   string responseData = reader.ReadToEnd();
   Newtonsoft.Json.JsonConvert.PopulateObject(responseData, upResponse);
   ......
  }
  catch (System.ServiceModel.Web.WebFaultException<MyException> ex)
    {
      throw ex;
    }
 //below is just for test purpose...
    catch (System.ServiceModel.FaultException<MyException> ex)
     {
       throw ex;
     }
    catch (Exception ex)
     {
        throw ex;
     }

UPDATE: How to Handle WebFaultException to return CustomException?

I added below at client side and it is hitting this.

catch (WebException ex)
 {
    if(ex.Response != null) 
    {
       string errorString = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
       MyException ex1 = new MyException();
       Newtonsoft.Json.JsonConvert.PopulateObject(errorString, ex1);
    }
 }

SOLUTION:

The problem was with below property

[DataMember]
public Type ExceptionType { get; set; }

ErrorDetails must be serializable objects and looks like Type is not.

来源:https://stackoverflow.com/questions/53949748/wcf-restful-service-post-webfaultexception-complete-sample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!