WCF INSTANCE MANAGEMENT Dhananjay Kumar MVP- Connected System
Feb 20, 2016
WCF INSTANCE MANAGEMENT Dhananjay Kumar
MVP-Connected System
Instance Management is a technique to decide, which SERVICE INSTANCE handles which CLIENT REQUEST and when?
This is a service side implementation detail This is useful to decide scalability,
performance, durability, transactions and queued calls
2
Instance Management
3
Instance Management
4
Instance Management
5
Instance Management
BEHAVIOR Instance management is a service side activation
implementation detail that should not get manifested to the client. WCF achieve this by BEHAVIOR.
A Behavior is a local attribute of service that does not affect its communication patterns.
Client does not aware of the Behavior. Service does not manifest behavior in binding or
metadata. Practically Behaviors are WCF classes. These are used at the runtime operations.
6
Instance Management
7
Instance ManagementBEHAVIOR
SERVICE BEHAVIOR
This applies directly to service implementation class.
This affects all the endpoints. This is used to configure service instance mode.
8
Instance Management
OPERATION BEHAVIOR
This is used to configure operation behavior. This affects only implementation of a particular
operation. This could apply only to method that implements
contract operation. This is not applied to the contract definition itself.
9
Instance Management
10
Instance Management
Per Call Instance ModeEvery Client request gets a new dedicated service instance. A service instance exists only when a client call is in progress.
11
Instance Management
Per Call Instance Mode
12
Instance Management
Per Call Instance Mode
13
Instance Management
Per Call Instance Mode
Demo
14
Instance Management
Per Session Instance Mode
15
Instance Management
Per Session Instance Mode
16
Instance Management
Per Session Instance Mode WCF can maintain a logical session between a client and service. The service instance remains in the memory throughout the
session. The client session is per service endpoint per proxy.
17
Instance Management
Configuring Session full Service
18
Instance Management
Behavior FacetsThe Behavior part is required, so that WCF will keep the
Service Instance Context alive throughout the session.
19
Instance Management
Behavior Facets InstanceContextMode.PerSession is default value for InstanceContextmode
property. The session terminates when client closes the proxy. Proxy has to notify the service that session has been closed and service calls the
Dispose () method on worker thread.
20
Instance Management
Contract FacetsIn order to create all messages from a particular client to a particular instance WCF needs to identify the client and this is done by TRANSPORT SESSION
21
Instance Management
Contract Facets
For this ServiceContract attribute having a property called SessionMode. Type of property SessionMode is enum SessionMode. It is inside namespace System.ServiceModel. Property SessionMode is a public property. SessionMode enum is default to SessionMode.Allowed. The Configured value of SessionMode is exposed to client in MetaData.
22
Instance Management
Contract Facets
23
Instance Management
SessionMode.Allowed When the SessionMode is configured with SessionMode.Allowed, transport session are allowed but not
enforced. The Behavior of service will depend upon behavior configuration and binding.
24
Instance Management
SessionMode.Allowed
25
Instance Management
SessionMode.Required SessionMode.Required value restricts to use Transport level session. If a contract is configured as SessionMode.Required with a service end point who's binding does not
maintain a Transport level session a Run time error will encountered at loading of service.
26
Instance Management
SessionMode.Required
27
Instance Management
SessionMode.Required
28
Instance Management
SessionMode.NotAllowed This disallows the use of Transport session. Regardless of the service configuration, when SessionMode.NowAllowed is configured at contract, the service will always behave as Per-call
service.
29
Instance Management
SessionMode.NotAllowed
30
Instance Management
SessionMode.NotAllowed
DEMO
31
Instance Management
Singleton Instance Mode
32
Instance Management
Singleton Instance Mode
33
Instance Management
Singleton Instance Mode
The Singleton Service Instance created only once. It created when the service host is created. It lives forever. It got disposed only when host shuts down. Singleton service instance does not require client to maintain any logical session. It does not required to use binding that supports transport level session.
34
Instance Management
Singleton Instance Mode
35
Instance Management
Singleton Instance Mode
Having scalability problem. All requests to service will run on different worker thread, so it will cause synchronization problem. Only one client could have access to singleton service instance at a time. It will decrease throughput of the service. Responsiveness is very low Singleton Service instance should only be used, when no other options are available. For instance Global Log Book, Single Communication Port etc.
36
Instance Management
Singleton Instance Mode
DEMO
37
Instance Management
Thank You.
Dhananjay KumarMVP-Connected System