我如何得到蔚蓝的队列消息(服务总线)的数据 [英] how do i get data from azure queue message (service bus)

查看:170
本文介绍了我如何得到蔚蓝的队列消息(服务总线)的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的控制台应用程序从服务总线的数据,而不是我的数据我得System.UnauthorizedAccessException的错误401
我有2静态只读字符串,我不知道怎么用
您可能需要服务的一个或两个以下根据详细程度,你需要 -

  //样品用法字符串briefingDetailsByIdURI =的String.Format(Constants.BRIEFING_DETAILS_ID_URI,brfId);    公共静态只读字符串ID_URI =htt​​ps://trtrtrtr.servicebus.windows.net/trtrtr/trttrttr/{0};    //样品的使用 - 字符串URI =的String.Format(Constants.sdgsdgg,sgsgsg,sgsgsg);    公共静态只读字符串DETAIL_ID_URI =htt​​ps://trtrtrtr.servicebus.windows.net/trtrtr/trrtrttr/{0}/{1};

我刚进去的app.config并把这个用正确的命名空间和密码

 <&的appSettings GT;
<! - 服务总线特定应用setings的信息连接 - >
<添加键=Microsoft.ServiceBus.ConnectionStringVALUE =端点=保安局://<&空间GT; .servicebus.windows.net /;
 SharedAccessKeyName =根stManageSharedAccessKey; SharedAccessKey =< paasword> />

之后,我在我的控制台Program.cs中去了。

Console.Title =Receiver2;

  //创建话题,如果已经不使用存储在app.config文件服务总线连接字符串存在,
        字符串的connectionString =
            CloudConfigurationManager.GetSetting(Microsoft.ServiceBus.ConnectionString);
        //丹斯的appSettings的AppSettings的getSettings()        //连接坳服务总线
        VAR namespaceManager =
            NamespaceManager.CreateFromConnectionString(的connectionString);
        //验证SI队列existe        如果(!namespaceManager.QueueExists(CommentaireQueue))
        {
            namespaceManager.CreateQueue(CommentaireQueue);
        }
        QueueClient客户= QueueClient.Create(CommentaireQueue);
        Console.WriteLine(试验台);
        //仿羔皮呢的Infini倒recevoir TOUS LES消息
        而(真)
        {
            VAR消息= client.Receive();
            如果(消息!= NULL)
            {
                VAR COMM = message.GetBody<串GT;();
                字符串的myString = comm.Contenu;
                尝试
                {
                        Console.WriteLine(myString的);                }
                最后
                {
                    // enlever勒消息德拉队列
                    message.Complete();
                }
            }
        }    }


解决方案

这是我用来连接到队列

  QueueClient _client;
        变种的connectionString = CloudConfigurationManager.GetSetting(Microsoft.ServiceBus.ConnectionString);
        VAR namespaceManager = NamespaceManager.CreateFromConnectionString(的connectionString);
        如果(!namespaceManager.QueueExists(QUEUENAME))
        {
            namespaceManager.CreateQueue(QUEUENAME);
        }        _client = QueueClient.CreateFromConnectionString(的connectionString,QUEUENAME);

而不是运行while循环。你可以使用的onMessage()是这样的:

  _client.OnMessage(消息= GT;
                {
                    尝试
                    {
                        //做逻辑
                    }
                    赶上(例外五)
                    {
                        message.Abandon();
                        抛出新的异常(e.Message);
                    }
                });

请确保连接串是您的Azure ServiceConfiguration设置正确的:

 <?XML版本=1.0编码=UTF-8&GT?;
&所述; ServiceConfiguration服务名=应用程序的xmlns =htt​​p://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfigurationosFamily =4OSVERSION =*schemaVersion =2015-04.2.6>
  <角色名称=应用程序>
    <实例数=1/>
    < ConfigurationSettings>
      <设定名=Microsoft.ServiceBus.ConnectionString值=连接字符串/>
    < / ConfigurationSettings>
  < /角色>
< / ServiceConfiguration>

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

    }

解决方案

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>

这篇关于我如何得到蔚蓝的队列消息(服务总线)的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆