views:

66

answers:

3

I have a WebMethod that receives following types of parameters:

[WebMethod]
User(long userid,int number)

When Client sends parameter with different types from I want, I have to catch this error and write to database etc.

For example ArgumentExceptions...

How can I solve this?

Thanks.

+1  A: 

Have you tried what happens when a client uses the wrong types?

I would expect the SOAP library to trap this and raise an exception.

Inside your own method you can only check the values of the incoming parameters. And that works just like inside a normal (non-web) method.

Henk Holterman
thats exactly what I mean. I couldnt find handling methods in SOAP Extensions.
Jack
A: 

You can use the following approach:

Each web method will always return some sort of WebMethodResult.

For example

[WebMethod]
public WebMethodResult DoSomethng(guid p_userId)
{

IfMethodIsSuccessful()
{
    WebMethodResultSuccess successResult = new WebMethod();
    // Add required information into web method result object

   return successResult;
}
else
{
   WebMethodResultFailure failedResult = new WebMethodResultFailure();
   return failedResult;
}

}

Idea here is that whenever web method is called it will return some sort of object.

In this case the WebMethodResult will be the parent class and WebMethodResultSuccess and WebMethodResult failure would inherit from the parent class.

Instead of IsMethodIsSuccessfull you can add your own logic, wrap everything into a try catch block and return success/failure results.

If you call the web methods from the java script or jquery, the exception won't be passed back to the client unless you use SOAP or some sort of alternative. By sending back custom objects, you can read them through javascrip/jquery and display appropriate message to the user.

Hope this makes sense.

vikp
A: 

In terms of logging, you should probably write a generic exception handling layer or look into open source alternatives.

You woulc catch specific exception and pass it to exception handling layer. Within that layer (dll) you'll log the exception either in the database or write into a flat file. DB is probably a better option since the data can be easily analysed.

Another alternative is to log into windows event log, but I personally don't like this because it's not that easy to produce statistics on the exceptions and I think it has a limit in size.

vikp