【问题标题】:Catch ArgumentException on WebService when receive Null Parameters接收 Null 参数时在 WebService 上捕获 ArgumentException
【发布时间】:2013-09-26 22:36:59
【问题描述】:

好的,昨天我发布了一些我正在开发的系统遇到的问题。我现在遇到的问题是,我正在经历最坏的情况,并认为用户以某种方式尝试调用我的 Web 服务并忘记输入参数,因此 Web 服务接收到空参数......我尝试使用 @ 进行验证987654321@,但它仍然给我一个ArgumentException 错误,所以我离开了if 验证,而是将整个代码放在try-catch 中,问题是这个“catch 没有进入那个”代码块,它只是在纯网络文本上不断抛出ArgumentException ...

这是我的代码:

[WebMethod(Description = "Private Title", EnableSession = false)]
public string[] M102(int p_enclosure, string p_transaction, int p_operation, int p_commodity,
                          string p_bl, string p_inDate, string p_blhouse, string p_anexo29, int p_tranType,
                          string p_vessel, string p_trip, int p_entry, string p_padlock, string p_brands, 
                          string p_appoint, string p_plates, string p_qmark)
{
    string eDate;
    try
    {/*/WebService Code/*/}
    catch (ArgumentException Ex)
    {
        string message;
        string[] error = 
            { 
                "000",
                "DataCaptureError.- " + Ex.Message,
                "Date"
            };
        SqlConnection con8 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd8 = new SqlCommand();
        cmd8.Connection = con8;
        cmd8.CommandText = "dbo.sp_errorlog";
        cmd8.CommandType = CommandType.StoredProcedure;
        cmd8.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = "0";
        cmd8.Parameters.Add("@p_enclosure", SqlDbType.NChar, 6).Value = "0";
        cmd8.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = "0";
        cmd8.Parameters.Add("@p_method", SqlDbType.NChar, 6).Value = "102";
        cmd8.Parameters.Add("@p_message", SqlDbType.NVarChar, 250).Value = error[1];
        cmd8.Parameters.Add("@vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd8.Parameters.Add("@vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
        con8.Open();
        cmd8.ExecuteNonQuery();
        con8.Close();
        cmd8.Connection.Close();
        message = "" + cmd8.Parameters["@vo_message"].Value;
        eDate = "" + cmd8.Parameters["@vo_errorDate"].Value;
        eDate = Convert.ToDateTime(eDate).ToString("dd/MM/yyyy HH:mm:ss");
        error[2] = eDate;
        return error;
    }

}

我要做的是获取该错误消息并将其放入我的数据库中......问题是它永远不会进入 CATCH 代码,它会直接吐出错误。

希望有人可以帮助我找到一种方法来捕获或验证这个 NULL 参数问题并将其粘贴到数据库中

谢谢大家。

【问题讨论】:

    标签: c# web-services validation isnull argumentexception


    【解决方案1】:

    如果您想允许用户传递 null,您必须使 int 参数可以为 null。将签名更改为使用int? 而不是int

    public string[] M102(int? p_enclosure, string p_transaction, int? p_operation, int? p_commodity,
                          string p_bl, string p_inDate, string p_blhouse, string p_anexo29, int p_tranType,
                          string p_vessel, string p_trip, int? p_entry, string p_padlock, string p_brands, 
                          string p_appoint, string p_plates, string p_qmark)
    

    字符串(和其他引用类型)很好,但任何值类型参数都不能接收空参数,这很可能是发生异常的地方。

    然后,在处理方法体中可以为空的参数时,您必须使用HasValue 属性来测试该参数是否有值,并获得实际值,您将不得不使用Value 属性。

    在方法主体中,您必须手动测试每个参数上的null,如果有任何不应该为空的(即整数)返回错误结果并将其记录到您的系统中。

    // you'd want to add this check early in the method body
    if(!p_enclosure.HasValue || p_transaction == null || !p_operation.HasValue ...
    {
        try{
           // log the error
        }catch{
           // ignore errors from the logging system
        }
    
        //   and stop processing
        return null; // or whatever makes sense in case of invalid arguments
    }
    

    【讨论】:

    • 实际上我想要的是不允许用户使用空值调用 WS ...但同时,如果用户无论如何都这样做,请使用我的数据库在表中捕获该异常控制系统中的所有错误。
    • @user2816654 - 嗯,这是语义问题。您想要的是允许使用空参数调用 Web 服务,但控制处理并手动记录错误并将错误结果返回给用户。我在答案中概述的方法可以让你做到这一点..
    • 好的,所以我尝试将所有参数切换为字符串,这样我可以捕获任何空值......之后我设置了一些 int 本地参数和一些 bool 参数......基本上我recibe字符串参数并通过布尔检查它们是否是数字......然后如果我发现参数有任何问题,我把它放在日志中......不完全是你给我的想法,但它奏效了,你的想法让我深思熟虑。
    • 太棒了。我仍然会说可空类型会更好,因为它们会反映您期望的实际数据类型,但字符串有效..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2020-11-07
    相关资源
    最近更新 更多