Azure ServiceBus 在 Client.Receive() 上返回 null

发布于 2024-12-23 02:40:18 字数 4134 浏览 6 评论 0原文

我在从天蓝色中设置的队列接收消息时遇到问题。 我之前已经使用相同的代码成功完成了此操作,但现在当我尝试获取消息时,我只是得到 null。 当我在 azure 管理控制台中查看队列时,我清楚地看到该队列包含 5 条消息。

这是代码:

ServiceBus SB = new ServiceBus();
Microsoft.ServiceBus.Messaging.BrokeredMessage message;
while (true)
{
    message = SB.ReceiveMessage("orders");
    if (message == null)
    {
        break;
    }
    Procurement.Order order = message.GetBody<Procurement.Order>();
    order.id = Guid.NewGuid().ToString();
    order.remindercount = 0;

    using (DbManager db = new DbManager())
    {
        if (db.SetSpCommand("CreateOrderHead",
            db.Parameter("@companyId", order.companyId),
            db.Parameter("@orderId", order.orderId),
            db.Parameter("@suppliercode", order.suppliercode),
            db.Parameter("@supplierorderId", order.supplierorderId),
            db.Parameter("@orderdate", order.orderdate),
            db.Parameter("@desireddate", order.desireddate),
            db.Parameter("@ordertext", order.ordertext),
            db.Parameter("@name", order.name),
            db.Parameter("@street", order.street),
            db.Parameter("@zip", order.zip),
            db.Parameter("@city", order.city),
            db.Parameter("@country", order.country),
            db.Parameter("@countrycode", order.countrycode),
            db.Parameter("@deliveryterms", order.deliveryterms),
            db.Parameter("@reference", order.reference),
            db.Parameter("@deliveryinstruction", order.deliveryinstruction),
            db.Parameter("@id", order.id),
            db.Parameter("@partycode", order.partyCode)
            ).ExecuteNonQuery() == 1)
        {
            message.Complete();
            message = null;
        }

        db.SetSpCommand("DeleteOrderRows",
            db.Parameter("@orderid", order.orderId),
            db.Parameter("@companyId", order.companyId)
            ).ExecuteNonQuery();

        foreach (Procurement.Orderrow r in order.Orderrows)
        {
            db.SetSpCommand("CreateOrderRow",
            db.Parameter("@companyId", r.companyId),
            db.Parameter("@orderId", r.orderId),
            db.Parameter("@orderrowId", r.orderrowId),
            db.Parameter("@itemId", r.itemId),
            db.Parameter("@itemdesc", r.itemdesc),
            db.Parameter("@orderqty", r.orderqty),
            db.Parameter("@desireddate", r.desireddate),
            db.Parameter("@rowtext", r.rowtext),
            db.Parameter("@supplieritemId", r.supplieritemId),
            db.Parameter("@unit", r.unit),
            db.Parameter("@id", order.id),
            db.Parameter("@unitprice", r.unitprice),
            db.Parameter("@rowprice", r.rowprice)
            ).ExecuteNonQuery();
        }
    }
}
Thread.Sleep(new TimeSpan(0, 1, 0));

这是 ServiceBus 类:

public class ServiceBus
{
    TokenProvider TokenProvider;
    MessagingFactory Factory;

    public ServiceBus()
    {
        TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(GetIssuerName(), GetSecret());
        Factory = MessagingFactory.Create(
            GetURINameSpace(),
            TokenProvider
            );
    }

    public void SendMessage(string queue, BrokeredMessage message)
    {
        var client = Factory.CreateQueueClient(queue);
        client.Send(message);
    }

    public BrokeredMessage ReceiveMessage(string queue)
    {
        var client = Factory.CreateQueueClient(queue, ReceiveMode.ReceiveAndDelete);
        BrokeredMessage message = client.Receive();
        return message;
    }

    private static Uri GetURINameSpace()
    {
        return ServiceBusEnvironment.CreateServiceUri("sb", GetNamespace(), string.Empty);
    }

    private static string GetNamespace()
    {
        return "Namespace i have verified its the right one";
    }

    private static string GetIssuerName()
    {
        return "Issuer i have verified its the right one";
    }

    private static string GetSecret()
    {
        return "Key i have verified its the right one";
    }
}

我认为这应该非常简单,但我无法找出我做错了什么。 这可能是我缺少的一些小东西......

无论如何,提前感谢!

I have a problem with receiving messages from a queue i have set up in azure.
I have done this successfully using the same code before but now i just get null when i try to fetch messages.
When i view the queue in azure management console i clearly see that the queue contains 5 messages.

Here is the code:

ServiceBus SB = new ServiceBus();
Microsoft.ServiceBus.Messaging.BrokeredMessage message;
while (true)
{
    message = SB.ReceiveMessage("orders");
    if (message == null)
    {
        break;
    }
    Procurement.Order order = message.GetBody<Procurement.Order>();
    order.id = Guid.NewGuid().ToString();
    order.remindercount = 0;

    using (DbManager db = new DbManager())
    {
        if (db.SetSpCommand("CreateOrderHead",
            db.Parameter("@companyId", order.companyId),
            db.Parameter("@orderId", order.orderId),
            db.Parameter("@suppliercode", order.suppliercode),
            db.Parameter("@supplierorderId", order.supplierorderId),
            db.Parameter("@orderdate", order.orderdate),
            db.Parameter("@desireddate", order.desireddate),
            db.Parameter("@ordertext", order.ordertext),
            db.Parameter("@name", order.name),
            db.Parameter("@street", order.street),
            db.Parameter("@zip", order.zip),
            db.Parameter("@city", order.city),
            db.Parameter("@country", order.country),
            db.Parameter("@countrycode", order.countrycode),
            db.Parameter("@deliveryterms", order.deliveryterms),
            db.Parameter("@reference", order.reference),
            db.Parameter("@deliveryinstruction", order.deliveryinstruction),
            db.Parameter("@id", order.id),
            db.Parameter("@partycode", order.partyCode)
            ).ExecuteNonQuery() == 1)
        {
            message.Complete();
            message = null;
        }

        db.SetSpCommand("DeleteOrderRows",
            db.Parameter("@orderid", order.orderId),
            db.Parameter("@companyId", order.companyId)
            ).ExecuteNonQuery();

        foreach (Procurement.Orderrow r in order.Orderrows)
        {
            db.SetSpCommand("CreateOrderRow",
            db.Parameter("@companyId", r.companyId),
            db.Parameter("@orderId", r.orderId),
            db.Parameter("@orderrowId", r.orderrowId),
            db.Parameter("@itemId", r.itemId),
            db.Parameter("@itemdesc", r.itemdesc),
            db.Parameter("@orderqty", r.orderqty),
            db.Parameter("@desireddate", r.desireddate),
            db.Parameter("@rowtext", r.rowtext),
            db.Parameter("@supplieritemId", r.supplieritemId),
            db.Parameter("@unit", r.unit),
            db.Parameter("@id", order.id),
            db.Parameter("@unitprice", r.unitprice),
            db.Parameter("@rowprice", r.rowprice)
            ).ExecuteNonQuery();
        }
    }
}
Thread.Sleep(new TimeSpan(0, 1, 0));

And this is the ServiceBus-class:

public class ServiceBus
{
    TokenProvider TokenProvider;
    MessagingFactory Factory;

    public ServiceBus()
    {
        TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(GetIssuerName(), GetSecret());
        Factory = MessagingFactory.Create(
            GetURINameSpace(),
            TokenProvider
            );
    }

    public void SendMessage(string queue, BrokeredMessage message)
    {
        var client = Factory.CreateQueueClient(queue);
        client.Send(message);
    }

    public BrokeredMessage ReceiveMessage(string queue)
    {
        var client = Factory.CreateQueueClient(queue, ReceiveMode.ReceiveAndDelete);
        BrokeredMessage message = client.Receive();
        return message;
    }

    private static Uri GetURINameSpace()
    {
        return ServiceBusEnvironment.CreateServiceUri("sb", GetNamespace(), string.Empty);
    }

    private static string GetNamespace()
    {
        return "Namespace i have verified its the right one";
    }

    private static string GetIssuerName()
    {
        return "Issuer i have verified its the right one";
    }

    private static string GetSecret()
    {
        return "Key i have verified its the right one";
    }
}

I think this should be pretty straight forward but i cant find out what im doing wrong.
Its probably something small that im missing...

Anyways, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-12-30 02:40:18

您在 SubcriptionDescription.MessageCount 中看到的那些 BrokeredMessage 不仅是常规消息,而且还是 $DeadLetterQueue-sub 队列中的消息计数!

使用此代码片段检索该子队列中的所有消息并打印出其详细信息。将 [主题] 和 [订阅] 重命名为您实际的:

MessagingFactory msgFactory = MessagingFactory.Create(_uri, _tokenProvider);
        MessageReceiver msgReceiver = msgFactory.CreateMessageReceiver("[topic]/subscriptions/[subscription]/$DeadLetterQueue", ReceiveMode.PeekLock);

        while (true)
        {
            BrokeredMessage msg = msgReceiver.Receive();

            if (msg != null)
            {
                Console.WriteLine("Deadlettered message.");

                Console.WriteLine("MessageId:                  {0}", msg.MessageId);
                Console.WriteLine("DeliveryCount:              {0}", msg.DeliveryCount);
                Console.WriteLine("EnqueuedTimeUtc:            {0}", msg.EnqueuedTimeUtc);
                Console.WriteLine("Size:                       {0} bytes", msg.Size);
                Console.WriteLine("DeadLetterReason:           {0}",
                    msg.Properties["DeadLetterReason"]);
                Console.WriteLine("DeadLetterErrorDescription: {0}",
                    msg.Properties["DeadLetterErrorDescription"]);
                Console.WriteLine();
                msg.Complete();
            }
        }

Those BrokeredMessages you see in your SubcriptionDescription.MessageCount are not just regular messages but also the count of the messages in the $DeadLetterQueue-sub queue!!!

Use this code snippet to retrieve all messages from that sub-queue and print out their details. Rename [topic] and [subscription] to your actual ones:

MessagingFactory msgFactory = MessagingFactory.Create(_uri, _tokenProvider);
        MessageReceiver msgReceiver = msgFactory.CreateMessageReceiver("[topic]/subscriptions/[subscription]/$DeadLetterQueue", ReceiveMode.PeekLock);

        while (true)
        {
            BrokeredMessage msg = msgReceiver.Receive();

            if (msg != null)
            {
                Console.WriteLine("Deadlettered message.");

                Console.WriteLine("MessageId:                  {0}", msg.MessageId);
                Console.WriteLine("DeliveryCount:              {0}", msg.DeliveryCount);
                Console.WriteLine("EnqueuedTimeUtc:            {0}", msg.EnqueuedTimeUtc);
                Console.WriteLine("Size:                       {0} bytes", msg.Size);
                Console.WriteLine("DeadLetterReason:           {0}",
                    msg.Properties["DeadLetterReason"]);
                Console.WriteLine("DeadLetterErrorDescription: {0}",
                    msg.Properties["DeadLetterErrorDescription"]);
                Console.WriteLine();
                msg.Complete();
            }
        }
冰雪梦之恋 2024-12-30 02:40:18

此问题的解决方案要么是天蓝色管理门户中的错误,使其显示队列上错误的消息数量,要么消息以某种方式被标记,以便它们不会被读取。
换句话说,它一直有效,我只需向队列添加一些新消息即可。

The solution to this problem was either a bug in azure management-portal making it show the wrong number of messages on the queue or the messages somehow got flagged so that they would not be read.
In other words it worked all along, i just had to add some new messages to the queue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文