Skip to main content

Posts

Showing posts with the label InstanceContextMode

Service Instance and InstanceContextMode attribute

InstanceContextMode can be set to PerCall, Single or PerSession. InstanceContextMode.PerCall Description New service instance for each call. To enable set InstanceContextMode.PerCall Service Instance created when The instance is created when a request is received from the client Service Instance disposed when Disposed after servicing a single call. Benefits Scalable, Service instance disposed after each call. Issues Stateless, lowers performance, Clients not aware of multiple instances created during session. InstanceContextMode.Single Description Single instance is used to serve all calls. To enable set InstanceContextMode.Single Service Instance created when The instance is created when the service host is created Service Instance disposed when Disposed when service host is closed. Benefits Easy to manage...

Controlling life cycle of a Service instance

1. Download the start file here . 2. To use a single service instance to handle request all clients, add the following annotation above the service implementation. [ ServiceBehavior (InstanceContextMode = InstanceContextMode .Single)] public class StudentService : IStudentService { ... 3. Run the ConsoleHost and ConsoleClient, add a student then use option 3 and 4. Notice the instance HashCode for all three service calls. 4. When the above ConsoleHost and ConsoleClient is running, add another ConsoleClient. 5. Using the new ConsoleClient, add a student then use the option 3 and 4. Switch to the first ConsoleClient and add another student. Then use option 4. 6. Notice that all of the above calls use just a single instance or a Singleton of the service. 7. Now, let's see how the PerCall ServiceInstanceMode behaves. To change it PerCall mode do the following changes. [ ServiceBehavior (InstanceContextMode =  InstanceContextMode .PerCall)] public ...