在我的基于休息的WCF服务中,我想抛出一个自定义错误异常,并使用XML Serializer序列化结果,因为我有一个特定的XML输出格式可供使用。
我已完全按照在线示例但在代码抛出异常后我没有得到xml格式的响应。相反,我得到一个带有html内容的XML标签,其中似乎是来自名为Request Error的服务器的标准响应。我得到的文字是(html标签):
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)
我有以下设置和课程。
服务:
[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);
..我在服务实现中的一个错误检查条件......
if (string.IsNullOrEmpty(request))
{
cswFault.Exception = new CswException("MissingParameterValue", "request");
cswFault.ErrorMsg = "Error";
throw new FaultException<CatalogueFaultContract>(cswFault);
}
我的自定义FaultException类:
[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);
}
}
我无法弄清楚为什么它没有显示序列化的故障类...除非它无法正确序列化?尝试过一个非常简单的课程但得到同样的答案。
答案 0 :(得分:0)
答案:改为使用WebFaultException类..............