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

Excel PMT Function

PMT function is very useful for calculating monthly payment required to payback a loan or mortgage at a fixed rate. This function require a minimum of three inputs, periodic rate, number of periods, present value or the loan amount. Here is a simple example. Home Loan: 350,000.00 Interest rate: 4.5% Number of years to repay the loan: 25 Note: To calculate monthly payment, we need to find the monthly rate and number of months as shown above. Then it is simply a matter of substituting the values into the payment function, as shown in the formula view below.

CUMIPMT and CUMPRINC function

CUMIPMT Cumulative interest payment function allows you to calculate the interest paid for a loan or from an investment from period A to period B. When getting a loan, CUMIPMT function can be used to calculate the total amount of interest paid in the first five months or from period 12 to period 20. A period can be a month, a week or two week. Loan Amount : 350,000.00 APR: 4.5% Down payment: 0.00 Years: 25 Payment per year: 12 From the above data, we can calculate the following: No of Period: 25 × 12 = 300 Periodic Rate: 4.5/12 = 0.375% Here is how you will substitute these values into the function. = CUMIPMT (periodic rate, No of period, vehicle price, start period, end period,  ) = CUMIPMT (0.375, 300, 350000, 1, 5, 0) In an excel worksheet, we use cell address instead of actual values as shown below: Here is the formula view of the worksheet: CUMPRINC Another related function is CUMPRINC. CUMPRINC function is used to calculate cumul