Skip to content

Instantly share code, notes, and snippets.

@subhasisrout
Created July 11, 2011 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subhasisrout/1076635 to your computer and use it in GitHub Desktop.
Save subhasisrout/1076635 to your computer and use it in GitHub Desktop.
Client Side Message Inspector
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Description;
namespace dotnet_topics
{
public class ConsoleOutputBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
ConsoleOutputMessageInspector inspector = new ConsoleOutputMessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
throw new Exception("Behavior not supported on the Server side!");
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Configuration;
namespace dotnet_topics
{
public class ConsoleOutputBehaviorExtensionElement : BehaviorExtensionElement
{
public ConsoleOutputBehaviorExtensionElement()
{
}
public override Type BehaviorType
{
get
{
return typeof(ConsoleOutputBehavior);
}
}
protected override object CreateBehavior()
{
return new ConsoleOutputBehavior();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
namespace dotnet_topics
{
public class ConsoleOutputMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
reply = buffer.CreateMessage();
Console.WriteLine("Received:\n{0}", buffer.CreateMessage().ToString());
}
public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Console.WriteLine("Sending:\n{0}", buffer.CreateMessage().ToString());
return null;
}
}
}
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://subhasis-pc/WcfService123/Service1.svc"
binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_IService1"
contract="WcfServiceRef123.IService1" name="basicHttpBinding_IService1" behaviorConfiguration="subhasisBehavior">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<extensions>
<behaviorExtensions>
<add name="ConsoleOutputBehavior" type="dotnet_topics.ConsoleOutputBehaviorExtensionElement, dotnet_topics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="subhasisBehavior">
<ConsoleOutputBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment