Home > WCF > WCF # 13 – WCF Behaviors and it’s types [How WCF runtime is initialized?]

WCF # 13 – WCF Behaviors and it’s types [How WCF runtime is initialized?]

Hi Geeks,

In general Behavior or behaviour  refers to the actions and mannerisms made by System in conjunction with its environment, which includes the other systems around . It is the response of the system to various stimuli or inputs.

* WCF Behaviors are classes that affect runtime operation.

* It comes in to the picture when WCF runtime(execution) starts on the client and server       and message flow starts in between the two.

For Example

ServiceHost is responsible for defining the instancing and
concurrency aspects of a server, in addition to dispatching messages to the
proper operation.

a) When a message is received by a service and dispatched
to a certain method of a class,

should ServiceHost  create a new instance
of that class for each request or should it reuse instances?

b) And when
ServiceHost  calls the method on that class, should it enlist in a transaction?
Both of these are specified in behaviors and used during initialization.

** There are three primary types of behaviors.

1) Service Behaviors

2) Endpoint Behaviors

3) Operation Behaviors

4) CallBack Behaviors

1) Service behaviors  run at the service level and have access to all of the endpoints. They control items such as instancing and transactions. Service behaviors are also available for authorization and auditing.

2) Endpoint behaviors are scoped to the service endpoint. These are well-suited for inspecting and taking actions on messages as they come in and out of service.

3) Operation behaviors are scoped at the operation level and are well-positioned for manipulating serialization, transaction flow, and parameter handling for a service operation.

4) CallBack Behaviors are similar to the Service Behaviors but control the Endpoints created in duplex communication.


** To understand how behaviors are used, it’s helpful to know

How the runtime is initialized?

This is done by the ChannelFactory on the client and by ServiceHost on the server. Both classes perform similar functions:

1. Accept a .NET type as input and read its attributed information.                                              
2. Load configuration from Web.Config and App.Config files. On the client, ChannelFactory is   primarily looking at binding information; on the server,ServiceHost  is primarily looking at contract and binding information.                                                                                                
3. Build the runtime environment structure,ServiceDescription                                                  
4. Start communication. On the client, ChannelFactory uses the channel to connect to the service; on the server, ServiceHost  opens the channel and listens for messages.


** In addition to initialization, Behaviors can also operate on data before it is transmitted or after it is received. On the client, Behaviors can be used to perform three functions:

1. Parameter Inspection. Inspect and/or change data in its .NET representation,
before it has been converted to XML.

2. Message Formatting. Inspect and/or change data during its conversion
between .NET types to XML.

3. Message Inspection. Inspect and/or change data in its XML representation,
before it has been converted to .NET types.
On the server, Behaviors can be used for two additional scenarios:

4.Operation Selection. At the service level, inspect the incoming message
and determine which operation should be called.

5. Operation Invocation. At the operation level, invoke the class method.


** Following diagram shows the flow of control among behavior elements that are
invoked when messages are sent between the client and server.


In above diagram the GetPrices() is the method hosted on the server and which is being called by the client and execution takes place in following steps

Step 1. When application code on the client calls GetPrices(), the Parameter Inspector and
Message Formatter are called and passed the parameters in their .NET format.

Step 2.Then, also on the client, the Message Inspector is called and passed the
XMLmessage.                                                                                                                            On the service, when the message arrives in the channel, the Message Inspector and Operation Selector are called and passed the incoming message to inspect and determine which operation should receive it.

Step 3.The Message Formatter is then called to format the message as a .NET type,
and the Parameter Inspector is called and passed the message in its .NET

Step 4. Finally, the Operation Invoker is called to invoke the
method on the target class, doing any setup and teardown along the way.

Thank You.

Categories: WCF
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: