tags:

views:

214

answers:

2

I have written a custom MessageEncoder for a WCF pipeline, which is then applied using a BindingElementExtensionElement within my app.config.

On the server, when a message arrives, my message encoder needs to pull some information from the stream, and hold it for the duration of the Operation Context, so that it may be returned as a header in the response. The header manipulation screams of Behavior functionality, so... I would like to know, how do I get my custom encoder to also apply a custom Behavior to the pipeline (which is where I will take care of storing this special 'information' across an operation context) - the encoder is essentially useless without the behavior, so I dont want to rely on the user remembering to add the behavior config when they add the encoder config - it should just happen automatically under the covers.

Additionally, where is a good resource for reading about the lifecycle of these pipeline elements?

Thanks

A: 

I wrote custom headers and message inspectors for WCF and found this article and some of the links in it useful along my way:

Handling custom SOAP headers via WCF Behaviors

HTH

Tanner
A: 

You have basically two options:

1) either you expose your behavior via an attribute, so that it can be added to the server's config file - something like this:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <YourCustomMessageEncoderBehavior />
        </behavior>
      </serviceBehaviors>
    </behaviors>

2) or you can create your own custom ServiceHost which makes sure to add this behavior to the list of dispatch behaviors if it's not already there.

class YourCustomServiceHost : ServiceHost
{
    public YourCustomServiceHost(Type serviceType, params Uri[] baseAddresses)
           : base(serviceType, baseAddresses) { }

    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();     

        YourCustomMessageEncodingBehavior behavior = 
           this.Description.Behaviors.Find<YourCustomMessageEncodingBehavior>();

        if (behavior == null)
        {
           behavior = new YourCustomMessageEncodingBehavior();
           this.Description.Behaviors.Add(behavior);
        }
        else
        {
           return;
        }
    }
}

Marc

marc_s