什么是等价的SoapExtension对JSON的WebMethods? [英] What is the equivalent to SoapExtension for JSON WebMethods?

查看:161
本文介绍了什么是等价的SoapExtension对JSON的WebMethods?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我用它来调用一些外部服务,如谷歌日历API几个Web方法,显然这些可以是非常易碎。

不幸的是我现在认识到,扔在这些方法的任何错误都不会引起异常泡到Global.asax中这也正是在这个应用越来越记录错误。

我见过的建议来包装一个try / catch的方法,这是做它,因为有各种各样的错误是ASP.Net会悄悄地吞下仍然是一个笨方法。

在试图找到我见过很多引用了的SoapExtension 的解决方案,这正是我想做的事情,但因为我没有被解雇中号返回JSON。我真正想要的是捕获错误就是这样的一种方式。

任何指针AP preciated,我仍然无法理解的ASP.Net团队如何能想到,默默吞咽这样的错误,这是一个好主意。

因此​​,例如,像这样的方法:

  [的WebMethod]
    [ExceptionHandling] //我可以写这样的处理,从JSON Web服务捕获异常?
    静态公共无效DeleteItem(字符串ID)
    {
        VAR API =新GoogleCalendarAPI(User.InternalUser());
        api.DeleteEvent(ID);
        返回成功;
    }


解决方案

有没有等同于的SoapExtension 对JSON的WebMethods,并具有自定义错误在生产站点开启后就会结果在一个一般错误消息被返回到客户端,没有错误提出过在服务器上。你无法绕开这一点。

如果您检查使用类似ILSpy的code,有没有办法通过像的SoapExtension 方法或类页的WebMethods。该错误是由ASP.Net,因为它调用Web方法吞下,你会得到唯一的通知发送到客户端总一般错误消息的HTTP 500错误。

在4.0,得到的WebMethods由这家名为:

  // System.Web.Script.Services.RestHandler
内部静态无效ExecuteWebServiceCall(HttpContext的背景下,WebServiceMethodData methodData)
{
    尝试
    {
        //剪断无关code
        RestHandler.InvokeMethod(背景下,methodData,rawParams);
    }
    赶上(异常前)
    {
        RestHandler.WriteExceptionJsonString(背景下,前);
    }
}

因此​​,如果调用你的方法抛出一个错误,它会调用以下code与500状态code,就没有再扔在那里,没有别的,你可以通过调用,所以,除非我失明它只是静静地被吞噬。即使你已经变得更糟自定义错误打开,其中任何理智的人都会,它会完全混淆原来的原因:

  // System.Web.Script.Services.RestHandler
内部静态无效WriteExceptionJsonString(HttpContext的背景下,异常前,INT状态code)
{
    //剪断code设置响应
    context.Response.TrySkipIisCustomErrors = TRUE;
    使用(的StreamWriter的StreamWriter =新的StreamWriter(context.Response.OutputStream,新UTF8Encoding(假)))
    {
        如果(当然是TargetInvocationException)
        {
            EX = ex.InnerException;
        }
        如果(context.IsCustomErrorEnabled)
        {
            streamWriter.Write(JavaScriptSerializer.SerializeInternal(RestHandler.BuildWebServiceError(AtlasWeb.WebService_Error,的String.Empty,的String.Empty)));
        }
        其他
        {
            streamWriter.Write(JavaScriptSerializer.SerializeInternal(RestHandler.BuildWebServiceError(ex.Message, ex.StackTrace,ex.GetType()全名)))。
        }
        streamWriter.Flush();
    }
}

我看不到它周围的方式,貌似是的WebMethod没有准备好生产code,无地自容。

I've a few web methods that I use to call some external services like the Google Calendar API, obviously these can be extremely brittle.

Unfortunately I now realise that any error thrown on these methods are not causing an exception to bubble up to Global.asax which is where errors are getting logged in this application.

I have seen suggestions to wrap the method in a try/catch, which is a stupid way of doing it as there are a variety of errors that ASP.Net will silently swallow still.

In trying to find a solution I've seen a lot of references to SoapExtension, which is exactly what I want to do but doesn't get fired as I'm returning Json. What I really want is a way to catch the error just like that.

Any pointers appreciated, I still can't understand how the ASP.Net team could have thought that silently swallowing errors like this was a bright idea.

So for example a method like this:

    [WebMethod]
    [ExceptionHandling] //can I write a handler like this to catch exceptions from JSON webservices?
    static public void DeleteItem(string id)
    {
        var api = new GoogleCalendarAPI(User.InternalUser());
        api.DeleteEvent(id);
        return "success";
    }

解决方案

There is no equivalent to SoapExtension for JSON WebMethods and having custom errors turned on in your production site will result in a generic error message being returned to the client, no error is ever raised on the server. You cannot circumvent this.

If you inspect the code using something like ILSpy, there is no way to pass a method or class to page WebMethods like SoapExtension. The error is swallowed by ASP.Net as it invokes the web method, the only notification you will get is a HTTP 500 error sent to the client with a total generic error message.

In 4.0, WebMethods get called by this:

// System.Web.Script.Services.RestHandler
internal static void ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)
{
    try
    {
        //snip irrelevant code
        RestHandler.InvokeMethod(context, methodData, rawParams);
    }
    catch (Exception ex)
    {
        RestHandler.WriteExceptionJsonString(context, ex);
    }
}

So if invoking your method throws an error it will call the following code with a statusCode of 500, there's no re-throw in there and nothing else you can pass in called so unless I'm being blind it just gets swallowed silently. Even worse if you've got custom errors turned on, which any sane person will, it'll completely obfuscate the original cause:

// System.Web.Script.Services.RestHandler
internal static void WriteExceptionJsonString(HttpContext context, Exception ex, int statusCode)
{
    //snip code setting up response
    context.Response.TrySkipIisCustomErrors = true;
    using (StreamWriter streamWriter = new StreamWriter(context.Response.OutputStream, new UTF8Encoding(false)))
    {
        if (ex is TargetInvocationException)
        {
            ex = ex.InnerException;
        }
        if (context.IsCustomErrorEnabled)
        {
            streamWriter.Write(JavaScriptSerializer.SerializeInternal(RestHandler.BuildWebServiceError(AtlasWeb.WebService_Error, string.Empty, string.Empty)));
        }
        else
        {
            streamWriter.Write(JavaScriptSerializer.SerializeInternal(RestHandler.BuildWebServiceError(ex.Message, ex.StackTrace, ex.GetType().FullName)));
        }
        streamWriter.Flush();
    }
}

I can't see a way around it, looks like WebMethod is not ready for production code, shame.

这篇关于什么是等价的SoapExtension对JSON的WebMethods?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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