Instancing in WCF



The following is what is in this article:

  • What InstanceContextMode is
  • PerSession
  • PerCall
  • Single
  • Attachment
  • Summary
What InstanceContextMode is

 InstanceContextMode determines the client proxy to communicate with the server in the following manners: 

  • Using one and the same instance of the server object (PerSession)
  • Using the new instance that is created on, every method call (PerCall)
  • Using the single instance that is created for all the clients (Single)

 What exactly are the benefits of using each InstanceContextMode? In which situation each InstanceContextMode should be used? We will discuss all the InstanceContextMode in detail

PerSession

One server instance per client session means the client proxy communicates with one and only one server instance until the session times out. In other words the server object resides on the server as long as the session times out (default 10 minutes).

  • Advantage: The main advantage of Per session is that values that have been set are retained on the server side without any additional measures.
  • Disadvantage: scalability is the main disadvantage here, because every active session takes memory in the server regardless of the client currently requires it or not.

Per session requires you to use a protocol that supports sessions. For example, you can use nettcpbinding with the connection oriented protocol TCP that allows the server to allocate the incoming message to the correct server object. The basichttpbinding is unsuitable because the transfer protocol HTTP is connectionless. You can also enforce the service to use the right binding by using the keyword SessionMode.Required in the service contract attribute.



The preceding is the declaration of InstanceContextMode->Session. You need to use the service behavior attribute at the top of the service implementation class.



In the attached source code, if you see the service interface method the iInstanceCount will be used. It is just used to hold the number of times this method has been executed. As I said earlier, the Persession actually creates one server instance per session client and the information will be retained in the server until the session expires. In our case the iInstanceCount will be incremented on, every method call from the client proxy. And the following snapshot is the output of the program.



You can find that information will be maintained in the service. For further details please refer to the attached source code.

PerCall

One server instance per method call means the client proxy communicates with the new service instance, that is created on, every method call from the client proxy. Yes, on every server method invocation from the client proxy the new instances will be created. As soon as the method is processed the new instance will be released again. If the service object implements the IDisposable interface, the Dispose method is called automatically after the result is sent to the proxy. This is extremely scalable since the object is released on every method call. Also there is no need to worry about threading problems. But the main drawback here is we cannot maintain the information in the server side.



The preceding one is the declaration of InstanceMode->Percall. You need to use the service behavior attribute at the top of the service implementation class.



In the attached source code, if you see the service interface method the iInstanceCount will be used. It is just used to hold the number of times this method has been executed. As I said earlier, the PerCall actually creates one server instance per method call, so the information will not be retained on the server. In our case the iInstanceCount will be incremented on, every method call from the client proxy and will be disposed of after the method call. And the following snapshot is the output of the program.



Refer to the attached code for the further details.

Single

One server instance for all the clients, as the name suggests, one server instance is created for all the clients connecting to the server. We will often come across the situation of creating global instances for all the clients. for that InstanceMode.Single would be very useful. The service instance created to serve for client 1 will also used to serve the clients 2 and 3.

 

The above one is the declaration for the InstanceMode->Single.



As I sais earlier, the global server instance will be created for all the clients. Notice that in the preceding output you can find that the Instance number will be shared across all the clients. If you run the second client the Instancenumber value will be incremented from the 100 where the client1 has finished the process. You can refer to the attached source code for further details.

Attachment

Refer to the source code attachment for further details.

Summary

PerSession One Server instance on every client session
PerCall One Server instance on every method call
Single One Server instance for all the clients


Similar Articles
View All Comments