Step 1: Introduction and Prerequsites
Queues offer First In, First Out (FIFO) message delivery to one or more competing consumers. That is, messages are typically expected to be received and processed by the receivers in the temporal order in which they were enqueued, and each message will be received and processed by only one message consumer. A key benefit of using queues is to achieve “temporal decoupling” of application components: in other words, the producers and consumers do not need to be sending and receiving messages at the same time, since messages are stored durably in the queue. A related benefit is “load leveling”, which enables producers and consumers to send and receive messages at different rates.
The following are some administrative and prerequisite steps you should follow before beginning the tutorial. The first is to create a Windows Azure service namespace, and to obtain a shared secret key. A service namespace provides an application boundary for each application exposed through the Service Bus. A shared secret key is automatically generated by the system when a service namespace is created. The combination of service namespace and shared secret key provides a credential for the Service Bus to authenticate access to an application.
To create a service namespace and obtain a shared secret key
-
To create a service namespace, follow the steps outlined in How to: Create or Modify a Service Bus Service Namespace.
-
In the main window of the Windows Azure Management Portal, click the name of the service namespace you created in the previous step.
-
At the bottom of the portal window, click Access Key.
-
In the Connect to your namespace window, make a note of the default issuer and default key, or copy them to the clipboard. You will use these values later in this tutorial.
The next step is to create a Visual Studio 2012 project and write two helper functions that load a comma-delimited list of messages into a strongly-typed (BrokeredMessage) .NET Framework List object.
To create a Visual Studio 2012 project
-
Open Visual Studio 2012 as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.
-
Create a new console application project. Click the File menu and select New, then click Project. In the New Project dialog, click Visual C# (if Visual C# does not appear, look under Other Languages), click the Console Application template, and name it QueueSample. Use the default Location. Click OK to create the project.
-
In the Solution Explorer, right-click the name of your project (in this example, QueueSample), and click Properties.
-
Click the Application tab on the left, then select .NET Framework 4 from the Target framework: dropdown. Click Yes when prompted to reload the project.
-
Add references to the Microsoft.ServiceBus, System.Runtime.Serialization, and System.ServiceModel assemblies:
-
In the Solution Explorer, right-click the References folder under the project folder and then click Add Reference….
-
Select the .NET tab in the Add Reference dialog and scroll down until you see Microsoft.ServiceBus, select it, and then click OK.
-
Repeat the above step for System.Runtime.Serialization and System.ServiceModel.
-
In the Solution Explorer, right-click the References folder under the project folder and then click Add Reference….
-
In the Solution Explorer, double-click the Program.cs file to open it in the Visual Studio editor. Change the namespace name from its default name of
QueueSampletoMicrosoft.ServiceBus.Samples.namespace Microsoft.ServiceBus.Samples { … -
Add
usingstatements for theMicrosoft.ServiceBus,Microsoft.ServiceBus.Messaging,Microsoft.ServiceBus.Description,System.IO, andSystem.Datanamespaces.using Microsoft.ServiceBus; using Microsoft.ServiceBus.Messaging; using Microsoft.ServiceBus.Description; using System.Data; using System.IO;
-
Create a text file named Data.csv, and copy in the following comma-delimited text.
IssueID,IssueTitle,CustomerID,CategoryID,SupportPackage,Priority,Severity,Resolved 1,Package lost,1,1,Basic,5,1,FALSE 2,Package damaged,1,1,Basic,5,1,FALSE 3,Product defective,1,2,Premium,5,2,FALSE 4,Product damaged,2,2,Premium,5,2,FALSE 5,Package lost,2,2,Basic,5,2,TRUE 6,Package lost,3,2,Basic,5,2,FALSE 7,Package damaged,3,7,Premium,5,3,FALSE 8,Product defective,3,2,Premium,5,3,FALSE 9,Product damaged,4,6,Premium,5,3,TRUE 10,Package lost,4,8,Basic,5,3,FALSE 11,Package damaged,5,4,Basic,5,4,FALSE 12,Product defective,5,4,Basic,5,4,FALSE 13,Package lost,6,8,Basic,5,4,FALSE 14,Package damaged,6,7,Premium,5,5,FALSE 15,Product defective,6,2,Premium,5,5,FALSE
Save and close the Data.csv file, and remember the location to which you saved it.
-
In the Solution Explorer, right-click the name of your project (in this example, QueueSample), click Add, then click Existing Item.
-
Browse to the Data.csv file that you created in step 6. Click the file, then click Add. You may need to ensure that All Files (*.*) is selected in the file type dropdown.
To create a function that parses a list of messages
-
Before the
Main()method, declare two variables: one of type DataTable, to contain the list of messages in Data.csv. The other should be of type List object, strongly typed to BrokeredMessage. The latter is the list of brokered messages that subsequent steps in the tutorial will use.namespace Microsoft.ServiceBus.Samples { public class Program { private static DataTable issues; private static List<BrokeredMessage> MessageList;
-
Outside the
Main()method, define a ParseCSV() method that parses the list of messages in Data.csv and loads the messages into a DataTable table, as shown here. The method returns a DataTable object.static DataTable ParseCSVFile() { DataTable tableIssues = new DataTable("Issues"); string path = @"..\..\data.csv"; try { using (StreamReader readFile = new StreamReader(path)) { string line; string[] row; // create the columns line = readFile.ReadLine(); foreach (string columnTitle in line.Split(',')) { tableIssues.Columns.Add(columnTitle); } while ((line = readFile.ReadLine()) != null) { row = line.Split(','); tableIssues.Rows.Add(row); } } } catch (Exception e) { Console.WriteLine("Error:" + e.ToString()); } return tableIssues; }
-
In the
Main()method, add a statement that calls theParseCSVFile()method:public static void Main(string[] args) { // Populate test data issues = ParseCSVFile(); }
To create a function that loads the list of messages
-
Outside the
Main()method, define a GenerateMessages() method that takes the DataTable object returned by ParseCSVFile() and loads the table into a strongly-typed list of brokered messages. The method then returns the List object. For example:static List<BrokeredMessage> GenerateMessages(DataTable issues) { // Instantiate the brokered list object List<BrokeredMessage> result = new List<BrokeredMessage>(); // Iterate through the table and create a brokered message for each row foreach (DataRow item in issues.Rows) { BrokeredMessage message = new BrokeredMessage(); foreach (DataColumn property in issues.Columns) { message.Properties.Add(property.ColumnName, item[property]); } result.Add(message); } return result; }
-
In the
Main()method, directly below the call toParseCSVFile(), add a statement that calls theGenerateMessages()method with the return value fromParseCSVFile()as an argument:public static void Main(string[] args) { // Populate test data issues = ParseCSVFile(); MessageList = GenerateMessages(issues); }
To obtain user credentials
-
First, create three global string variables to hold these values. Declare these variables directly after the previous variable declarations, for example:
namespace Microsoft.ServiceBus.Samples { public class Program { private static DataTable issues; private static List<BrokeredMessage> MessageList; // add these variables private static string ServiceNamespace; private static string IssuerName; private static string IssuerKey; …
-
Next, create a function that accepts and stores the service namespace, issuer name, and issuer key. Add this method outside
Main(). For example:.static void CollectUserInput() { // User service namespace Console.Write("Please enter the service namespace to use: "); ServiceNamespace = Console.ReadLine(); // Issuer name Console.Write("Please enter the issuer name to use: "); IssuerName = Console.ReadLine(); // Issuer key Console.Write("Please enter the issuer key to use: "); IssuerKey = Console.ReadLine(); }
-
In the
Main()method, directly below the call toGenerateMessages(), add a statement that calls theCollectUserInput()method:public static void Main(string[] args) { // Populate test data issues = ParseCSVFile(); MessageList = GenerateMessages(issues); // Collect user input CollectUserInput(); }
Compiling the Code
-
From the Build menu in Visual Studio, select Build Solution or press F6 to confirm the accuracy of your work so far.