Thursday, December 10, 2009

Difference between IServiceBehavior and IEndpointBehavior

I believe this could sound silly, but for those who are fairly new to WCF definitely would like know. By looking at the interface names, we can say that one is for extending the "Service" and the other is for extending "Endpoints". We have recently done couple of interesting things in our organization and thought would share this information with you guys.

The ApplyDispatchBehavior method on IServiceBehavior has access to all endpoints and their runtime components and so shouldn't that be enough for wiring up customization? The short answer is yes, but there are subtle differences.

Some usability differences are:
  • ServiceBehavior applies only on a Service while EndpointBehavior applies on both client and service.

  • ServiceBehavior can be specified via Attributes/Code/Config file while Endpointbehavior can be specified through Code or Config file.

  • ServiceBehavior has access to all ServiceEndpoints dispatch runtime and so could modify all dispatch runtimes while EndpointBehavior gets called with the runtime for that endpoint only.


Look at it this way, ServiceBehavior lets you access runtime parameters for all endpoints while EndpointBehavior lets you access runtime components only for that endpoint. So if you have a need to extend functionality that spawns the entire contract (or multiple contracts) then use ServiceBehavior and if you are interested in extending one specific endpoint then use EndpointBehavior.

And of course, the biggest difference is if you want to customize endpoints on client/consumer then the only option is IEndpointBehavior.

2 comments:

Thyagaraju Govardhan said...

Neat explanation... Thanks for Sharing

Thyagaraju Govardhan

Contracts for Difference said...

This was very informative. I have been reading your blog a lot over the past few days and it has earned a place in my bookmarks.