Capturing exceptions during request parsing
I have an ASP.NET webservice and some of the fields in the requ开发者_C百科est 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?
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.
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();
}
精彩评论