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 7631859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:27:55+00:00 2026-05-31T06:27:55+00:00

I have a RESTful WCF service and I’ve implemented an ErrorHandler such as the

  • 0

I have a RESTful WCF service and I’ve implemented an ErrorHandler such as the following:

public class MyErrorHandler : IErrorHandler, IServiceBehavior
{
    // OMITTED: IServiceBehavior Members

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        DoSomeCustomLogging(error);
        WebOperationContext ctx = WebOperationContext.Current;
        ctx.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
    }           
}   
   
public class MyErrorHandlerElement : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        return new MyErrorHandler();
    }

    public override Type BehaviorType
    {
        get
        {
            return typeof(MyErrorHandler);
        }
    }
}
   

It’s hooked up in my web.config like so:

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="Rest">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <extensions>
    <behaviorExtensions>
      <add name ="errorHandler" type="ACME.MyErrorHandlerElement, ACME.MyErrorHandler"/>
    </behaviorExtensions>
  </extensions>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  <standardEndpoints>
    <webHttpEndpoint>
      <standardEndpoint name="" faultExceptionEnabled="false" helpEnabled="false"     automaticFormatSelectionEnabled="true"  />
    </webHttpEndpoint>
  </standardEndpoints>
</system.serviceModel>

For some reason, if my service throws an exception, 400 is returned to the client instead of 500. Any ideas why?

  • 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-31T06:27:57+00:00Added an answer on May 31, 2026 at 6:27 am

    You need to modify the fault that gets passed in by reference, this isnt your regular webmethod call.

    #Region "IErrorHandler Members"
    Public Function HandleError(ByVal [error] As Exception) As Boolean Implements IErrorHandler.HandleError
      Console.WriteLine("HandleError called.")
      ' Returning true indicates you performed your behavior.
      Return True
    End Function
    
    ' This is a trivial implementation that converts Exception to FaultException<GreetingFault>.
    Public Sub ProvideFault(ByVal [error] As Exception, ByVal ver As MessageVersion, ByRef msg As Message) Implements IErrorHandler.ProvideFault
      Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....")
      Dim fe As New FaultException(Of GreetingFault)(New GreetingFault([error].Message))
      Dim fault As MessageFault = fe.CreateMessageFault()
      msg = Message.CreateMessage(ver, fault, "http://microsoft.wcf.documentation/ISampleService/SampleMethodGreetingFaultFault")
    End Sub
    #End Region
    

    MSDN Source

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

Sidebar

Related Questions

I have WCF restful service and have property called Image with Imageclass [DataMember] public
I have wirtten a RESTful WCF Service. Incorporating E-Tags, expires headers. The caching works
We have created a WCF RESTful service for a WPF(UI) Application. The UI sends
I have a RESTful service that I am developing in WCF . I am
I have been following a couple of articles regarding RESTful web services with WCF
I have a WCF restful service that I'm trying to upload an image to.
I have a wcf restful service with a operation contract that contains two values
I have a WCF service with multiple operations exposed as a RESTful API. Most
I have a RESTful WCF service that exposes a method that takes in an
I have a RESTful WCF web service that processes huge XML files that are

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.