C# 使服务检查器返回自定义对象而不是c中的错误异常

C# 使服务检查器返回自定义对象而不是c中的错误异常,c#,wcf,soap,client-server,inspector,C#,Wcf,Soap,Client Server,Inspector,我正在让服务检查器验证针对xsd的soap请求。我想让AfterReceiveRequest方法在客户机到达服务之前用正确的响应而不是故障异常来回复客户机,如果请求负载有任何问题 我的后接收人请求方法 en public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { MessageBuffer b

我正在让服务检查器验证针对xsd的soap请求。我想让AfterReceiveRequest方法在客户机到达服务之前用正确的响应而不是故障异常来回复客户机,如果请求负载有任何问题

我的后接收人请求方法

en      public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy (int.MaxValue);

        try
        {
            Message messageCopy = buffer.CreateMessage();
            XElement payloadTovalidate = Helper.GetXmlPayload(messageCopy, _payloadStartTag);

            if (payloadTovalidate != null)
            {
                CustomValidator validator = new CustomValidator();
                validator.Validate(payloadTovalidate.ToString(), _interfaceName);
            }
            else
            {
                return new CustomException(string.Format("ERROR: Incorrect xml format in the payload or attachment. Expected object of type {0} ;", _payloadStartTag), "asd", "asdas");

            }
        }
        catch(Exception exception)
        {
            throw new FaultException(exception.Message);
        }
        finally
        {
            request = buffer.CreateMessage ();
            buffer.Close ();
        }

        return null;

    }

改变服务将不是一个选项,因为有很多

您需要为此使用faultContract,并使用自己的异常类型。您的验证是否需要出现在消息上?如果您在稍后的扩展点(例如IOperationInvoker)上进行验证,则返回响应会容易得多。@ErnieL yes。我需要验证结果在正确的soap响应上。IOperationVoker是如何工作的?什么时候触发?IOperationInvoker是调用服务类之前的最后一个扩展点。因此,您可以完全覆盖响应对象,停止服务调用不需要引发异常。看@ErnieL-我会查的。谢谢你的意见