By and large, the service instance mode is strictly a service-side implementation detail that should not manifest itself on the client side in any way. To support that and a few other local service-side aspects, WCF defines the notion of behaviors. A behavior is a local attribute of the service or the client that does not affect its communication patterns. Clients should be unaware of service behaviors, and behaviors do not manifest themselves in the service’s binding or published metadata. You have already seen two service behaviors in the previous chapters: Chapter 1 uses the service metadata behavior to instruct the host to publish the service’s metadata over HTTP-GET or to implement the MEX endpoint, and Chapter 3 uses the service behavior to ignore the data object extension. No client can ever tell simply by examining the communication and the exchanged messages if the service is ignoring the data object extension or who published its metadata.
WCF defines two types of declarative service-side behaviors,
governed by two corresponding attributes. The ServiceBehaviorAttribute
is used to configure
service behaviors; that is, behaviors
that affect all endpoints (all contracts and operations) of the service.
Apply the ServiceBehavior
attribute
directly on the service implementation class.
Use the OperationBehaviorAttribute
to configure
operation behaviors; that is,
behaviors that affect only the implementation of a particular operation.
The OperationBehavior
attribute can
be applied only on a method that implements a contract operation, never
on the operation definition in the contract itself. You will see the use
of Operation
Behavior
attribute later in this chapter
and in subsequent chapters as well.
In the context of this chapter, the ServiceBehavior
attribute is used to configure the service instance mode. As shown in
Example 4-1, the
attribute defines the InstanceContextMode
property of the enum type InstanceContextMode
. The value of the Instance
Context
Mode
enum controls which
instance mode is used for the service.
Example 4-1. ServiceBehaviorAttribute used to configure the instance context mode
public enum InstanceContextMode { PerCall, PerSession, Single } [AttributeUsage(AttributeTargets.Class)] public sealed class ServiceBehaviorAttribute : Attribute,... { public InstanceContextMode InstanceContextMode {get;set;} //More members }
The enum is correctly called InstanceContextMode
rather than InstanceMode
because it actually controls the
instantiation mode of the context hosting the instance, rather than that
of the instance itself (recall from Chapter 1
that the instance context is the innermost execution scope of the
service). By default, however, the instance and its context are treated
as a single unit, so the enum does control the life of the instance as
well. You will see later in this chapter and in subsequent chapters how
(and when) you can disengage the two, and for what purposes.
Get Programming WCF Services, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.