【问题标题】:WCF Restful POST Return Error Message from Server to ClientWCF Restful POST 从服务器返回错误消息到客户端
【发布时间】:2017-03-22 07:00:39
【问题描述】:

我需要关于如何将服务器上捕获的错误消息返回给客户端的建议。我正在创建 WCF Restfull 服务并能够在使用 GET 方法时返回错误消息。但很难找到从 POST 方法返回错误消息的正确方法。

已编辑

对于@Abd:

我尝试过使用 throw WebFaultException,如下代码:

 try
                    {
                        newbudgetid = _service.Create(budgettrx);
                    }
                    catch (Exception ex)
                    {
                        error_message = ex.Message;
                        MyCustomErrorDetail customerror = new MyCustomErrorDetail(
"Error", error_message);

                        throw new WebFaultException<MyCustomErrorDetail>(customerror, HttpStatusCode.NotFound);

                    }

这是类定义:

public class MyCustomErrorDetail
    {
    public MyCustomErrorDetail(string errorInfo, string errorDetails)
        {
            ErrorInfo = errorInfo;
            ErrorDetails = errorDetails;
        }
        [DataMember]
        public string ErrorInfo { get; private set; }
        [DataMember]
        public string ErrorDetails { get; private set; }
    }

在客户端代码上,我应该如何编写服务器的错误输出?

 BudgetTransactionRequest bt = new BudgetTransactionRequest
                {
                    transaction_code = "7PRM007690 ",
                    category = "Expenses",
                    claim_status = "Presales ID",
                    amount = "320000.00000",
                    application_type = "Payment Request",
                    opportunity = "BSMD000586",
                    project = null,
                    request_date = new DateTime(2013, 03, 09),
                    request_status = "Validated",
                    owner = "nurul.wiiyanti",
                };




                WebClient proxy = new WebClient();
                proxy.Headers["Content-Type"] = "application/json";
                MemoryStream ms = new MemoryStream();
                DataContractJsonSerializer serialize = new DataContractJsonSerializer(typeof(BudgetTransactionRequest));
                serialize.WriteObject(ms, bt);
                byte[] data = proxy.UploadData("http://10.10.64.19:8082/Service1.svc/CreateBudgetTransaction/", "POST", ms.ToArray());
                Stream stream = new MemoryStream(data);
                DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(BudgetTransactionRequest));

【问题讨论】:

  • 我也有同样的问题
  • 找到解决方案了吗...

标签: c# rest wcf post


【解决方案1】:

您可以创建一个 ServiceResult 类并将其用于所有 Get 和 Post 方法。在这个类中,您可以使用 Generic Result、HasException、ExceptionMessage 等属性。

【讨论】:

    【解决方案2】:

    抛出一个 WebFaultException(T errorDetail, HttpResponseCode 代码)

    在这里您将响应类型设置为另一个对象类型,这很有意义,并且您还设置了所需的 ResponseCode。

    errorDetail必须是可序列化的

    这个link 可能有用,或者这个link

    【讨论】:

    • 在客户端代码上,您只需阅读http状态码“response.StatusCode”获取状态码和“response.Content.ReadAsString()”获取内容
    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多