C# 从WCF模式验证器回滚自定义响应

C# 从WCF模式验证器回滚自定义响应,c#,.net,wcf,soap,service,C#,.net,Wcf,Soap,Service,我正在编写一个服务,实现了IDispatchMessageInspector类,并重写了AfterReceiveRequest函数 我实际上想做的是,如果验证不起作用,就不要抛出任何异常。我想构建自己的响应并将其发送回去 我该怎么做?我根本不想发回SOAP消息 public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)

我正在编写一个服务,实现了
IDispatchMessageInspector
类,并重写了
AfterReceiveRequest
函数

我实际上想做的是,如果验证不起作用,就不要抛出任何异常。我想构建自己的响应并将其发送回去

我该怎么做?我根本不想发回SOAP消息

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                try
                {
                    ValidateMessage(ref request);
                }
                catch (FaultException e)
                {
                    throw new FaultException<CustomResponse>( new CustomResponse { Success = false, ErrorMessage = e.Message});

                }
                return null;
            }
接收方请求后的公共对象(ref消息请求、IClientChannel通道、InstanceContext InstanceContext) { 尝试 { 验证消息(ref请求); } 捕获(错误异常e) { 抛出新的FaultException(新的CustomResponse{Success=false,ErrorMessage=e.Message}); } 返回null; } 看起来我必须抛出一个异常

谢谢

查看如何使用创建消息

            void IDispatchMessageInspector.BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {
                try
                {
                    ValidateMessage(ref reply);                
                }
                catch (FaultException fault)
                {
                    // handle the fault and create the reply
                    // choose one of the CreateMessage overloads    
                    reply = Message.CreateMessage(reply.Version, "SOME MESSAGE");                    
                }
            }
有关CreateMessage的其他重载,请参见以下内容