Dışarıya aktar (0) Yazdır
Tümünü Genişlet
EN
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.

Step 2: Create Management Credentials

Updated: August 18, 2014

This is the second step in the Service Bus messaging features tutorial. In this step, you define the management operations you will use to create secure (shared secret) credentials with which your application will be authorized.

  1. For clarity, this tutorial places all the queue operations in a separate method. Create a Queue() method in the Program class, below the Main() method. For example:

    public static void Main(string[] args)
    {
    …}
    static void Queue()
    {
    }
    
    
  2. The next step is to create a shared secret credential using a TokenProvider object. The creation method takes the issuer name and key that was obtained in the CollectUserInput() method. Add the following code to the Queue() method:

    static void Queue()
    {
        // Create management credentials
        TokenProvider credentials = TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
    }
    
    

  1. Create a new service namespace management object, with a URI containing the service namespace name and the management credentials obtained in the last step, as arguments. Add this code directly beneath the code added in the previous step:

    NamespaceManager namespaceClient = new NamespaceManager(ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty), credentials);
    

Example

At this point, your code should look similar to the following:

namespace Microsoft.ServiceBus.Samples
{
    public class Program
    {

        private static DataTable issues;
        private static List<BrokeredMessage> MessageList;
        private static string ServiceNamespace;
        private static string IssuerName;
        private static string IssuerKey;

        public static void Main(string[] args)
        {
            // Collect user input
            CollectUserInput();

            // Populate test data
            issues = ParseCSVFile();
            MessageList = GenerateMessages(issues);

        }

        static void Queue()
        {

            // Create management credentials
                TokenProvider credentials = TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
            // Create namespace client
            NamespaceManager namespaceClient = new NamespaceManager(ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty), credentials);
        }

        static void CollectUserInput()
        {
            // User service namespace
            Console.Write("Please provide the service namespace to use: ");
            ServiceNamespace = Console.ReadLine();

            // Issuer name
            Console.Write("Please provide the issuer name to use: ");
            IssuerName = Console.ReadLine();

            // Issuer key
            Console.Write("Please provide the issuer key to use: ");
            IssuerKey = Console.ReadLine();
        }


       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;
        }

        static DataTable ParseCSVFile()
        {
            DataTable tableIssues = new DataTable("Issues");
            string path = @"..\..\data.csv";
            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);
                }
            }

        return tableIssues;
    }
}
}

In the next step, you create the queue to which you will send messages.

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.

Show:
© 2014 Microsoft