Instance Deactivation
Conceptually, the sessionful service instance management technique as described so far connects a client (or clients) to a service instance. Yet, the real picture is more complex. Recall from Chapter 1 that each service instance is hosted in a context, as shown in Figure 4-2.

Figure 4-2. Contexts and instances
What sessions actually do is correlate the client messages not to
the instance, but to the context that hosts it. When the session starts,
the host creates a new context. When the session ends, the context is
terminated. By default, the lifetime of the context is the same as that
of the instance it hosts. However, for optimization and extensibility
purposes, WCF provides the service designer with the option of
separating the two lifetimes and deactivating the instance separately
from its context. In fact, WCF also allows a context to exist without an
associated instance at all, as shown in Figure 4-2. I call this instance management
technique context deactivation. The common way of
controlling context deactivation is via the
ReleaseInstanceMode
property of the OperationBehavior attribute:
public enum ReleaseInstanceMode
{
None,
BeforeCall,
AfterCall,
BeforeAndAfterCall,
}
[AttributeUsage(AttributeTargets.Method)]
public sealed class OperationBehaviorAttribute : Attribute,...
{
public ReleaseInstanceMode ReleaseInstanceMode
{get;set;}
//More members
}ReleaseInstanceMode ...