How to add custom EndPointBehavior in web.config service?

I followed in this article and created the clusters MyMessageInspector and MyEndPointBehavior , as shown 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 in web.config?

I have added the following 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 below, he complains:

 <serviceBehaviors> <behavior> <myMessageInspector/> 

His complaint is as follows:

Invalid item in configuration. The extension "myMessageInspector" does not come from the correct base extension type "System.ServiceModel.Configuration.BehaviorExtensionElement".

How to add MyEndPointBehavior in web.config?

+6
source share
1 answer

You must also create a custom BehaviorExtensionElement and use it in the web.config file. There are many articles that can help you, like these

http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector

http://cgeers.com/2011/05/10/wcf-message-logging/

http://burcakcakiroglu.com/?p=2083

http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/

In any case, correct your code 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 } 

Add a new BehaviorExtensionElement here.

 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> 
+12
source

All Articles