[英]C# Azure Service Bus Queue OnMessage Callback
I'm writing some functionality that responds to an Azure Service Bus Queue. 我正在编写一些响应Azure Service Bus队列的功能。 This currently polls on a specified queue and OnMessage triggers a callback to a method in the original class that calls it:
当前,这会在指定的队列上进行轮询,并且OnMessage会触发对调用它的原始类中的方法的回调:
partial class Class1
{
private void BeginProcessing()
{
serviceBusHelper.Listen(QueueType.Inbound, HandleTransaction);
}
private bool HandleTransaction(BrokeredMessage message)
{
...
}
}
And then the service bus helper class: 然后是服务总线帮助程序类:
public class ServiceBusHelper : IServiceBusHelper
{
ManualResetEvent CompletedResetEvent = new ManualResetEvent(false);
public void Listen(QueueType queue, Action<BrokeredMessage> callback)
{
switch (queue)
{
case QueueType.Inbound:
inboundClient.OnMessage(message =>
{
try
{
callback(message);
}
catch (Exception ex)
{
...
}
CompletedResetEvent.WaitOne();
});
break;
...
}
}
It is connecting correctly to the azure service bus queue and retrieving the message, however the callback never seems to actually trigger. 它已正确连接到azure服务总线队列并检索该消息,但是该回调似乎从未真正触发过。 What I'm trying to achieve is a service that will continuously respond to an OnMessage event, trigger a new worker (from within class1) despite the OnMessage actually being triggered from the ServiceBusHelper class.
我试图实现的服务将持续响应OnMessage事件,尽管实际上是从ServiceBusHelper类触发了OnMessage,但仍会触发新的worker(从class1内部)。
the callback never seems to actually trigger.
回调似乎从未真正触发过。
In order to reproduce the issue, I do a test in a console application with the following code on my side, which works fine. 为了重现该问题,我在控制台应用程序中进行了测试,并附带了以下代码,效果很好。
public static void processmessage()
{
string connectionString = "{connectionstring here}";
var queueName = "{queue name}";
var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
var options = new OnMessageOptions();
options.AutoComplete = false;
try
{
client.OnMessage(message =>
{
HandleTransaction(message);
}, options);
}
catch (Exception)
{
}
}
private static void HandleTransaction(BrokeredMessage message)
{
Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>()));
}
If possible, you can try to modify private bool HandleTransaction(BrokeredMessage message)
to make it return void private void HandleTransaction(BrokeredMessage message)
, and check whether it works fine. 如果可能,您可以尝试修改
private bool HandleTransaction(BrokeredMessage message)
以使其返回void private void HandleTransaction(BrokeredMessage message)
,然后检查其是否正常运行。
So I've managed to get this working by modifying to the asynchronous OnMessage counterpart. 因此,我设法通过修改异步OnMessage对应项来使其工作。 I believe the root cause was related to not specifing OnMessageOptions to the OnMessage, or the CompletedResetEvent not interacting how I expected.
我相信根本原因与未将OnMessageOptions指定给OnMessage有关,或者与CompletedResetEvent未按我的预期进行交互有关。
public void Listen(QueueType queue, Action<BrokeredMessage> callback)
{
OnMessageOptions options = new OnMessageOptions
{
MaxConcurrentCalls = maxConcurrent,
AutoComplete = false
};
switch (queue)
{
case QueueType.Inbound:
inboundClient.OnMessageAsync(async message =>
{
bool shouldAbandon = false;
try
{
callback(message);
// complete if successful processing
await message.CompleteAsync();
}
catch (Exception ex)
{
shouldAbandon = true;
Debug.WriteLine(ex);
}
if (shouldAbandon)
{
await m.AbandonAsync();
}
}, options);
break;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.