ArgumentNullException C#WebMethods

时间:2014-11-28 23:26:30

标签: c# web-services exception-handling null try-catch

所以我试图强制用户将值放在WebService中......但是如果用户发送了一个或多个空参数,那么这个WebService只会发回错误。

关键在于,我想抓住这个错误并将其保存在日志中,这或多或少是我现在所拥有的:

string[] ResponseArray = new string[2];


[WebMethod(Description = "blahblahblah", EnableSession = false)]
public string[] exampleWS(int param_1, int param_2, ... , int param_n)
{
    try
    {
        "WSLogic and code"
    }
    catch(Exception Ex)
    {
       "Code to call error log (custom made error log for DB)"
       ResponseArray[0] = Ex.Message;
       ResponseArray[1] = "Error's Date and Time";
    }
}

因此,如果用户调用此WebService,例如,他忘记了一个参数并将其保留为null,则用户会得到如下内容:

Cannot convert into System.Int32.
Parameter name: type -------> blah blah error message blah blah

我想要做的是,实际捕获此消息并调用日志并注册此错误,但我不知道如何在这种情况下调用异常,因为它甚至没有输入Try / Catch代码。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以使用nullable types。 例如:

public string[] exampleWS(int? param_1, int? param_2, ... , int? param_n)