Skip to content

Instantly share code, notes, and snippets.

@subhasisrout
Created July 11, 2011 19:49
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/1076647 to your computer and use it in GitHub Desktop.
Save subhasisrout/1076647 to your computer and use it in GitHub Desktop.
Server side Message Inspector
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Description;
namespace WcfService123
{
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)
{
throw new Exception("Behavior not supported on the consumer side!");
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
ConsoleOutputMessageInspector inspector = new ConsoleOutputMessageInspector();
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Configuration;
namespace WcfService123
{
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 WcfService123
{
public class ConsoleOutputMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Console.WriteLine("Received:\n{0}", buffer.CreateMessage().ToString());
return null;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
reply = buffer.CreateMessage();
Console.WriteLine("Sending:\n{0}", buffer.CreateMessage().ToString());
}
}
}
<system.serviceModel>
<services>
<service name="WcfService123.Service1" behaviorConfiguration="WcfService123.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost/WcfService123/Service1.svc"/>
</baseAddresses>
</host>
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" contract="WcfService123.IService1" behaviorConfiguration="subhasisBehavior">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="ConsoleOutputBehavior" type="WcfService123.ConsoleOutputBehaviorExtensionElement, WcfService123, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService123.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<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