We can send the exceptions from wcf REST to
client using WebFaultException or by
using WebProtocolException which comes in wcf REST starter kit.The following
example demonstrates the WebProtocolException
implementation.
In wcf REST
starter kit we get Microsoft.ServiceModel.Web dll file which gives features like
throwing exceptions and authentication.
To get the
features of wcf REST starter kit in IIS hosted services you should add the
factory in ServiceHost file like below.
<%@ ServiceHost Language="C#" Debug="true" Service="WcfRESTException.Service" CodeBehind="Service.svc.cs" Factory="Microsoft.ServiceModel.Web.WebServiceHost2Factory"%>
->Create a
custom exception class with property Description.
[DataContract]
public class CustomException
{
[DataMember]
public string Description { get;
set; }
}
->Create wcf REST service with SayHello method.
[ServiceContract]
public class Service
{
[OperationContract]
[WebGet(ResponseFormat
= WebMessageFormat.Json)]
public string SayHello(string
name)
{
//Create
custom exception object with proper error description.
CustomException
objCustomException = new CustomException();
objCustomException.Description = "error description";
//Throw
the exception.
throw
new WebProtocolException(HttpStatusCode.BadRequest, "", objCustomException, null,
null);
//Return
string.
return
"Hello " + name;
}
}
->There is no need of web.config file modification.Wcf Rest starter kit do it for you.
->There is no need of web.config file modification.Wcf Rest starter kit do it for you.
Let me know, if you have any feedback. Mail me for
source code. Enjoy reading my articles…
sekhartechblog@gmail.com
No comments:
Post a Comment