WCF REST POST服务中的错误消息

时间:2017-03-14 05:59:01

标签: c# wcf

是否可以为POST方法返回错误消息? 对于GET方法,我使用WebFaultException返回错误消息。

以下是使用POST服务的客户端代码:

try
            {
                BudgetTransactionRequest bt = new BudgetTransactionRequest
                {
                    transaction_code = "MHSS112",
                    category = "Expenses",
                    amount = 12345,
                    claim_status = "Propose Re-Class",
                    application_type = "test saja",
                    request_date = new DateTime(2013, 03, 09),
                    request_status = "Validated",
                    owner = "nurul.widiyanti", 
                };
                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://localhost:1910/Service1.svc/CreateBudgetTransaction/MLPT03", "POST", ms.ToArray());
                Stream stream = new MemoryStream(data);
                DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(BudgetTransactionRequest));
                Console.WriteLine("Success !!");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR : " + ex);
                Console.ReadKey();
            }

有一个所有者字符串,其中包含用户名。当我填写错误的用户名。它会返回错误的请求。同时,在服务器代码中。如果某些字段错误,我放置了try catch块来返回异常。不,我希望客户端可以从服务器代码中获取异常。有可能吗?

0 个答案:

没有答案