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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:43:47+00:00 2026-06-04T13:43:47+00:00

Summary: In a WCF REST service how can I handle incorrect input parameters using

  • 0

Summary:

In a WCF REST service how can I handle incorrect input parameters using my own custom fault response?

Detail:

I have the following interface method in a WCF REST service:

[OperationContract]
[WebGet(UriTemplate = "getitem?id={itemId}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[FaultContract(typeof(DataModel.Fault))]
DataModel.Item GetItem(int itemId);

Within my implementation of the GetItem method I catch any error conditions (i.e. item doesn’t exist) and create a WebFaultException. This works without any issues whatsoever.

However, lets say the invoking client calls the following url:

http://myserver.com/itemservice/getitem?id=abc

i.e. the input value for id cannot be cast to int as required by the GetItem method

This call rightly fails as the given id value is not castable to int, however this failure occurs before any of my code is executed and therefore I cannot return my DataModel.Fault object as per the FaultContract assigned to the interface method.

So the question is, how can I hook a high level error handler to catch errors of this type and subsequently return my own fault structure rather than the HTML error that WCF generates:

Request Error
The server encountered an error processing the request. See server logs for more details.

As a continuation of the same question I would also like to be able to do the same thing if the client calls the following:

http://myserver.com/itemservice/gettitem?id=abc

notice the typo in the URI, gettitem (two t’s) instead of getitem

  • 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-04T13:43:48+00:00Added an answer on June 4, 2026 at 1:43 pm

    Use IErrorHandler interface to extend your service.

    This will allow you to provide appropriate fault for any exception.
    This post shows how to hook up your own error handler to your service behavior.

    I personally use single class to implement all necessary elements like this:

    public class ErrorSavingServiceBahavior : BehaviorExtensionElement, IServiceBehavior, IErrorHandler
    {
        private string m_sHostName;
    
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            this.m_sHostName = serviceHostBase.Description.Name;
            foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
            {
                if (!chanDisp.ErrorHandlers.Contains(this))
                {
                    chanDisp.ErrorHandlers.Add(this);
                }
            }
        }
    
        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
        }
    
        public bool HandleError(Exception error)
        {
            //my custom code
        }
    
        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            //my custom code
        }
    
        public override Type BehaviorType
        {
            get { return this.GetType(); }
        }
    
        protected override object CreateBehavior()
        {
            return this;
        }
    }
    

    and web.config I extend with:

    <behaviors>
      <serviceBehaviors>
        <behavior name="BehaviorName">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/>
          <saveExceptions />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add
          name="saveExceptions"
          type="Namespace.Class, DLLName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary A parent can have many children. How do you write a service such
I've got a hosted WCF service that I created a custom factory for, so
I've created a WCF DataService and for this service I require some custom authentication
I have the following method on a WCF service: /// <summary> /// Receives the
I am writing a service using wcf. I have created a solution with two
In summary How do I acces a WCF service on localhost when hosted in
I'm creating an app based on SOA, I've created WCF Service Project using Framework
SUMMARY How can I make my GUI application run on windows startup on a
Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in
I created a simple RESTful WCF service with a single method for receiving files

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.