• WebService客户端调用错误处理


    错误处理
    •调用时可以提供一个额外的错误回调函数
    •包括超时和服务器端抛出的异常
    •超时只能设置在WebService级别
    –或者设置在PageMethods对象上
    –无法在每个MethodCall时指定
    •Sys.Net.WebServiceError
    –timedout、message、exceptionType、stackTrace属性


    aspx
        <form id="form1" runat="server">
            
    <asp:ScriptManager ID="ScriptManager1" runat="server">
                
    <Services>
                    
    <asp:ServiceReference Path="ErrorHandling.asmx" />
                
    </Services>
            
    </asp:ScriptManager>
            
            
    <input type="button" value="getDivision" onclick="getDivision(5, 0)" />
            
    <input type="button" value="timeout" onclick="timeout()" />
            
            
    <script language="javascript" type="text/javascript">
                function getDivision(a, b)
                {
                    ErrorHandling.GetDivision(a, b, 
    null, failedCallback);
                }
                
                function timeout()
                {
                    ErrorHandling.set_timeout(
    2000);
                    ErrorHandling.Timeout(
    null, failedCallback);
                }
                
                function failedCallback(error)
                {
                    var message 
    = String.format(
                        
    "Timeout: {0}\nMessage: {1}\nExceptionType: {2}\nStackTrace: {3}",
                        error.get_timedOut(),
                        error.get_message(),
                        error.get_exceptionType(),
                        error.get_stackTrace());
                
                    alert(message);
                }
            
    </script>
        
    </form>

    cs
        protected void Page_Load(object sender, EventArgs e)
        {

        }

    ErrorHandling.asmx
    <%@ WebService Language="C#" Class="ErrorHandling" %>

    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Web.Script.Services;
    using System.Threading;

    [WebService(Namespace 
    = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class ErrorHandling  : System.Web.Services.WebService
    {
        [WebMethod]
        
    public int GetDivision(int a, int b)
        {
            
    return a / b;
        }

        [WebMethod]
        
    public int Timeout()
        {
            Thread.Sleep(
    5000);
            
    return 0;
        }
    }
  • 相关阅读:
    bzoj4028 [HEOI2015]公约数数列
    bzoj4766 文艺计算姬
    bzoj4241 历史研究
    bzoj3744 Gty的妹子序列
    bzoj4540 [Hnoi2016]序列
    uoj#228 基础数据结构练习题
    bzoj2467 [中山市选2010]生成树
    bzoj2125 最短路
    bzoj4800 [Ceoi2015]Ice Hockey World Championship
    bzoj2463 [中山市选2009]谁能赢呢?
  • 原文地址:https://www.cnblogs.com/timy/p/1172846.html
Copyright © 2020-2023  润新知