2013-09-26 19 views
0

好吧,昨天我发布了一些我正在开发的系统的一些问题。我现在遇到的问题是,我正在经历最坏的情况,并且认为用户以某种方式尝试调用我的Web服务并忘记放入参数,因此Web服务接收到空参数...我尝试使用If进行验证,但它仍然给我一个ArgumentException错误,所以我离开了if验证,而是我把整个代码放在了一个try-catch,问题是这个“catch没有进入该代码块,它只是一直抛出ArgumentException在普通网页文本...在接收Null参数时在WebService上捕获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; 
    } 

} 

什么我试图d o是得到这个错误信息并把它放在我的数据库中......事情是它从来没有进入CATCH代码,它直接进行并吐出错误。

希望有人可以帮我找到一个办法赶上或验证此NULL参数问题,并把它贴在DB

感谢反正家伙。

回答

2

如果要允许用户传递null,则必须使参数为空,即int。更改签名使用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 
} 
+0

其实我想要的是不允许用户使用空值调用WS ...但同时,如果用户这样做反正,捕捉表中的异常与我的数据库有控制系统内的所有错误。 –

+0

@ user2816654 - 嗯,这是一个语义问题。您想要的是允许使用空参数调用Web服务,但要控制处理并手动记录错误并将错误结果返回给用户。我在答案中概述的方法可以让你做到这一点.. –

+0

我会试试看,谢谢你的建议人:3 –