WCF Return different fault depending on RequestFormat
I have a WCF method:
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "exception")]
public List<Color> GenerateException()
I want to return a different fault based on the client (SOAP vs JSON)
because they don't seem to have visibility to the message if I don't.
So I check the content type and choose my type accordingly
String ct = WebOperationContext.Current.IncomingRequest.ContentType;
FaultException fe = new FaultException(errorMessage, new
FaultCode(errorCode));
WebFaultException<ErrorData> errorDataWebFault = new
WebFaultException<ErrorData> (errorData,
System.Net.HttpStatusCode.InternalServerError);
if (ct.Contains("text/xml"))
throw fe;
else//Can throw any of the WebFaultExceptions here
throw errorDataWebFault;
If I don't check, SOAP consumers can't see the message of
WebFaultExceptions, and JSON consumers cant see the message of the
FaultException.
Is there a better way to do this? Feels like there should be something
"built in".
No comments:
Post a Comment