从Azure ServiceBus中的订阅/主题中读取现有消息

时间:2019-01-08 09:27:30

标签: .net-core azureservicebus

我正在尝试使用Microsoft.Azure.ServiceBus.dll(在.Net Core 2.1中)读取Azure ServiceBus订阅上的所有现有消息,但仍在努力。

我发现了许多下面的示例应该起作用的示例,但事实并非如此:

var client = new SubscriptionClient(ServiceBusConnectionString, topicName, subscription, ReceiveMode.PeekLock, null);
var totalRetrieved = 0;

while (totalRetrieved < count)
{
    var messageEnumerable = subscriptionClient.PeekBatch(count);

    //// ... code removed from this example as not relevant
}

我的问题是.PeekBatch方法不可用,我对如何处理此方法感到困惑。

我已经从GitHub(https://github.com/paolosalvatori/ServiceBusExplorer)下载了ServiceBusExplorer的源代码,上面的代码示例已经完成了。但是不在.Net Core / Microsoft.Azure.ServiceBus命名空间中。

不过,为了清楚起见,我试图读取队列中已经存在的消息-我已经通过其他示例创建了响应新消息的侦听器,但是在消息之后,我需要以这种断开连接的方式进行工作已经放在队列中。

2 个答案:

答案 0 :(得分:0)

ServiceBusExplorer使用WindowsAzure.ServiceBus库,它是一个.Net Framework库,您不能在.Net Core应用程序中使用它。您应该在.Net Core应用程序中使用Microsoft.Azure.ServiceBus(.Net标准库)。

检查here中是否有Microsoft.Azure.ServiceBus示例

答案 1 :(得分:0)

var client = new SubscriptionClient(ServiceBusConnectionString, topicName, subscription, ReceiveMode.PeekLock, null);
client .RegisterMessageHandler(
    async (message, token) =>
    {   
        await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
    }
    );

尝试使用RegisterMessageHandler。它会 从实体连续接收消息。它注册一个消息处理程序并 开始一个新线程以接收消息。等待此处理程序 每次接收方收到新消息时。