Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4075404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:17:38+00:00 2026-05-20T17:17:38+00:00

How can I get my WCF service to communicate errors in a RESTful manner?

  • 0

How can I get my WCF service to communicate errors in a RESTful manner? Specifically, if the caller passes invalid query string parameters to my method, I’d like to have a 400 or 404 HTTP error returned to the user. When I search for HTTP error status in relation to WCF, all I can find are pages where people are trying to resolve errors they’re receiving. I’d rather not just throw a FaultException, because that gets converted to a 500 error, which is not the correct status code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:17:39+00:00Added an answer on May 20, 2026 at 5:17 pm

    I found a helpful article here: http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/. Based on that, this is what I came up with:

    public class HttpErrorsAttribute : Attribute, IEndpointBehavior
    {
        public void AddBindingParameters(
            ServiceEndpoint endpoint, 
            BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyClientBehavior(
            ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }
    
        public void ApplyDispatchBehavior(
            ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            var handlers = endpointDispatcher.ChannelDispatcher.ErrorHandlers;
            handlers.Clear();
            handlers.Add(new HttpErrorHandler());
        }
    
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    
        public class HttpErrorHandler : IErrorHandler
        {
            public bool HandleError(Exception error)
            {
                return true;
            }
    
            public void ProvideFault(
                Exception error, MessageVersion version, ref Message fault)
            {
                HttpStatusCode status;
                if (error is HttpException)
                {
                    var httpError = error as HttpException;
                    status = (HttpStatusCode)httpError.GetHttpCode();
                }
                else if (error is ArgumentException)
                {
                    status = HttpStatusCode.BadRequest;
                }
                else
                {
                    status = HttpStatusCode.InternalServerError;
                }
    
                // return custom error code.
                fault = Message.CreateMessage(version, "", error.Message);
                fault.Properties.Add(
                    HttpResponseMessageProperty.Name,
                    new HttpResponseMessageProperty
                    {
                        StatusCode = status,
                        StatusDescription = error.Message
                    }
                );
            }
        }
    }
    

    This allows me to add a [HttpErrors] attribute to my service. In my custom error handler, I can ensure that the HTTP status codes I’d like to send are sent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a WCF service configured with message security (no transport
I'm trying to host a WCF service in IIS so that I can access
I can get easily see what projects and dlls a single project references from
I can get both System.Net.Mail and System.Web.Mail to work with GMail, but I can't
Logging can get complicated, quickly. Considering that you have some code, how do you
I can get the executable location from the process, how do I get the
I can get simple examples to work fine as long as there's no master
I can get the element like this $(#txtEmail) but I'm not sure how to
I can get a list of running threads from Process.GetCurrentProcess().Threads, but I need to
I CAN get the associated icon just fine, and draw it accordingly, however, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.