Exportera (0) Skriv ut
Visa allt
EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

Step 1: Introduction and Prerequsites

Updated: March 16, 2015

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 service namespace, and to obtain a shared access signature (SAS) key. A service namespace provides an application boundary for each application exposed through Service Bus. A SAS key is automatically generated by the system when a service namespace is created. The combination of service namespace and SAS key provides a credential with which Service Bus authenticates access to an application.

  1. To create a service namespace, follow the steps outlined in How To: Create or Modify a Service Bus Service Namespace.

  2. In the main window of the Azure Management Portal, click the name of the service namespace you created in the previous step.

  3. Click Configure.

  4. In the shared access signature generator section, make a note of the primary key associated with the RootManagerSharedAccessKey policy, or copy it to the clipboard. You will use this value later in this tutorial.

The next step is to create a Visual Studio project and write two helper functions that load a comma-delimited list of messages into a strongly-typed (BrokeredMessage)  List object.

  1. Open Visual Studio as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.

  2. 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.

  3. Use the NuGet package manager to add the Service Bus libraries to your project:

    1. In the Solution Explorer, right-click the project folder and click Manage NuGet Packages.

    2. In the Manage Nuget Packages dialog, search online for WindowsAzure.ServiceBus and click Install

  4. In 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 QueueSample to Microsoft.ServiceBus.Samples.

    namespace Microsoft.ServiceBus.Samples
    {
        …
    
  5. Modify the using statements as shown in the following code snippet.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Threading;
    using Microsoft.ServiceBus.Messaging;
    
    
    
    
    
  6. 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.

  7. In the Solution Explorer, right-click the name of your project (in this example, QueueSample), click Add, then click Existing Item.

  8. 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.

  1. 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;
    
    
    
  2. 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;
    }
    
  3. In the Main() method, add a statement that calls the ParseCSVFile() method:

    public static void Main(string[] args)
    {
    
        // Populate test data
        issues = ParseCSVFile();
    
    }
    

  1. 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;
    }
    
  2. In the Main() method, directly below the call to ParseCSVFile(), add a statement that calls the GenerateMessages() method with the return value from ParseCSVFile() as an argument:

    public static void Main(string[] args)
    {
    
        // Populate test data
        issues = ParseCSVFile();
        MessageList = GenerateMessages(issues);
    }
    

  1. 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 sasKeyName = "RootManageSharedAccessKey";
            private static string sasKeyValue;
            …
    
  2. Next, create a function that accepts and stores the service namespace and SAS 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 key
        Console.Write("Please enter the SAS key to use: ");
        sasKeyValue = Console.ReadLine();
    }
    
  3. In the Main() method, directly below the call to GenerateMessages(), add a statement that calls the CollectUserInput() 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.

Visa:
© 2015 Microsoft