Skip to main content

Posts

Showing posts with the label Singleton

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 ...