如何从消息队列中检索我存储在azure服务总线中的对象

时间:2017-06-07 07:10:01

标签: azure azureservicebus

我将对象存储在azure服务总线中,现在我想要检索该对象及其值。这是我用来在azure服务总线中存储对象的代码。

class Program
    {
      static void Main(string[] args)
       {
foreach(string file in Directory.EnumerateFiles(@"C:\Users\shubham.mittal\Desktop\xml", "*.xml")) 
            {
                string contents = File.ReadAllText(file);
                XDocument doc = XDocument.Parse(contents);
                string locationcode = doc.Root.Element("Customer").Element("Location_Code").Value;
                string Phone_Number = doc.Root.Element("Customer").Element("Phone_Number").Value;
                var connectionString = "Endpoint=sb://namespacename.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=sharedaccesskey";
                var queueName = "shubhamqueue";

                var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
                data d=new data();
                d.location_code=locationcode;
                d.Phone_Number = Phone_Number;
                var message1 = new BrokeredMessage(d);
                client.Send(message1);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我们可以使用Brokered​Message.​Get​Body<​T>获取BrokerMessage信息。因此,在您的情况下,我创建了一个演示,它可以在我这边工作。

1.创建一个C#控制台项目。

2。Reference the Microsoft Azure Service Bus SDK

3.添加公共类

public class Data

{
    public string LocationCode { get; set; }
    public string PhoneNumber { get; set; }
}

4。将以下代码添加到Program.cs

中的main函数中
  var client =
  QueueClient.CreateFromConnectionString("servicebus connection string", queueName);
  Data data = new Data
  {
      LocationCode =  "test location code",
      PhoneNumber = "test phone number"
  };
  var sendMessage = new BrokeredMessage(data);

  client.Send(sendMessage);

  client.OnMessage(message =>
  {
     var dataInfo = message.GetBody<Data>();
     Console.WriteLine($"location: {dataInfo.LocationCode}, phone number: {dataInfo.PhoneNumber}");
  });

5.测试代码。

enter image description here