简体   繁体   English

ArgumentNullException C#WebMethods

[英]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. 因此,我试图强制用户将值放入WebService中……但是,如果用户发送一个或多个null参数,则此WebService只会发回一个错误。

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: 因此,如果用户调用此WebService,例如,他忘记了一个参数并将其保留为空,则用户将获得如下所示的信息:

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 . 在这种情况下,您可以使用可为null的类型 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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