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
1
2
3
4
5
6
7
8
9
public enum InstanceContextMode
{
PerCall,
PerSession,
Single
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IMyContract
{...}
2. SessionMode
1
2
3
4
5
6
7
8
9
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.
1
2
3
4
5
6
[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 |