Sample code to demonstrate WebFaultException usage…
[DataContract(Name = "error")] [Serializable] public class ErrorDetail { [DataMember(Name = "code")] public string ErrorCode; [DataMember(Name = "message")] public string Message; } [DataContract(Name = "errors")] [Serializable] public class ErrorDetails: List<ErrorDetail> {} [WebGet(UriTemplate = "/get.json?cd={code}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] public InfoData GetInfo(string code) { if (string.IsNullOrWhiteSpace(code)) throw new WebFaultException<ErrorDetail>(new ErrorDetail() { ErrorCode = "100", Message = "Parameter code is null" }, System.Net.HttpStatusCode.BadRequest); try { //Do something } catch (Exception ex) { Log.LogError(ex, "GetInfo..."); throw new WebFaultException<ErrorDetail>(new ErrorDetail() { ErrorCode = "500", Message = "Unexpected error occurred..." }, System.Net.HttpStatusCode.InternalServerError); } finally { } }
Reblogged this on Sutoprise Avenue, A SutoCom Source.