Adding behaviors programmatically

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Operation behavior(s) can be programmatically added either on the service or on the consumer side.

Service

When you have an instance of the host, grab out the service description (host.Description), then find the endpoint which hosts the contract. In the list of operations of the contract there is a place where to add operation behaviors. Here is an example:

  host1.Description.EndpointsNo.Contract.Operations[M].Behaviors.Add(behavior);

 

If you do not have host instance (for example hosting in IIS) then you should implement your custom host and perform the same action as described above.

Consumer

If you want to add an operation behavior on the client side the official documentation is a bit confusing. Adding behavior before the factory is created is finde, but there is not description how to do that. One factory is opened by invoking of factory.CreateChannel(). If you ever implemented your own communication object this happens in OnCreateChannelmethod of your factory.

Anyhow, the behavior has to be added before you call factory.CreateChannel(), and that is after

factory = new ChannelFactory<IMyExtendedService>(binding, address);

So, here is the ultimate one-line-code solution:

 factory.Endpoint.Contract.OperationsNo.Behaviors.Add(myBehavior as IOperationBehavior);

 


For more information see http://msdn.microsoft.com/en-us/library/system.servicemodel.description.ioperationbehavior.aspx.


Posted Jun 03 2009, 12:01 AM by Damir Dobric
Filed under:

Comments

Hannes kock wrote re: Adding behaviors programmatically
on 01-29-2010 15:15

thx m8, valuable info

developers.de is a .Net Community Blog powered by daenet GmbH.