In my rest based WCF service I would like to throw a custom fault exception and use XML Serializer to serialize the results as I have a specific XML output format to use.
I have followed online examples exactly but after the code throws an exception I don't get an xml formatted response. Instead I get an XML tag with a load of html inside in what appears to be a standard response from the server entitled Request Error. The text I get is (html tags):
The server encountered an error processing the request. The exception message is 'Error 123'. See server logs for more details. The exception stack trace is:
at CSW.Services.CheckForCatalogueFaults(String request, String service) in C:\netDev\CatalogueService\CatalogueService\Services.svc.cs:line 64
at CSW.Services.GetCapabilities(String request, String version, String service) in C:\netDev\CatalogueService\CatalogueService\Services.svc.cs:line 69
at SyncInvokeGetCapabilities(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
I have the following settings and classes.
Service:
[OperationContract]
[XmlSerializerFormat(SupportFaults = true)]
[FaultContract(typeof(CatalogueFaultContract))]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/csw?REQUEST={request}&VERSION={version}&SERVICE={service}")]
Capability GetCapabilities(string request,string version, string service);
..one of my error checking conditions in Services implementation...
if (string.IsNullOrEmpty(request))
{
cswFault.Exception = new CswException("MissingParameterValue", "request");
cswFault.ErrorMsg = "Error";
throw new FaultException<CatalogueFaultContract>(cswFault);
}
My Custom FaultException class:
[XmlRoot(ElementName = "ExceptionReport",Namespace="http://www.opengis.net/ows")]
public class CatalogueFaultContract
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Ns;
[XmlAttribute(AttributeName = "Version")]
public string Version = "1.0.0";
[XmlElement(Namespace = "http://www.opengis.net/ows")]
public CswException Exception { get; set; }
[XmlAttribute(Namespace = XmlSchema.Namespace, AttributeName = "schemaLocation")]
public string xsiSchemaLocation = "http://www.opengis.net/ows http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd";
[XmlIgnore]
public string ErrorCode { get; set; }
[XmlIgnore]
public string ErrorMsg { get; set; }
public CatalogueFaultContract()
{}
public CatalogueFaultContract(string errorCode,string locator)
{
Ns = new XmlSerializerNamespaces();
Ns.Add("", "");
Ns.Add("ows", "http://www.opengis.net/ows");
Exception=new CswException(errorCode,locator);
}
}
I can't figure out why it is not displaying the serialized fault class... unless it can't serialize it properly? Have tried with a very simple class but get same response.
Answer: use a WebFaultException class instead..............