AJAX WebService in ASP.Net - 如何进行全局错误处理?

AJAX WebService in ASP.Net - how to global error handling?

我的 *.aspx 站点中有大约 50 多个 AJAX WebMethod,由 JQuery 调用(有时是原始 jquery,有时是 lib 调用)。

这里有几个例子:

 [WebMethod]
    public static string GetLog()
    {
        DAL.LogService log = new DAL.LogService();
        string items = log.GetLog();
        return items;
    }

    [WebMethod]
    public static void ClearBenchmarks()
    {
        DAL.LogService log = new DAL.LogService();
        log.ClearBenchmarks();
    }

    [WebMethod]
    public static void WriteLog(string message1, string message2, string user, string type)
    {
        DAL.LogService.WriteLog(message1, message2, user, type);
    }

事实上,大多数时候,它们只重定向到我应用程序的数据层,但实际上,ofc,我有 50 多个单一方法...

现在我想在我的应用程序中包含错误处理 - 如何在不在每个方法中都使用 try catch 的情况下进行全局处理?

这是 .Net 中的一个已知问题 - Application_Error of global.asax 从未触发 Web 服务。

所以你可以尝试以下想法。

  1. 您可以在每个 Web 服务方法周围放置一个 try-catch

  2. 使用外观设计模式并在父级中包含 try-catch 对象

  3. 编写自定义 SOAP 扩展或 HTTPModule

希望对您有所帮助