从WCF架构验证器返回自定义响应
本文关键字:返回 自定义 响应 验证 WCF | 更新日期: 2023-09-27 18:13:22
我正在编写一个服务,并实现了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;
}
看起来我必须抛出一个异常?
谢谢
考虑使用beforeendreply来创建消息。
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