【问题标题】:Handling an exception thrown by a server running a webservice处理运行 Web 服务的服务器抛出的异常
【发布时间】:2017-08-07 20:48:03
【问题描述】:

我正在使用 C# windows 窗体创建一个工具,它可以快速检查我们公司内部订购网站上的订单状态。我正在尝试处理 Web 请求在尝试打开新 IE 窗口时引发的格式化异常。

我的工具添加了 8 位用户输入并将其添加到 URL 以获取用户特定订单并打开一个新的 IE 窗口以显示它。

private void scRequestBtn_Click(object sender, EventArgs e)
{      
    try
    {
        var scUrl = "http://iwha.mycompany.com/servicesdashboard/InstanceInfoRedirect.aspx?instance_id=";
        var scInput = scUrl += scTextBox.Text;
        Process.Start("IExplore.exe", scInput);
    }
    catch (FormatException)
    {
        MessageBox.Show("Your entry is not valid.  Please verify the ID number");
    }
}

如果输入了 8 位数字以外的信息(例如,如果用户键入字符串而不是整数),我会得到一个带有服务器错误的 IE 窗口。它有一个堆栈跟踪如下:

[FormatException: Input string was not in a correct format.] Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String Value, NumberFormatInfo NumberFormat) +203 Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) +73

[InvalidCastException: Conversion from string "gsdfgsdfgsfg" to type 'Long' is not valid.] Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) +328 ServiceDashboard_41.InstanceInfoRedirect.Page_Load(Object sender, EventArgs e) +86 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

我觉得托管订购网站的内部服务器引发的异常与您看到的典型异常不同。我读过“肥皂”异常可能就是所谓的,但我并不真正了解如何处理。

我的问题是你将如何处理程序之外的东西引发的异常?

【问题讨论】:

  • 垃圾进,垃圾出。清理用户输入以确保其符合他们应该输入的内容。

标签: c# web-services exception-handling


【解决方案1】:

调用服务器/网页时,必须检查 HTTP 返回码。这是检查是否发生错误的唯一方法。

根据我在您的代码中看到的情况,您只能打开 IE 并在您身边输入。你也可以尝试格式化服务器的输入(我想要一个长的,所以你可以检查输入是数字)

在 try catch 语句中包含进程启动将允许您处理错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2020-01-07
    • 2017-06-15
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多