C# WCF instance configuration

WCF has the confusing mechanism for management of instance on RPC call. The following separating shows an example of this situation. If you are more interested in this, you can find details from the book called Programming WCF Services 3rd edition.

1. InstanceContextMode

public enum InstanceContextMode
{
    PerCall,
    PerSession,
    Single
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IMyContract
{...}

2. SessionMode

public enum SessionMode
{
    Allowed,
    Required,
    NotAllowed
}

[ServiceContract(SessionMode = SessionMode.Allowed)]
interface IMyContract
{...}

3. Binding
BasicHttpBinding cannot support Session mode. But NetTcpBinding and NetNamedPipeBinding can support this.

Therefor if you want to have PerCall InstanceContextMode, it is recommended that Session mode should be NotAllowed as shown the following example. Also you can use this strategy for PerSession InstanceContextMode with considering the WCF bindings.

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
interface IMyContract
{...}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyService : IMyContract
{...}

The following table summarize the combination of above cases.

Binding Session mode Context mode Instance mode
Basic Allowed/NotAllowed PerCall/PerSession PerCall
TCP, IPC Allowed/Required PerCall PerCall
TCP, IPC Allowed/Required PerSession PerSession
WS (no Message security, no reliability) NotAllowed/Allowed PerCall/PerSession PerCall
WS (no Message security, no reliability) Allowed/Required PerSession PerSession
WS (no Message security, no reliability) NotAllowed PerCall/PerSession PerCall
Advertisements
This entry was posted in .NET, Programming. Bookmark the permalink.

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