Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF - difference between a behaviour and a contract?

Tags:

wcf

In WCF, what is the difference between a Behaviour and a Contract? From examining the config file, both seem to point to the interface of the service functionality. Why are they both needed?

like image 654
Craig Schwarze Avatar asked Jan 25 '10 03:01

Craig Schwarze


3 Answers

The Cole's Notes version:

  • The Contract specifies what the service actually does. In other words, what Operations are valid.

  • The Endpoint specifies an actual running instance of the service. It is the actual "service" in the sense that it executes, either as a Windows Service or under IIS.

  • The Service Behavior defines how the endpoint interacts with clients. Attributes like security, concurrency, caching, logging, etc. - those are all part of the behavior.

  • There is also an Operation Behavior which is similar to the Service Behavior but only gets applied when a specific operation is run.

For more information I suggest you start with the WCF Architecture Overview.

like image 186
Aaronaught Avatar answered Oct 11 '22 12:10

Aaronaught


A bit late for an answer but

  • as I'm currently struggling with the difference between the two
  • and as this is one of the first results on Google
  • and because the current answers did not give me that aha moment

following is in a nutshell what turned the light on for me (cudo's to Fred Yang)

ServiceContract

  • affects the behavior of both client and server
  • can apply to both interface and class
  • affects the WSDL emitted

ServiceBehavior

  • only affects the behavior of the server
  • only applicable to a class implementation
  • does not affect the WSDL

From ServiceContract vs ServiceBehavior

In WCF, ServiceContract attribute affect the behavior of both client and server, while ServiceBehavior only affect the behavior of server. ServiceContract can apply both to interface and class, but ServiceBehavior can only apply to class implementation. ServiceContract affect the wsdl emitted, but ServiceBehavior will not affect wsdl emitted.

like image 32
Lieven Keersmaekers Avatar answered Oct 11 '22 12:10

Lieven Keersmaekers


The Contract is the interface -- it defines the service operations exposed by the WCF service, which may or may not correspond 1:1 to an unadorned code interface your application.

The Behavior is an implementation of that interface on the host -- for all intents and purposes, "the service."

like image 21
Jay Avatar answered Oct 11 '22 10:10

Jay