, . 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
}