Worker Role CommunicationVersion: 2.1.1 DescriptionIn this hands-on lab, you will explore internal and external endpoints by implementing a WCF chat service and host it in a Windows Azure worker role. OverviewWeb roles in Windows Azure provide support for HTTP and HTTPS protocols through their endpoints. For other TCP/IP protocols and ports, external endpoints allow worker roles to expose Internet-facing services over ports other than 80 and 443. These endpoints can be bound to any process and provide the means to communicate externally using a variety of TCP-based application protocols. Similarly, for inter-role communications, internal endpoints provide a way for roles to communicate directly with each other without the need to go through queues, although queues remain the preferred method for reliable asynchronous message processing. In this hands-on lab, you will explore internal and external endpoints by implementing a WCF chat service and host it in a Windows Azure worker role. The AzureTalk Service In the solution, a Windows Azure worker role hosts the WCF service, which listens on an external TCP endpoint that you configure for this role. The service implements a duplex contract over a TCP channel that allows clients to register, send and receive messages, and receive notifications from the service over the same channel.
Figure 1 Chat service duplex contract A typical exchange between the client application and the worker role service involves the following operations:
In order for the service to deliver notifications and messages from other users, the client application needs to implement a callback contract, which the service specifies in its own contract using the CallbackContract attribute.
Figure 2 Duplex service specifying its callback contract (C#)
Figure 3 Duplex service specifying its callback contract (Visual Basic) The contract for the chat service indicates that it requires sessions through its SessionMode setting. In addition, because the contract marks the Register operation as being initiating by means of the IsInitiating attribute, whenever a client invokes this particular operation, the WCF infrastructure establishes a session with the client and assigns an identifier to this session. Thereafter, and as long as the client maintains the session open, WCF assigns the same session ID to every operation invoked by the client and makes it available in the operation context through its SessionId property. The chat service uses this identifier to correlate every exchange from a particular client.
Figure 4 Service contract session requirements (C#)
Figure 5 Service contract session requirements (Visual Basic) In addition to the WCF session, the chat service also maintains its own notion of a session, which allows it to keep track of each connected client. The service assigns a SessionInformation object to each active client and uses it to register information about its session. To manage sessions, the service keeps an internal dictionary of SessionInformation objects and uses the session ID assigned by WCF as the key to access the sessions it contains. When creating a new session during client registration, the service records the session ID, a user name for the client, and a callback channel to the client. The callback channel allows the service to invoke operations on the client application including forwarding messages from peers and alerting when other users connect or disconnect. Additionally, the session contains an ID for the worker role where the client session is active. This piece of information will become significant in Exercise 2, when you implement an inter-role communication mechanism that enables communication between clients connected to different worker roles.
Figure 6 Managing sessions in the Chat Service Because multiple clients using the service can read and update sessions concurrently, the service uses a SessionManager class to control access to the session information dictionary in a thread-safe manner. The code provided with the lab already contains the chat service contracts, an implementation for the Session Manager, and a client application that you can use to test the service. During the course of the lab, you will configure the internal and external endpoints of the worker role and implement the chat service. ObjectivesIn this hands-on lab, you will learn how to:
PrerequisitesThe following is required to complete this hands-on lab:
SetupIn order to execute the exercises in this hands-on lab you need to set up your environment.
Note: Make sure you have checked all the dependencies for this lab before running the setup. Using the Code SnippetsThroughout the lab document, you will be instructed to insert code blocks. For your convenience, most of that code is provided as Visual Studio Code Snippets, which you can use from within Visual Studio 2010 to avoid having to add it manually. If you are not familiar with the Visual Studio Code Snippets, and want to learn how to use them, you can refer to the Setup.docx document in the Assets folder of the training kit, which contains a section describing how to use them. ExercisesThis hands-on lab includes the following exercises:
Estimated time to complete this lab: 60 minutes. Note: When you first start Visual Studio, you must select one of the predefined settings collections. Every predefined collection is designed to match a particular development style and determines window layouts, editor behavior, IntelliSense code snippets, and dialog box options. The procedures in this lab describe the actions necessary to accomplish a given task in Visual Studio when using the General Development Settings collection. If you choose a different settings collection for your development environment, there may be differences in these procedures that you need to take into account. |
.png)
.png)
.png)
.png)
.png)
.png)