Created
October 18, 2019 12:23
-
-
Save mesuttalebi/e0aad6d1f5a67073c67bf3ce818916a8 to your computer and use it in GitHub Desktop.
ClientMessageInspectorInjector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ServiceModel; | |
using System.ServiceModel.Channels; | |
using System.ServiceModel.Description; | |
using System.ServiceModel.Dispatcher; | |
namespace Helper | |
{ | |
public static class ClientMessageInspectorInjector | |
{ | |
public static ClientMessageInspector InjectClientMessageInspector<T>(this ClientBase<T> client) where T : class | |
{ | |
var inspector = new ClientMessageInspector(); | |
client.Endpoint.EndpointBehaviors.Add(new EndpointBehavior(inspector)); | |
return inspector; | |
} | |
} | |
public class ClientMessageInspector : IClientMessageInspector | |
{ | |
public string XmlRequest { get; set; } | |
public string XmlResponse { get; set; } | |
public void AfterReceiveReply(ref Message reply, object correlationState) | |
{ | |
XmlResponse = reply.ToString(); | |
} | |
public object BeforeSendRequest(ref Message request, IClientChannel channel) | |
{ | |
XmlRequest = request.ToString(); | |
return null; | |
} | |
} | |
public class EndpointBehavior : IEndpointBehavior | |
{ | |
private readonly IClientMessageInspector _inspector; | |
public EndpointBehavior(IClientMessageInspector inspector) | |
{ | |
_inspector = inspector; | |
} | |
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) | |
{ | |
// No implementation necessary | |
} | |
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) | |
{ | |
clientRuntime.ClientMessageInspectors.Add(_inspector); | |
} | |
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) | |
{ | |
// No implementation necessary | |
} | |
public void Validate(ServiceEndpoint endpoint) | |
{ | |
// No implementation necessary | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment