Reputation: 11625
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?
Upvotes: 15
Views: 19327
Reputation: 1
Contract is contract between client and service provider. It tells what data will be exposed.
Service behavior is behavior of service which tells how service will behave when client will hit the service. Lets suppose if multiple clients are hitting service then whether it is able to handle to request or not, how to handle multiple request, what to do to handle multiple request. These things are handle by behavior of service.
Upvotes: -2
Reputation: 58451
A bit late for an answer but
following is in a nutshell what turned the light on for me (cudo's to Fred Yang)
ServiceContract
ServiceBehavior
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.
Upvotes: 8
Reputation: 122654
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.
Upvotes: 33
Reputation: 57939
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."
Upvotes: 7