azure - .Net Core Azure服务总线获取当前消息计数

标签 azure asp.net-core azureservicebus

从队列获取计数的旧方法是这样的:

 CloudQueue q = queueClient.GetQueueReference(QUEUE_NAME);
            q.FetchAttributes();
            qCnt = q.ApproximateMessageCount;

这不再适用于带有 AMQP 1.0 的 .netCore

我没有找到获取消息计数的好方法。关于我缺少什么有什么想法吗?

最佳答案

有一个 Microsoft.Azure.Management.ServiceBus Libary,它是一个预览版本,100% 兼容.Netcore。我们可以获得更多detail在这里。

准备工作:

Registry Azure Active Directory application and assign Role

步骤:

创建一个.net core控制台项目并添加以下代码。

 var tenantId = "tenantid";
            var context = new AuthenticationContext($"https://login.windows.net/{tenantId}");
            var clientId = "Client";
            var clientSecret = "Secret";
            var subscriptionId = "subscriptionId";
            var result =  context.AcquireTokenAsync(
                "https://management.core.windows.net/",
                new ClientCredential(clientId, clientSecret)).Result;

            var creds = new TokenCredentials(result.AccessToken);
            var sbClient = new ServiceBusManagementClient(creds)
            {
                SubscriptionId = subscriptionId
            };
            var queueParams = new QueueCreateOrUpdateParameters()
            {
                Location = "East Asia",
                EnablePartitioning = true
            };
           
            var queue = sbClient.Queues.ListAll("groupname", "namespace").ToList().FirstOrDefault(x => x.Name.Equals("queuename")); 
            var messagecount = queue.MessageCount;

enter image description here

从 Azure 门户,我们检查队列中的消息

enter image description here

Project.json 文件:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.Azure.Management.ServiceBus": "0.2.0-preview",
    "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.9",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

关于azure - .Net Core Azure服务总线获取当前消息计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900813/

相关文章:

Azure 服务总线 ACS 颁发者和 key 丢失

asp.net-mvc - ajax POST 长请求超时问题

c# - 将数据库恢复到服务器时出错 : . NET Core 不应使用文件支持的模型

postgresql - 从数据工厂读取 Postgress(以 SQL_ASCII 编码)

c# - 更新到 .net core 2.2 后信号 r 不工作

c# - 脚手架标识时的 VS 错误消息

c# - 在适用于 .NET 的 Azure 管理库中创建具有多个声明的 Azure 服务总线策略

asp.net-mvc-2 - Windows Azure "Sandbox"

azure - 如何将 appsettings.json 设置包含到 Azure DevOps 构建和发布管道中

c# - 在 WCF Azure 服务总线中动态分配 json 响应内容类型