MessageInspector – WCF

Actually I got one assignment from current job that when client call the service, I want to add some object that I can use at service side.

 

So basically, I want to try to display the SOAP message sent to the service host. In order to do this, you might want to pay attention to 3 things:

· System.ServiceModel.Configuration.BehaviorExtensionElement

· System.ServiceModel.Description.IEndpointBehavior

· System.ServiceModel.Dispatcher.IClientMessageInspector

 

You can fine code here : http://cid-39f476822b8257be.skydrive.live.com/self.aspx/.Public/WCF^_Demo/MitWCFDay1.zip

Here is the service…

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using MitWCFDay2;
  7. namespace MitWCFDay1
  8. {
  9.     [ServiceContract]
  10.     [MyMessageInspector]
  11.     public interface ICalcMath
  12.     {
  13.         [OperationContract]
  14.         int AddNumber(int i, int j);
  15.     }
  16.     public class CalcMath : ICalcMath
  17.     {
  18.         #region ICalcMath Members
  19.         public int AddNumber(int i, int j)
  20.         {
  21.             return i + j;
  22.         }
  23.         #endregion
  24.     }
  25. }

If you observe carefully then one  MyMessageInspector attribute is define to service. I’ll explain that later.

 

Now We have to create MyMessageInspector class  and code is following…

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel.Channels;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Description;
  8. using System.ServiceModel.Dispatcher;
  9. using System.ServiceModel.Configuration;
  10. namespace MitWCFDay1
  11. {
  12.     class MyMessageInspector : Attribute, IClientMessageInspector, System.ServiceModel.Description.IEndpointBehavior
  13.     {
  14.         #region IClientMessageInspector Members
  15.         public void AfterReceiveReply(ref Message reply, object correlationState)
  16.         {   // Ignore
  17.         }
  18.         public object BeforeSendRequest(ref Message request, IClientChannel channel)
  19.         {
  20.             Console.WriteLine(request.ToString());
  21.            
  22.             return null;
  23.         }
  24.         #endregion
  25.          #region IEndpointBehavior Members
  26.         public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
  27.         { // Ignore
  28.         }
  29.         public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  30.         {
  31.             clientRuntime.MessageInspectors.Add(new MyMessageInspector());
  32.         }
  33.         public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  34.         { // Ignore
  35.         }
  36.         public void Validate(ServiceEndpoint endpoint)
  37.         { // Ignore
  38.         }
  39.         #endregion
  40.     }
  41.     
  42.     
  43.     public class CustomBehaviorSection : BehaviorExtensionElement
  44.     {
  45.         public override Type BehaviorType
  46.         {
  47.             get { return typeof(MyMessageInspector); }
  48.         }
  49.         protected override object CreateBehavior()
  50.         {
  51.             return new MyMessageInspector();
  52.         }
  53.         
  54.         
  55.     }
  56.     public class Information
  57.     {
  58.         public static Information CurrentInformation { get; set; }
  59.         public string ComputerName { get; set; }
  60.         public string UserName { get; set; }
  61.         public Information(string computerName,string userName)
  62.         {
  63.             if (CurrentInformation == null)
  64.             {
  65.                 this.ComputerName = computerName;
  66.                 this.UserName = userName;
  67.                 CurrentInformation = this;
  68.             }
  69.             else
  70.                 CurrentInformation = this;
  71.             
  72.         }
  73.     }
  74. }

Here MessageInspector class is responsible to set whatever client want to set.  There is other class call CustomBehaviorSection and it is derived from BehaviorExtensionElement. We have to give this BehaviorExtensionElement into app.config file.

Code Snippet
  1. <?xml version=1.0 encoding=utf-8 ?>
  2. <configuration>
  3.   
  4.   <system.serviceModel>
  5.     <services>
  6.       <service name=MitWCFDay1.CalcMath behaviorConfiguration=ForAuthorazation>
  7.         <endpoint address=net.tcp://localhost:56/MyMathService binding=netTcpBinding contract=MitWCFDay1.ICalcMath bindingConfiguration=ForSecurity name=Binding1 />
  8.         <endpoint address=http://localhost:560/MyMathService binding=basicHttpBinding contract=MitWCFDay1.ICalcMath name=Binding2“  />
  9.         <host>
  10.           <baseAddresses>
  11.             <add baseAddress=net.tcp://localhost:56/MyMathService/>
  12.           </baseAddresses>
  13.         </host>
  14.         
  15.       </service>
  16.       
  17.       
  18.     </services>
  19.     <client>
  20.       <endpoint address=net.tcp://localhost:56/MyMathService binding=netTcpBinding contract=MitWCFDay1.ICalcMath name=Binding1 behaviorConfiguration =aa />
  21.       <endpoint address=http://localhost:560/MyMathService binding=basicHttpBinding contract=MitWCFDay1.ICalcMath name=Binding2 behaviorConfiguration=aa />
  22.     </client>
  23.     <behaviors>
  24.       
  25.       <serviceBehaviors>
  26.         <behavior  name=ForAuthorazation>
  27.           <serviceAuthorization serviceAuthorizationManagerType=MitWCFDay1.CalcAutoMng,MitWCFDay1 />
  28.         
  29.         </behavior>
  30.       </serviceBehaviors>
  31.       <endpointBehaviors >
  32.         <behavior name=aa>
  33.           <CustomBehaviorSection />
  34.         </behavior>
  35.       </endpointBehaviors>
  36.     </behaviors>
  37.     <extensions>
  38.       <behaviorExtensions>
  39.         <add name=CustomBehaviorSection type=MitWCFDay1.CustomBehaviorSection, MitWCFDay1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null“  />
  40.       </behaviorExtensions>
  41.     </extensions>
  42.     
  43.     
  44.     
  45.     <bindings>
  46.       <netTcpBinding>
  47.         <binding name=ForSecurity>
  48.           <security mode=Transport>
  49.             
  50.             <transport clientCredentialType=Windows/>
  51.             
  52.           </security>
  53.         </binding>
  54.         
  55.       </netTcpBinding>
  56.       
  57.     </bindings>
  58.     
  59.     
  60.   </system.serviceModel>
  61. </configuration>

First we have created behaviorExtensions and after we have define that into endpointBehaviors.

now if you put break point on MessageInspector , before calling the service it will call the methods of MessageInspector  class.

Don’t forget to add the attribute on service.. other wise throw the error…

Advertisement

About miteshisheth
Believe in sharing knowledge.

One Response to MessageInspector – WCF

  1. Joan says:

    Really good one. Can u please give the situation where we should used MessageInspector except security scenario.

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.