Flex webservice error handeling
While working on a top-secret project with a Flex GUI we ran into a big issue. There is a known bug in flex that prevents the client from receiving the proper webservice error response from the server. Without using the Flex proxy server you cannot read any response from a webservice that has an HTTP Response Code other then 200. After considering how to work around this for a while it came to me, using the Context object in C#/.NET you can set the HTTP Response Code as well as other items in the HTTP Response headers. We put it in the Finally of our exception handling to set the Context HTTP Response Code to 200. We started debugging this and saw the HTTP Response Code change, however for some reason after the method finished executing it changed the error back to 500. This at least let us know we were on the right track it just wasn’t sticking.
Thank GOD for all those stupid MSDN charts I got with my subscription. I quickly found the solution to the second issue. I added this line to my global.asax file and now it works perfectly!
protected void Application_EndRequest(object sender,EventArgs e)
{
if(Context.Response.StatusCode==500 || Context.Response.StatusCode == 300)
{
Context.Response.StatusCode = 200;
}
}
The application calls the Application_EndRequest at the end of each request to the webservice. While this is not the most graceful way to do this, you can easily add a Header to the Context in the method (in the finally of the try block) that sets a flag whether it should change the StatusCode.
Update #1 - 11/1/2007
Just for reference your code at the highest level should be throwing SoapException’s like the following….
throw new SoapException("Text for the Soap Error goes here", SoapException.ClientFaultCode)
you can also change it to
throw new SoapException("Text for the Soap Error goes here", SoapException.ServerFaultCode)
You should set it to ClientFaultCode v. ServerFaultCode to specify if it was an error that the server caused or if it was the result of something the client did. You can also include the exception that your catch() caught like this
throw new SoapException("Text for the Soap Error goes here", SoapException.ServerFaultCode, exp)
SoapExceptions are what Flex is expecting and any other format might cause issues with Flex being able to interpret it.














December 7th, 2007 at 3:07 pm
Context.Response.StatusCode == 500 && Context.Response.StatusCode == 300
should be…
Context.Response.StatusCode == 500 || Context.Response.StatusCode == 300
December 11th, 2007 at 8:57 am
Fixed! Thanks for the input.