如何使用fault contract和jquery ajax处理wcf错误 [英] how to handle wcf errors using fault contract and jquery ajax

查看:80
本文介绍了如何使用fault contract和jquery ajax处理wcf错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我在互联网上搜索了很多但是无法找到解决问题的方法。



问题



我创建了两个应用程序。一个是WCF服务应用程序,另一个是通过jQuery Ajax消耗WCF服务操作的主应用程序。

一切正常。最后,我想通过在WCF服务中添加错误处理代码来完成任务。

现在,我在WCF服务中添加了Fault Contract但是无法理解如何从服务中传递错误直接jQuery ajax。现在,没有传递自定义错误消息,并且正在传递常规服务错误消息错误请求。



WCF服务代码



Hello All,

I searched a lot in the internet but could not be able to find any solution for my problem.

Problem

I have created two applications. One is WCF service application and another one is the main application that consumes WCF service operations through jQuery Ajax.
Everything is working fine. Finally, I want to complete the task by adding code for error handling in WCF service.
Now, I added "Fault Contract" in WCF service but cannot be able to understand how to pass the error from service to jQuery ajax directly. Right now, the custom error message is not being passed and the general service error message "Bad Request" is being passed.

WCF Service Code

[ServiceContract]
public interface IAdmin_UserDet
{
  [OperationContract]
  [FaultContract(typeof(ErrorHandler))]
  string getData();
}

[DataContract]
public class ErrorHandler
{
   [DataMember]
   public string message { get; set; }
   [DataMember]
   public string methodName { get; set; }
   [DataMember]
   public int lineNumber { get; set; }
   [DataMember]
   public string errorDateTime { get; set; }
}

[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Admin_UserDet : IAdmin_UserDet
{
    [WebInvoke(Method = "GET", UriTemplate = "returnData", ResponseFormat =        WebMessageFormat.Json)]
    public string getData()
    {
        try
        {
            int num1 = 2;
            int num2 = 0;

            int res = num1/num2;
            return res.ToString();
        }
        //catch(FaultException<ErrorHandler> objErr)
        catch(Exception ex)
        {
            ErrorHandler objErr = new ErrorHandler();
            objErr.message = "division by zero";
            objErr.methodName = "getData";
            //return objErr.Detail.message + " | " + objErr.Detail.methodName;
            throw new FaultException<ErrorHandler>(objErr);
        }
    }
}





jQuery Ajax代码(通过它我打电话给服务)





jQuery Ajax Code (through which I am calling the service)

var wcfUrl = "http://localhost/VMService/Admin_UserDet.svc/";

$('button[id=btnTest]').click(function () {
    $.ajax({
        type: "GET",
        url: wcfUrl + "returnData",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (response) {
           alert("no error");
           console.log(response);
        },
        error: function (e) {
          console.log(JSON.parse(e.responseText));
     }
  });
});





运行应用程序后我收到的错误消息



Error Message which I am getting after running the application

GET http://localhost/VMService/Admin_UserDet.svc/returnData 400 (Bad Request)





但是,我希望错误信息为除零| getData,它已在getData方法中设置。



请指导我的确切方式/任何链接,我可以从哪里了解如何处理来自jquery ajax的WCF错误。



谢谢。



But, I want the error message as "division by zero | getData", which has been set in getData Method.

Please guide me the exact way / any link from where I might get an idea about how to handle WCF errors from jquery ajax.

Thank you.

推荐答案

' button [id = btnTest]')。click( function (){
('button[id=btnTest]').click(function () {


.ajax({
type: 获取
url:wcfUrl + returnData
dataType: json
contentType: 应用/ JSON; charset = utf-8,
成功: function (响应){
alert( 无错误);
console .log(响应) ;
},
错误:功能(e){
console .log( JSON .parse(e.responseText));
}
});
});
.ajax({ type: "GET", url: wcfUrl + "returnData", dataType: "json", contentType: "application/json; charset=utf-8", success: function (response) { alert("no error"); console.log(response); }, error: function (e) { console.log(JSON.parse(e.responseText)); } }); });





运行应用程序后我收到的错误消息



Error Message which I am getting after running the application

GET http://localhost/VMService/Admin_UserDet.svc/returnData 400 (Bad Request)





但是,我希望错误信息为除零| getData,已在getData方法中设置。



请指导我从哪里获取有关如何处理WCF错误的确切方法/链接jquery ajax。



谢谢。



But, I want the error message as "division by zero | getData", which has been set in getData Method.

Please guide me the exact way / any link from where I might get an idea about how to handle WCF errors from jquery ajax.

Thank you.


这篇关于如何使用fault contract和jquery ajax处理wcf错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆