WCF Bad Request
I’ve just identified a horrible bug in WCF for the .NET Framework 3.5.
A caught exception in a WebInvoke operation will cause WCF to return an HTTP 400 Bad Request status code to the client. Any caught exception. Every time. Regardless of whatever error code you might want to send back.
I found the error by mistake. I’d used “BadGateway” instead of “BadRequest” in my code. If it weren’t for other odd WCF behavior, I wouldn’t have noticed that my status code was being ignored.
Consider the following example:
-
// Read the Xml into our object and save.
-
try
-
{
-
// The following line triggered the error.
-
obj.ReadXml(reader);
-
obj.Save();
-
// Set HTTP Cache Options and MIME Type.
-
Utilities.SetCaching(WebOperationContext.Current, obj.DateModified, 60);
-
Utilities.SetMimeType(Format.Xml);
-
return Utilities.GetXmlStream(obj);
-
}
-
catch (Exception e)
-
{
-
// Was it a schema validation error? If so, provide detail.
-
if (!string.IsNullOrEmpty(_XmlValidationErrors))
-
{
-
// I slipped here, using BadGateway 502 instead of Bad Request 400.
-
// But WCF doesn't care. If you enter the catch block it's _always_ 400.
-
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadGateway;
-
WebOperationContext.Current.OutgoingResponse.StatusDescription = _XmlValidationErrors;
-
WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = false;
-
return null;
-
}
-
}
If no error occurs, WCF will return the status code you specify. A try/finally block will work just fine; WCF returns whatever status code you specify. Enter a catch block, though, and it’s nothing but 400 Bad Request.
Hey, if there’s an error, it must be the client’s fault, right?
Gordon said,
Wrote on April 9, 2008 @ 11:34 pm
Grrr..
It seems like it’s “by design” in the webhttp binding.
Apparently you’re supposed to override the webhttpbinding and implement your own custom error handler.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2248288&SiteID=1
As if we needed more proof that the webhttp binding was an afterthought to the ultra soapy WCF anyway… * sigh *
Simon said,
Wrote on April 10, 2008 @ 10:19 am
Nice — I like how they assume that the server getting an unhandled exception is actually the fault of the client.
Most other environments manage to make do with 500 errors for that sort of thing
WCF Instance Context « Infovark Underground said,
Wrote on May 21, 2008 @ 10:31 pm
[...] finally figured out the source of my bad request problem. Apparently the Windows Communication Foundation deals with exceptions differently [...]