Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default instance context mode?

Tags:

wcf

When I do not specify InstanceContextMode in the service, what's the default instance mode?

like image 371
user496949 Avatar asked Jan 17 '11 08:01

user496949


People also ask

What is instance context mode?

The InstanceContextMode property interacts with some other settings. For example, if the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to Multiple.

What is default instance context mode in WCF?

Per-call service is the default instance activation mode of WCF. When a WCF service is configured for a per-call service, a CLR object is created for the timespan a client call or request is in progress.

What are the different instance modes in WCF?

The WCF framework has defined the following three Instance Context modes: PerCall, PerSession, and Single.

What is concurrency mode in WCF?

In WCF, concurrency issues can arise when two or more threads try to access the same resource at the same time. Note that a WCF service can handle one single request at a time. Concurrency in WCF enables you to control multiple active threads in an InstanceContext at a particular point of time.


3 Answers

It's PerSession

Link to MSDN doc

like image 177
vc 74 Avatar answered Nov 08 '22 09:11

vc 74


The simple answer is that the default Instancing mode is PerSession

Provided:

  • The Session Type you are using supports sessions
  • See [Binding type session support] (https://learn.microsoft.com/en-us/dotnet/framework/wcf/system-provided-bindings).
  • If the channel does not create a session the behavior is as if it were PerCall.
  • The Service contract allows sessions - default is "Allowed"

Here is a Microsoft provided sample with the default imperatively configured in the code. Default behavior sample

[ServiceBehavior(  
AutomaticSessionShutdown=true,  
ConcurrencyMode=ConcurrencyMode.Single,  
InstanceContextMode=InstanceContextMode.PerSession,  
IncludeExceptionDetailInFaults=false,  
UseSynchronizationContext=true,  
ValidateMustUnderstand=true)]
public class CalculatorService : ICalculator { ... }

I found reading about session in this Microsoft article ( Using Sessions ) particularly enlightening in understanding how Sessions is opened and closed and how this relates to Instancing and Concurrency.

By default the WCF client will create a new session, which will create a server instance, all calls for the duration of the session is called a conversation and is served by a single instance (Instancing) of the server with a single thread (Concurrency) dedicated to that session/client/conversation.

If you use the default instancing behavior in WCF, all calls between a WCF client object are handled by the same service instance. Therefore, at the application level, you can think of a session as enabling application behavior similar to local call behavior. For example, when you create a local object:

A constructor is called.

All subsequent calls made to the WCF client object reference are processed by the same object instance.

A destructor is called when the object reference is destroyed.

Sessions enable a similar behavior between clients and services as long as the default service instance behavior is used.

Hope this helps someone as it took me a while to find the answer.

like image 20
Rohan Avatar answered Nov 08 '22 09:11

Rohan


Not all bindings support PerSession mode like basicHttpBinding that supports Percall mode by default.

like image 31
Debab Ramzi Avatar answered Nov 08 '22 09:11

Debab Ramzi