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
{
...
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
{
...
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
{
...
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."
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
Post a Comment