15

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?

4 Answers 4

33

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.

1
8

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.

7

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."

-2

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.

1
  • How exactly does this extremely late answer add any value or anything new to what has already been given?
    – slugster
    Dec 10, 2015 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.