I have followed this article and have created MyMessageInspector
and MyEndPointBehavior
clases as below:
public class MyMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("Incoming request: {0}", request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
public class MyEndPointBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
How to add MyEndPointBehavior to the web.config?
I've added the below extensions:
<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.MyEndPointBehavior, MessageInspectorProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
But when I try to use it in below, it complains:
<serviceBehaviors>
<behavior>
<myMessageInspector/>
Its complain is as below:
Invalid element in configuration. The extension 'myMessageInspector' does not derive from correct extension base type 'System.ServiceModel.Configuration.BehaviorExtensionElement'.
How to add MyEndPointBehavior
to the web.config?
You have to also create a custom BehaviorExtensionElement and use it in web.config file. There are many articles which can help you like these
http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector
https://github.com/geersch/WcfMessageLogging
http://burcakcakiroglu.com/?p=2083
http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/
Anyway fix your code to this way
public class MyMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("Incoming request: {0}", request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
public class MyEndPointBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
Here add new BehaviorExtensionElement
public class CustomBehaviorExtensionElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new MyEndPointBehavior();
}
public override Type BehaviorType
{
get
{
return typeof(MyEndPointBehavior);
}
}
}
And update your web.config
<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.CustomBehaviorExtensionElement, MessageInspectorProject"/>
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior>
<myMessageInspector />
</behavior>
</endpointBehaviors>
</behaviors>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With