I'm working with a client who has a web method like this:
[WebMethod]
public XmlDocument Send(string stuff)
{
// ...
}
At present, there's a class of exceptions which occur which the code is re-throwing, triggering ASP.Net's standard handling of exceptions.
We'd like to change it so that the webmethod still returns status code 500, but with some text/plain
diagnostic information we provide rather than the default ASP.Net stuff.
What's the appropriate way to do that?
I've made it work, using Context.Response.End
like this:
[WebMethod]
public XmlDocument Send(string stuff)
{
try
{
// ...normal processing...
return xmlDocument;
}
catch (RelevantException)
{
// ...irrelevant cleanup...
// Send error
Context.Response.StatusCode = 500;
Context.Response.Headers.Add("Content-Type", "text/plain");
Context.Response.Write("...diagnostic information here...");
Context.Response.End();
return null;
}
}
But that feels hacky, so I'm hoping there's a better answer.
But that feels hacky, so I'm hoping there's a better answer.
It feels hacky because it is hacky.
The better answer is: Return XML, like you said you would, with whatever information it is you want to include. The service returns XML, it was intended for code, not people, to consume.
[WebMethod]
public XmlDocument Send(string stuff)
{
try
{
// ...normal processing, creates xmlDocument...
return xmlDocument;
}
catch (RelevantException)
{
// ...irrelevant cleanup...
// ...error processing, creates xmlDocument...
Context.Response.StatusCode = 500;
return xmlDocument;
}
}