从WCF Schema Validator

时间:2016-10-17 21:37:18

标签: c# .net wcf soap service

我正在编写服务并已实施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;
            }

看起来我必须抛回异常?

由于

1 个答案:

答案 0 :(得分:0)

使用BeforeSendReply来创建消息。

            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的其他重载,请参阅以下内容。

https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.createmessage(v=vs.110).aspx