Simon Johnson
Simon Johnson

Reputation: 7912

Capturing exceptions during request parsing

I have an ASP.NET webservice and some of the fields in the request are defined as enums. When entering a blank or invalid value, the response comes back as:

Parameter name: type ---> System.ArgumentException: Must specify valid information for parsing in the string.
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Is it possible to capture errors like this and return an XML based response instead?

Upvotes: 2

Views: 860

Answers (2)

John Saunders
John Saunders

Reputation: 161831

No, there's no way to do this with ASMX web services.

Naturally, you can do this with WCF.

Of course, it would be better if your client sent valid data. You might want to find out why they aren't.

Upvotes: 2

m.edmondson
m.edmondson

Reputation: 30922

Sure, it would look something like this:

try
{
    Enum.Parse(Type enumType, String value, Boolean ignoreCase)
}
catch (ArgumentException e)
{
   //Serialise exception information from 'e' into XML
   //(not shown here) and set it as the response
   Response.Write(xmlMessage);
   Response.End();
}

Upvotes: -1

Related Questions