WCF REST tip 2: WebFaultException
Join the DZone community and get the full member experience.
Join For Freesoap 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:
when we execute this http request we get the following result back:
the server returned a 401 error code and the body is our datacontract in json format. this is the result that we wanted.
Published at DZone with permission of Pieter De Rycke, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Deploying Smart Contract on Ethereum Blockchain
-
How To Use Git Cherry-Pick to Apply Selected Commits
-
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
-
Java Concurrency: Condition
Comments