【问题标题】:Handling async ASMX web service exceptions处理异步 ASMX Web 服务异常
【发布时间】:2010-04-16 10:23:29
【问题描述】:

我开发了 Silverlight 客户端,它可以对 asmx Web 服务进行异步 Web 服务调用。问题是,我想处理异常,以便能够在客户端应用程序中判断 web 服务中是否存在异常(因此将记录到 web 服务的本地)或是否存在通信问题(即网络服务的端点错误)。

在我的项目中测试这两种类型的异常时,我得到了相同的通用异常:

System.ServiceModel.CommunicationException: The remote server returned an error: NotFound.

当网络服务中发生异常时,这个异常毫无用处,因为它显然已经被发现了。

此一般错误的存在是否与安全性有关(不允许查看真正的错误)?这不可能是因为我在开发 PC 上运行时没有调试字符串。

不管怎样,我的问题是,在商业 silverlight 应用程序中处理异步错误的最佳方法是什么?

欢迎任何链接或想法! :)

非常感谢!

安迪。

【问题讨论】:

标签: c# .net silverlight web-services asmx


【解决方案1】:

是的,一般错误涉及安全性。这个想法是,如果攻击者确实在页面等中发现了错误。这个人不知道错误的原因是什么。

你是否在serviceDebug标签中开启了远程调试?

http://www.mostlydevelopers.com/mostlydevelopers/blog/post/2009/01/14/Debugging-Tips-ndash3b-The-remote-server-returned-an-error-NotFound.aspx

这应该返回一个不太常见的错误。

【讨论】:

  • 啊,这么想,感谢您的链接,但这似乎是在处理 WCF 服务,而不是普通的 asmx(具有 [WebMethod] 属性的服务)。不确定它会起作用!
【解决方案2】:

我认为您实际上可能在这里遇到了 404 错误。如果服务中存在异常但 includeDetailsInException 设置为 false,那么您将得到一个 FaultException,除了 Exception.Message 之外什么都没有。

我认为您需要查看运行服务的机器,看看您的客户收到异常时是否有任何错误或警告。特别是,如果服务在 .NET 2.0 或更高版本上运行,则 ASP.NET 健康监控的默认配置将在发生未处理的异常时将警告消息记录到应用程序事件日志中。

【讨论】:

    【解决方案3】:

    我也试图为此找到“正确”的解决方案。返回原始异常不是一个好主意,所以这是我想出的简化版本:

       public class AjaxResponse
        {            
            public string Message { get; set; }
            public string FullError { get; set; }
            public AjaxResponse() { }
            public AjaxResponse(Exception e) 
            {
                Message = e.Message;
                FullError = e.ToString();
            }
        }
        public class AjaxResponse<T> : AjaxResponse
        {
            public T Response { get; set; }
    
            public AjaxResponse() { }
            public AjaxResponse(Exception e) : base(e) { }
            public AjaxResponse(T resp)
            {
                Response = resp;
            }           
        }
    

    用法:

            [WebMethod]
            public AjaxResponse<T> DoStuff(...)
            {
                try
                {
                    T value = new T(...);
                    return new AjaxResponse<T>(value);
                }
                catch (Exception ex)
                {
                    return new AjaxResponse<T>(ex);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      相关资源
      最近更新 更多