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

  • SEARCH
  • Home
  • 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 8899889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:58:05+00:00 2026-06-15T00:58:05+00:00

I have a web service that looks something like this: [ServiceContract] public class MyService

  • 0

I have a web service that looks something like this:

[ServiceContract]
public class MyService
{
   [OperationContract]
   public void BreakStuff()
   {
      throw new MyException();
   }
}

It’s not a very powerful web service right now, but you watch – it’s gonna be great!

If I call BreakStuff(), WCF traps the exception, returns an HTTP 500 and optionally spits out a serialized stack trace if IncludeExceptionDetailsInFaults is set to true. I’d like to override this behavior and spit out my own custom error message object, and return HTTP 200. I want to do this because I think throwing exceptions in certain cases is cleaner than littering the code with try/catch blocks and having all web methods return some sort of Result object.

What I’ve done is implemented my own IDispatchMessageFormatter, which has the contract:

public interface IDispatchMessageFormatter
{
   void DeserializeRequest(Message message, object[] parameters);
   Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result);
}

SerializeReply gets called for normal HTTP 200 responses, however if the web method throws an exception, WCF never calls SerializeReply so I’m not given the chance to control the response output.

Does WCF provide the ability to customize error responses?

  • 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-06-15T00:58:07+00:00Added an answer on June 15, 2026 at 12:58 am

    You can implement a custom error handler by implementing the IErrorHandler interface. I’ve used this approach in the past to return custom error information in JSON format. In the ProvideFault method you can also change the response code. Below is a sample of one approach I’ve used.

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {   
        fault = GetJsonResponseFromException(version, error);
        ConfigureMessageFormat(ref fault);
        ConfigureHttpReponse(ref fault, error);
    }
    
    protected virtual void ConfigureMessageFormat(ref Message fault)
    {
        var formatting =
          new WebBodyFormatMessageProperty(WebContentFormat.Json);
        fault.Properties.Add(WebBodyFormatMessageProperty.Name, formatting);
    }
    
    protected virtual void ConfigureHttpReponse(ref Message fault, Exception error)
    {
        var exception = error as WebFaultException<ServiceFault>;
        var statusCode = HttpStatusCode.InternalServerError;
        var description = HttpWorkerRequest
            .GetStatusDescription(Convert.ToInt32(statusCode));
    
        if (exception != null)
        {
            statusCode = exception.StatusCode;
            description = exception.Reason.Translations[0].Text;
        }
    
        var httpResponse = new HttpResponseMessageProperty()
        {
            StatusCode = statusCode,
            StatusDescription = description
        };
    
        httpResponse.Headers[HttpResponseHeader.ContentType] = "application/json";
        fault.Properties.Add(HttpResponseMessageProperty.Name, httpResponse);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service that looks like this: public class TheService : System.Web.Services.WebService
I have asimple WCF HTTP/SOAP web service, the service implementation looks something like this:
My web service is receiving xml from a third party that looks like this:
I have an abstract class. Let's call it Lifeform. It looks something like: public
I have a web service that returns this string via the jQuery $.ajax() call
I have a web service that takes a byte[] and saves it. This works
Right now, if you have a test that looks like this: [TestMethod] [DeploymentItem(DataSource.csv)] [DataSource(
I have some web methods in an asmx web service that currently look like
I have project that is a WCF service (.svc) that looks like the following:
I have string that look like Array that fetched from other webservice like this

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.