Skip to main content

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 class StudentService : IStudentService
{
...

8. Repeat step 3 to step 5. Notice that for each method call a new service instance is created.

9. Let's look at how the PerSession ServiceInstanceMode works. To use PerSession mode do the following changes.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class StudentService : IStudentService
{
...

10. Repeat step 3 to step 5. Notice that for each method call a new service instance is created. This is because we are using basicHttpBinding.

11. In the ConsoleClient, change the binding used by the client instance to netTcpBinding as shown below:

StudentServiceClient client = new StudentServiceClient("NetTcpBinding_IStudentService");

12. Repeat step 3 to step 5. Notice that a new instance is created for each new client but not for each method call.

13. To ensure that only the binding that supports session is used to access the service. Add the following annotation to service contract.

[ServiceContract(SessionMode=SessionMode.Required)]
public interface IStudentService
{
...

14. Run the ConsoleHost. Notice that an exception is thrown. Comment or remove basicHttpBinding. Run the ConsoleHost again. Notice that it works fine now.

We can also control if an specific operation in a service contract is allowed to initiate or terminate the session. If IsInitiating is set to true for a specific operation, it creates a new service instance is created when that operation is called. If IsInitiating is set to false for a specific operation, then it is not allowed to create a new service instance when that operation is called.

If IsTerminating is set to true for a specific operation, it disposes the service instance after serving the call. If IsTerminating is set to false for a specific operation, then it does not dispose service instance after serving the call.

15. Add the following annotation above RemoveStudent in service contract.

[OperationContract(IsInitiating=false)]

void RemoveStudent (string stuid); ...

16. Add the following annotation above ListStudent in service contract. 

[OperationContract(IsInitiating = false,IsTerminating=true)]
IEnumerable<Student> ListStudents();...

17. Build solution. Set ConsoleHost as the start up project. Press Ctrl + F5 (start without debugging). Update the ServiceReference of the ConsoleHost.

18. Enable Multiple startup projects to start ConsoleHost and ConsoleClient. Click Start or  press F5.

19. In the client console window, select option 2. Notice that an InvalidOperationException is thrown with the following message. You are not allowed to call the RemoveStudent since it is not initiating.

"The operation 'RemoveStudent' cannot be the first operation to be called because IsInitiating is false."

20. Try calling option 4. Notice that again an InvalidOperationException is thrown for the same reason as above.

21. Add a student (option 1). This is allowed because there is no annotation for AddStudent method and by default IsInitiating is set to true and IsTerminating is set to false.

22. Add one more student and call option 2 to remove one student.

23. Try calling option 4. Notice that it works now. But since IsTerminating is set to true, the service instance will be disposed after the call.

24. Now try removing a student (option 2). It should throw an InvalidOperationException again.








Comments

Popular posts from this blog

A Comprehensive Evaluation of the Internal Consulting Process: Steps and Considerations

Introduction Internal consulting has emerged as a critical function within organizations, offering in-house expertise to solve complex business problems and drive change. It closely mirrors external consulting in methodology but is differentiated by the consultant's intimate knowledge of the organization and a vested interest in its long-term success. This article aims to evaluate the key steps involved in the internal consulting process, offering insights into each phase's significance and challenges. Steps in the Internal Consulting Process The internal consulting process can generally be segmented into five distinct stages: Initial Assessment, Data Collection and Analysis, Solution Development, Implementation, and Evaluation. Below is an evaluation of each step: Step 1: Initial Assessment Objective: To understand the problem or opportunity area and define the scope of the project. Significance: A well-defined scope ensures that the consulting project stays focused and manage...

The Evolving Landscape of Consulting Practice: Changes and Implications

Introduction Consulting is a field that thrives on its ability to adapt to market demands and emerging trends. As businesses evolve due to technological advancements, shifts in consumer behavior, and fluctuations in global markets, consulting practices must keep pace. This article explores some of the significant changes currently transforming the consulting industry and discusses their implications for both consultants and clients. Technological Disruption Data Analytics and Artificial Intelligence Consulting firms are increasingly integrating data analytics and artificial intelligence into their service offerings. These technologies allow consultants to offer data-driven insights that can significantly enhance strategic decision-making. This evolution means consultants now need skills in data interpretation and analysis, alongside their traditional expertise in business strategy. Virtual Consulting Platforms The advent of digital platforms enables consulting services to be offered re...

The Skillset of Internal Consultants: A Comparative Analysis

Introduction In the organizational landscape, the role of internal consultants has gained prominence due to the increasing complexity of business problems and the need for specialized in-house expertise. While many skills required for internal consulting overlap with those of external consultants, there are distinct abilities that set them apart. This article aims to compare and contrast these skill sets to provide a clearer understanding of what makes an effective internal consultant. Skills Common to Both Internal and External Consultants Problem-Solving Both types of consultants need to excel at identifying issues and creating viable solutions. Critical thinking and analytical skills are paramount for dissecting complex situations and recommending actionable strategies. Communication Excellent communication skills are a must for any consultant. Whether it’s making a presentation to stakeholders, writing a report, or simply discussing ideas with a team, effective communication is key...