Implementacja intefrejsu IClientMessageInspector

Implementacja intefrejsu IClientMessageInspector umożliwiającego odczytanie i modyfikację komunikatu soap otrzymanego z serwisu. Dodatkowo należy zaimplementować interfejs IEndpointBehavior, za pomocą którego definiujemy własny behavior oraz napisać swój BehaviorExtensionElement co daje nam możliwość wykorzystania behaviora bezpośrednio w pliku konfiguracyjnym.

 namespace ConsoleMyApp
 {
     public class MyBehavior : IEndpointBehavior
     {
         #region IEndpointBehavior Members

         public void AddBindingParameters(
             ServiceEndpoint endpoint,
             BindingParameterCollection bindingParameters)
         {
         }

         public void ApplyClientBehavior(
             ServiceEndpoint endpoint,
             ClientRuntime clientRuntime)
         {
             clientRuntime.MessageInspectors.Add(new MyMessageInspector());
         }

         public void ApplyDispatchBehavior(
             ServiceEndpoint endpoint,
             EndpointDispatcher endpointDispatcher)
         {
         }

         public void Validate(
             ServiceEndpoint endpoint)
         {
         }

         #endregion
     }

     public class MyMessageInspector : IClientMessageInspector
     {
         #region IClientMessageInspector Members

         public void AfterReceiveReply(
             ref Message reply,
             object correlationState)
         {
             Console.WriteLine(
             "Received the following reply: '{0}'", reply.ToString());           
         }

         public object BeforeSendRequest(
             ref Message request,
             IClientChannel channel)
         {
             Console.WriteLine(
             "Sending the following request: '{0}'", request.ToString());
             return null;
         }

         #endregion
     }

     public class CustomBehaviorExtensionElement : BehaviorExtensionElement
     {
         protected override object CreateBehavior()
         {
             return new MyBehavior();
         }

         public override Type BehaviorType
         {
             get
             {
                 return typeof(MyBehavior);
             }
         }
     }
 }

I przykładowy app.config

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
   <system.diagnostics>
     <sharedListeners>
       <add name="System.Net" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\logs\System.Net.trace.txt" traceOutputOptions="DateTime" />
     </sharedListeners>
     <sources>
       <source name="System.ServiceModel.MessageLogging">
         <listeners>
           <add name="messages"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="c:\logs\messagesCC.svclog" />
         </listeners>
       </source>
       <source name="System.ServiceModel" switchValue="All" propagateActivity="true">
         <listeners>
           <add name="messages"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="c:\logs\diagnosticsCC.svclog" />
         </listeners>
       </source>
       <source name="System.Net" switchValue="All">
         <listeners>
           <add name="System.Net"/>
         </listeners>
       </source>
       <source name="System.Net.HttpListener" switchValue="All">
         <listeners>
           <add name="System.Net"/>
         </listeners>
       </source>
       <source name="System.Net.Sockets" switchValue="All">
         <listeners>
           <add name="System.Net"/>
         </listeners>
       </source>
       <source name="System.Net.Cache" switchValue="All">
         <listeners>
           <add name="System.Net"/>
         </listeners>
       </source>
     </sources>
   </system.diagnostics>
   <system.serviceModel>
     <bindings>
       <basicHttpBinding>
         <binding name="PhBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
           receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
           bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
           maxBufferSize="524288" maxBufferPoolSize="524288" maxReceivedMessageSize="524288"
           messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
           useDefaultWebProxy="true">
           <readerQuotas maxDepth="32" maxStringContentLength="5242880"
             maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
           <security mode="None">
             <transport clientCredentialType="None" proxyCredentialType="None"
               realm="" />
             <message clientCredentialType="UserName" algorithmSuite="Default" />
           </security>
         </binding>        
       </basicHttpBinding>
       <wsHttpBinding>
         <binding name="customWsHttpBinding">
           <security mode="None" />
         </binding>
       </wsHttpBinding>
     </bindings>
     <client>
       <endpoint address="ADDRESS_SERVISU" behaviorConfiguration="Behavior"
         binding="basicHttpBinding" bindingConfiguration="PhBinding"
         contract="MyService.PhPortType" name="PhPort" />
     </client>
     <extensions>
       <behaviorExtensions>
         <add name="BehaviorElement"
           type="ConsoleMyApp.CustomBehaviorExtensionElement, ConsoleMyApp"/>
       </behaviorExtensions>
     </extensions>
     <behaviors>
       <endpointBehaviors>
         <behavior name="Behavior">
           <BehaviorElement />
         </behavior>
       </endpointBehaviors>
     </behaviors>
     <diagnostics>
       <messageLogging
            logEntireMessage="true"
            logMalformedMessages="false"
            logMessagesAtServiceLevel="true"
            logMessagesAtTransportLevel="false"
            maxMessagesToLog="3000"
            maxSizeOfMessageToLog="2000"/>
     </diagnostics>
   </system.serviceModel>
 </configuration>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s