Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a Custom EndPointBehavior to the web.config of the service?

Tags:

c#

asp.net

wcf

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?

like image 348
The Light Avatar asked Mar 26 '13 12:03

The Light


1 Answers

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>
like image 186
Milan Matějka Avatar answered Oct 12 '22 13:10

Milan Matějka