0

在我的基于休息的 WCF 服务中,我想抛出一个自定义错误异常并使用 XML 序列化器来序列化结果,因为我有一个特定的 XML 输出格式要使用。

我完全按照在线示例进行操作,但是在代码引发异常后,我没有得到 xml 格式的响应。取而代之的是,我得到了一个带有大量 html 的 XML 标记,这似乎是来自服务器的标准响应,标题为请求错误。我得到的文本是(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);

        }
    }

我不知道为什么它不显示序列化的故障类......除非它不能正确地序列化它?尝试了一个非常简单的课程,但得到了相同的响应。

4

1 回答 1

0

答案:改用 WebFaultException 类............

于 2012-11-12T11:02:15.950 回答