简体   繁体   中英

ArgumentNullException C# WebMethods

So i am trying to force the user to put values in the WebService ... But if the user sends one or more null parameters, this WebService just sends back an error.

The point is this, i want to catch that error and save it in a log, and this is more or less what i currently have:

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";
    }
}

So if the user calls this WebService and for example, he forgets one parameter and leaves it null, user gets something like this:

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

What i want to do, is actually catch this message and call the log and register this error, but i do not know how to call an exception in this case since it does not even enter the Try/Catch code.

In this case you may use nullable types . For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM