DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. WCF REST tip 2: WebFaultException

WCF REST tip 2: WebFaultException

Pieter De Rycke user avatar by
Pieter De Rycke
·
May. 01, 11 · News
Like (0)
Save
Tweet
Share
17.10K Views

Join the DZone community and get the full member experience.

Join For Free

soap supports propagating exceptions from the service to the client. this is called “faults” and in wcf this is represented by the faultexception class. exceptions in rest services are represented by a http status code >= 400. not a lot of people seem to be aware that wcf supports setting http status error codes by throwing a webfaultexception . most people doing rest in wcf seem to use the statuscode property of the outgoingresponse object that can be accessed from the weboperationcontext .

because the webfaultexception derives from the regular faultexception, it has the advantage that it results in service implementations that can use soap based bindings or http based bindings by just changing the web.config or app.config. just like the regular faultexception, the webfaultexception exists in two versions: a non generic version and a generic version. the non generic version only supports setting the http status code, while the generic version allows to specify a datacontract class that should be send to the client.

demo wcf service

we will create a small sample wcf service to show usage of the webfaultexception class. the wcf service will always return a http 401 status code along with some more detailed information about the error in json format.

the detail for the webfaultexception is very simple, it consists of 2 properties: a reason field and a detailed information field.

namespace jsonerrormessage.service
{
    [datacontract]
    public class errordata
    {
        public errordata(string reason, string detailedinformation)
        {
            reason = reason;
            detailedinformation = detailedinformation;
        }

        [datamember]
        public string reason { get; private set; }

        [datamember]
        public string detailedinformation { get; private set; }
    }
}

the interface of the service is very simple; it consist of only one method:

namespace jsonerrormessage.service
{
    [servicecontract]
    public interface ihelloservice
    {
        [webget(responseformat = webmessageformat.json)]
        [operationcontract]
        void hello();
    }
}

the implementation of the service is of course very simple:

namespace jsonerrormessage.service
{
    public class helloservice : ihelloservice
    {
        public void hello()
        {
            errordata errordata = new errordata("you are not allowed to access this service.",
                "we don't allow anybody to access this service.");

            throw new webfaultexception<errordata>(errordata,
                httpstatuscode.unauthorized);
        }
    }
}

and finally the following configuration needs to be added into the web.config or app.config file:

<system.servicemodel>
  <services>
    <service name="jsonerrormessage.service.helloservice">
      <endpoint address=""
                binding="webhttpbinding"
                contract="jsonerrormessage.service.ihelloservice" />
    </service>
  </services>
</system.servicemodel>

testing the demo wcf service

i will use fiddler to test the implementation of the wcf service. fiddler is a free tool to debug http traffic and i highly recommend using it.

fiddler has a request builder that allows creating http requests very easily:

fiddler request builder

when we execute this http request we get the following result back:

response unauthorized json

the server returned a 401 error code and the body is our datacontract in json format. this is the result that we wanted.

Windows Communication Foundation REST Web Protocols

Published at DZone with permission of Pieter De Rycke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • ChatGPT: The Unexpected API Test Automation Help
  • Load Balancing Pattern
  • Stream Processing vs. Batch Processing: What to Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: