0

I need to get data from service bus from my console application, instead of my data I've got System.UnauthorizedAccessException error 401 I've got 2 static readonly string that I don't know how to use You may require one or both of the services below depending on the level of detail that you need-

    //sample usage string briefingDetailsByIdURI = string.Format(Constants.BRIEFING_DETAILS_ID_URI, brfId);

    public static readonly string ID_URI = "https://trtrtrtr.servicebus.windows.net/trtrtr/trttrttr/{0}";



    //sample usage - string URI = string.Format(Constants.sdgsdgg, sgsgsg, sgsgsg);

    public static readonly string DETAIL_ID_URI = "https://trtrtrtr.servicebus.windows.net/trtrtr/trrtrttr/{0}/{1}";

I just went in app.config and put this with the right namespace and password

<appSettings>
<!-- Service Bus specific app setings for messaging connections -->
<add key="Microsoft.ServiceBus.ConnectionString"        value="Endpoint=sb://<namespace>.servicebus.windows.net/;
 SharedAccessKeyName=Root      stManageSharedAccessKey;SharedAccessKey=<paasword> />

after that I went in my console program.cs

Console.Title = "Receiver2";

        // Creating the topic if it does not exist already using the service bus connection string stored in the app.config file
        string connectionString =
            CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
        //appSettings dans appsettings getSettings()

        //connection au service bus
        var namespaceManager =
            NamespaceManager.CreateFromConnectionString(connectionString);
        //verification  si queue existe

        if (!namespaceManager.QueueExists("CommentaireQueue"))
        {
            namespaceManager.CreateQueue("CommentaireQueue");
        }


        QueueClient client = QueueClient.Create("CommentaireQueue");
        Console.WriteLine("test console");
        //boucle infini pour recevoir tous les messages
        while (true)
        {
            var message = client.Receive();
            if (message != null)
            {
                var comm = message.GetBody<string>();
                string myString = comm.Contenu;
                try
                {


                        Console.WriteLine(myString);

                }
                finally
                {
                    //enlever le message de la queue
                    message.Complete();
                }
            }
        }

    }
Sharp
  • 121
  • 1
  • 2
  • 16

1 Answers1

0

This is what I use to connect to the queue

QueueClient _client;
        var connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
        var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
        if (!namespaceManager.QueueExists(QueueName))
        {
            namespaceManager.CreateQueue(QueueName);
        }

        _client = QueueClient.CreateFromConnectionString(connectionString, QueueName);

Instead of running a while loop. You could be using OnMessage() like this:

_client.OnMessage(message =>
                {
                    try
                    {
                        //do logic
                    }
                    catch (Exception e)
                    {
                        message.Abandon();
                        throw new Exception(e.Message);
                    }
                });

Make sure the connections string is set correct in your Azure ServiceConfiguration:

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="App" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2015-04.2.6">
  <Role name="App">
    <Instances count="1" />
    <ConfigurationSettings>
      <Setting name="Microsoft.ServiceBus.ConnectionString" value="THE CONNECTION STRING" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>
Jeffrey Rosselle
  • 745
  • 1
  • 9
  • 25
  • the problem is creating the queue namespace.createQueue throws an unauthorizedException – Sharp Feb 08 '16 at 15:20
  • I can see that you're using the CloudConfigurationManager too. Make sure that the setting "Microsoft.ServiceBus.ConnectionString" is in your ServiceConfigration.Cloud (and local) of your Azure Project. I updated my answer with the service configuration – Jeffrey Rosselle Feb 08 '16 at 15:23
  • thank you Jeffrey, but isn't what I've done in the app.config – Sharp Feb 08 '16 at 15:36
  • it's a console application the only way I configure this in my opinion is app.config and i've done it – Sharp Feb 08 '16 at 15:53
  • I understand, unfortunatly I cannot seem to find anything wrong at this moment. Have you tried looking at following posts, the error looks simular: http://stackoverflow.com/questions/18032506/how-to-create-queue-in-windows-azure and http://stackoverflow.com/questions/18558299/servicebus-throws-401-unauthorized-error – Jeffrey Rosselle Feb 08 '16 at 16:00
  • http://stackoverflow.com/questions/35292136/how-do-i-re-use-a-token-service-bus and what do you think about this? – Sharp Feb 09 '16 at 12:34