Just Simply Code

Another developer blog

  • Home
  • About
  • Register

Logging

Rewrite SOAP request message before sending

January 20, 2019 / Leave a Comment

For debugging purposes, I needed to alter the xml SOAP request completely before sending the request. In this post, I showed how to log all outgoing SOAP request by using IClientMessageInspector. I’d use the same method to alter the request before sending. Like this post?

Posted in: Uncategorised Tagged: IClientMessageInspector, IEndpointBehavior, Logging, SOAP

Logging all outgoing SOAP requests

September 8, 2018 / Leave a Comment

In my previous post, I showed my implementation for logging outgoing HTTP requests. In this post, I’ll show my implementation for logging outgoing SOAP requests using custom endpoint behaviour. First I have an implemention of the IClientMessageInspector. IMessageLoggingInspector is mainly for DI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public interface IMessageLoggingInspector : IClientMessageInspector
{
}
 
public class MessageLoggingInspector : IMessageLoggingInspector
{
    // AfterReceiveReply is called when the response is received.
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        string messageContent = CopyAndGetMessageContent(ref reply);
 
        var message = new
        {
            Id = correlationState,
            Response = messageContent
        };
 
        // Code to log response message goes here
 
    }
    
    // BeforeSendRequest is called before the request is sent. This is where the request should be logged.
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var messageContent = CopyAndGetMessageContent(ref request);
        
        var requestId = Guid.NewGuid();
        var message = new
        {
            Id = requestId,
            Body = messageContent,
            RequestDate = DateTime.UtcNow
        };
    
        // Code to log message goes here    
 
        // Whatever returned here will be the correlationState in AfterReceiveReply method above.
        // You need this to track which response is correlated to a request.
        return requestId;
    }
 
 
    private static string CopyAndGetMessageContent(ref Message request)
    {
        var buffer = request.CreateBufferedCopy(int.MaxValue);
 
        // Create a copy and set it back to request
        // because as soon as the request is read, you cannot read it again.
        request = buffer.CreateMessage();
        var originalMessage = buffer.CreateMessage();
        string messageContent;
 
        using (var stringWriter = new StringWriter())
        {
            using (var xmlTextWriter = new XmlTextWriter(stringWriter))
            {
                originalMessage.WriteMessage(xmlTextWriter);
                xmlTextWriter.Flush();
                xmlTextWriter.Close();
            }
            messageContent = stringWriter.ToString();
        }
 
        return messageContent;
    }
}

Then I need to implement IEndpointBehavior.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public interface IMessageLoggingBehaviour : IEndpointBehavior
{
}
 
public class MessageLoggingBehaviour : IMessageLoggingBehaviour
{
    private readonly IMessageLoggingInspector _messageLoggingInspector;
 
    public MessageLoggingBehaviour(IMessageLoggingInspector messageLoggingInspector)
    {
        _messageLoggingInspector = messageLoggingInspector;
    }
 
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }
 
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(_messageLoggingInspector);
    }
 
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }
 
    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

Finally, I just need to add MessageLoggingBehaviour when I create … [Read more…]

Posted in: Uncategorised Tagged: IClientMessageInspector, IEndpointBehavior, Logging, SOAP
RSS
Follow by Email
Facebook
Twitter
LinkedIn

Recent Posts

  • Add Javascript to bottom of body tag from a partial view in MVC
  • Ignore specified members field when serialise to Json string
  • Rewrite SOAP request message before sending
  • Badly auto generated WCF proxy
  • Adding Polly policies

Archives

  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • October 2017
  • September 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • July 2016
  • June 2016
  • March 2016
  • February 2016
  • January 2016

Copyright © 2019 Just Simply Code.

Me WordPress Theme by themehall.com