对于我的WCF客户端,我希望能够为给定端点的每个操作执行一些 pre 和 post 工作。
鉴于此,IParameterInspector
(BeforeCall
和AfterCall
)给了我几乎我需要的一切。问题是
AfterCall
(请参阅here)。 为了解决这个问题,我可以添加一个IClientMessageInspector
,因为AfterReceiveReply
会在异常时被调用。这让我有能力
问题:
IClientMessageInspector.AfterReceiveReply
获取(或创建等效的)异常,表示最终会向调用者抛出的内容。如果是这样,有没有办法将异常标记为处理,以致调用者不会获得异常?答案 0 :(得分:0)
如果我理解正确,您希望将所有异常静音。这是可能的,可以通过替换IClientMessageInspector.AfterReceiveReply
和"反序列化"中的消息来完成。 IClientMessageFormatter.DeserializeReply
中的新消息。
下面列出的代码在抛出异常时返回默认值作为结果值。
消息:
public sealed class FakeMessage : Message
{
#region Fields
private MessageProperties properties;
private MessageHeaders headers;
#endregion
#region Constructors
public FakeMessage(MessageVersion version, string action)
{
this.headers = new MessageHeaders(version);
this.headers.Action = action;
}
#endregion
#region Message Members
public override MessageHeaders Headers
{
get { return headers; }
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
throw new NotSupportedException();
}
public override MessageProperties Properties
{
get
{
if (this.properties == null)
{ properties = new MessageProperties(); }
return properties;
}
}
public override MessageVersion Version
{
get { return headers.MessageVersion; }
}
#endregion
}
邮件格式化程序:
public sealed class FakeMessageFormatter : IClientMessageFormatter
{
#region Fields
private IClientMessageFormatter baseFormatter;
private object defaultReturnValue;
#endregion
#region Construcotrs
public FakeMessageFormatter(IClientMessageFormatter baseFormatter, Type returnType)
{
this.baseFormatter = baseFormatter;
if (returnType.IsValueType && returnType != typeof(void))
{ this.defaultReturnValue = Activator.CreateInstance(returnType); }
}
#endregion
#region IClientMessageFormatter Members
public object DeserializeReply(Message message, object[] parameters)
{
if (message is FakeMessage)
{ return defaultReturnValue; }
return baseFormatter.DeserializeReply(message, parameters);
}
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
return baseFormatter.SerializeRequest(messageVersion, parameters);
}
#endregion
}
最后是消息检查员:
public sealed class FakeMessageInspector : IClientMessageInspector
{
#region IClientMessageInspector Members
public void AfterReceiveReply(ref Message reply, object correlationState)
{
if (reply.IsFault)
{ reply = new FakeMessage(reply.Version, (string)correlationState); }
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return request.Headers.Action + "Response";
}
#endregion
}