Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← The Address Bar as the New Command Line
    • Review: Restful Web Services →

    WCF Bad Request

    09 Apr 2008 by Dean in .NET, REST, WCF, Web Services / 5 Comments

    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:

    1.  // Read the Xml into our object and save.
    2.  try
    3.  {
    4.   // The following line triggered the error.
    5.   obj.ReadXml(reader);
    6.   obj.Save();
    7.   // Set HTTP Cache Options and MIME Type.
    8.   Utilities.SetCaching(WebOperationContext.Current, obj.DateModified, 60);
    9.   Utilities.SetMimeType(Format.Xml);
    10.   return Utilities.GetXmlStream(obj);
    11.  }
    12.  catch (Exception e)
    13.  {
    14.   // Was it a schema validation error? If so, provide detail.
    15.   if (!string.IsNullOrEmpty(_XmlValidationErrors))
    16.   {
    17.    // I slipped here, using BadGateway 502 instead of Bad Request 400.
    18.    // But WCF doesn't care. If you enter the catch block it's _always_ 400.
    19.    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadGateway;
    20.    WebOperationContext.Current.OutgoingResponse.StatusDescription = _XmlValidationErrors;
    21.    WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = false;
    22.    return null;
    23.   }
    24.  }

    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?

    Related posts

    1. REST: How to respond to an HTTP POST
    2. WCF Instance Context
    3. Using WCF for REST, Part 2
    4. Handling COM Error Codes
    • Tweet

    5 Comments

    • Gordon

      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 *

      09 Apr 2008 11:04 pm
      Reply
      • Simon

        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 :)

        10 Apr 2008 10:04 am
        Reply
        • WCF Instance Context « Infovark Underground

          [...] finally figured out the source of my bad request problem. Apparently the Windows Communication Foundation deals with exceptions differently [...]

          • trcull

            Not sure if this helps your particular case, but this guy found that changing the InstanceContextMode made a similar problem go away…

            http://underground.infovark.com/2008/05/21/wcf-instance-context/

            10 Sep 2009 04:09 am
            Reply
            • Dean

              That’s funny, @trcull. You just highlighted a link to another post of mine! I found the answer to the problem described here, in April 2008, one month later, in May 2008.

              A commenter on that post confirmed that it seemed to be a glitch related to InstanceContextMode.

              11 Sep 2009 04:09 pm
              Reply

              Leave a Comment

              Posting your comment...

              Subscribe to these comments via email

              • Categories

                • .NET (41)
                • AJAX (3)
                • Books (7)
                • HTML (9)
                • Infovark (8)
                • Programming (48)
                • REST (11)
                • SQL (3)
                • Testing (3)
                • Tools (13)
                • UI (3)
                • WCF (11)
                • Web Services (8)
                • WPF (4)
                • XML (4)
              • Archives

              • Get future articles


                 

              • Blogroll

                • Ajaxian
                • Anne Van Kesteren
                • Brain.Save()
                • Coding Horror
                • Eric Sink
                • Joel Spolsky
                • John Resig
                • Mark Pilgrim
                • Raymond Chen
                • Scott Hansleman
                • Secret Geek
                • Steve Yegge
                • The Daily WTF
                • The Database Programmer
              • Meta

                • Log in
                • Entries RSS
                • Comments RSS
                • WordPress.org
            • Site map

              • News
              • Product
              • Download
              • Buy
              • Support
              • About
            • Recent Posts

              • Review: Brownfield Application Development in .NET
              • Using Modal Dialogs with a Splash Screen in WPF
              • Highlighting query terms in a WPF TextBlock
              • Getting XAML Hyperlink text to wrap
              • How to format the XAML Hyperlink NavigateUri
            • Twitter

              Copyright 2011 Infovark, Inc. All rights reserved.