处理异步ASMX Web服务异常

时间:2010-04-16 10:23:30

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

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

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

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

当Web服务中发生异常时,这个例外显然是无用的,因为它已被发现。

是否存在此通用错误与安全性(不允许查看真正的错误)?因为我在开发PC上运行,所以我没有调试字符串这一事实。

无论哪种方式,我的问题是,在商业Silverlight应用程序中处理异步错误的最佳方法是什么?

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

非常感谢!

安迪。

3 个答案:

答案 0 :(得分:0)

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

您是否已在serviceDebug标记中启用远程调试?

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

这应该返回一般性错误。

答案 1 :(得分:0)

我想你实际上可能会在这里收到404错误。如果服务中存在异常,但includeDetailsInException设置为false,那么除了FaultException之外,您只能获得Exception.Message

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

答案 2 :(得分:0)

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

   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);
            }
        }