How to handle InvalidOperationException in web service

  Kiến thức lập trình

I have a web service, I need a way to handle InvalidOperationException.
For example, when a user call my service and forget to pass a parameter, currently by default, it returns the below exception:

System.InvalidOperationException: Missing parameter: StudentId. at
System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection
collection) at
System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at
System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Another example, When a user call a method that doesn’t exist in MyService
https://localhost/Services/MyService.asmx/InvalidMethod
it returns the below exception:

System.InvalidOperationException: invalidmethod Web Service method
name is not valid. at
System.Web.Services.Protocols.HttpServerProtocol.Initialize() at
System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,
HttpContext context, HttpRequest request, HttpResponse response,
Boolean& abortProcessing)

it returns 500 Internal Server Error in both cases, Is there away to return different status code and show a different message to the user? How to catch this kind of exceptions?

I read some articles and I found something called SoapExtension but I am not sure if it is was designed for such purposes.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT