使用 .NET Core SDK 创建过滤的服务总线订阅 [英] Create filtered Service Bus subscription using .NET Core SDKs

查看:20
本文介绍了使用 .NET Core SDK 创建过滤的服务总线订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 .NET Core 2.1 应用程序内部使用 Azure 服务总线.我熟悉 Nuget 包 Microsoft.Azure.ServiceBus 附带的 SDK,我目前正在使用它编写主题并从那里接收消息.

I want to use Azure Service Bus from inside an .NET Core 2.1 application. I'm familiar with the SDK coming with the Nuget package Microsoft.Azure.ServiceBus and using this I'm currently writing to a topic and receiving messages from there.

我现在想要的是为每个客户创建关于这个主题的订阅.关键是我想使用 SqlFilter 创建一个过滤订阅.这意味着,我必须通过代码创建订阅.

What I want now is to create subscriptions on this topic for every client. The point is that I want to create a filtered subscription using the SqlFilter. This means, I have to create the subscription by code.

顺便说一句:据我所知,除此之外,在代码中执行此操作的唯一其他方法是使用 ARM 模板进行部署,因为门户不允许我创建过滤订阅.

BTW: As far as I've seen it the only other way then doing it in code is using ARM template for the deployment because the portal will not allow me to create a filtered subscription.

我知道,Microsoft.Azure.ServiceBus 无法创建资源,因此我转到了 Nuget 包 Microsoft.Azure.Management.ServiceBus.所以我现在可以像这样从代码创建一个新的订阅:

I understood, that Microsoft.Azure.ServiceBus is not able to create ressources and so I went to the Nuget package Microsoft.Azure.Management.ServiceBus. So I now can create a new subscription from code now like this:

private static async Task CreateSubscriptionAsync()
{
    var context = new AuthenticationContext($"https://login.microsoftonline.com/{Configuration["AppSettings:AzureManagement:TenantId"]}");
    var token = await context.AcquireTokenAsync(
        "https://management.core.windows.net/",
        new ClientCredential(Configuration["AppSettings:AzureManagement:ClientId"], Configuration["AppSettings:AzureManagement:ClientSecret"]));
    var creds = new TokenCredentials(token.AccessToken);
    using (var sbClient = new ServiceBusManagementClient(creds)
    {
        SubscriptionId = VariableHelper.Configuration["AppSettings:AzureManagement:AzureSubscriptionId"]
    })
    {                
        var queueParams = new SBSubscription
        {
            LockDuration = TimeSpan.FromSeconds(10)
        };          
        await sbClient.Subscriptions.CreateOrUpdateAsync(
            Configuration["AppSettings:ServiceBus:ResourceGroup"],
            Configuration["AppSettings:ServiceBus:Namespace"],
            Configuration["AppSettings:ServiceBus:TopicName"],
            "mysub",
            queueParams);                
    }
}

这有效,所以我有一个新订阅,我也可以删除它.但是现在我可以在哪里定义过滤器?SBSubscription 类型不包含定义过滤器的选项,并且 sbClient.Subscriptions.CreateOrUpdateAsync 中没有方法重载.

This works so I have a new subscription which I'm also able to delete. But now where can I define the filter? The SBSubscription-type contains no option to define a filter and there is no method-overload in sbClient.Subscriptions.CreateOrUpdateAsync.

我发现在代码中执行此操作的唯一方法是基于旧的 Nuget WindowsAzure.ServiceBus,它不适用于 .NET Core.所以我会像这样使用 NamespaceManager:

The only way I found to do this in code is based on the old Nuget WindowsAzure.ServiceBus which is not available for .NET Core. So there I would use a NamespaceManager like this:

var manager = NamespaceManager.CreateFromConnectionString("MyConnectionString");            
var rule = new RuleDescription("property > 4");
var sub = manager.CreateSubscriptionAsync(new SubscriptionDescription("mytopic", "mysub"), rule); 

如果没有这个功能,完整的主题对我来说似乎毫无用处,我无法相信这意味着 Azure 服务总线还没有为 .NET Core 做好准备.

Without this feature the complete topic-thing seems to be very useless to me and I can't believe that this means that Azure Service Bus is simply not ready for .NET Core.

Arunprabhu 建议的解决方案

我只是想完整地展示解决方案.

I just wanted to present the solution in complete.

  1. 添加对 NuGet Microsoft.Azure.Management.ServiceBus 的引用.
  2. 添加对 NuGet Microsoft.Azure.ServiceBus 的引用.
  3. 按如下方式创建子:

private static async Task CreateSubscriptionAsync()
{
    // this is to show that it works with any subscription-name
    var subName = Guid.NewGuid().ToString("N");     
    // create the subscription using Azure Management API
    var context = new AuthenticationContext($"https://login.microsoftonline.com/{Configuration["AppSettings:AzureManagement:TenantId"]}");
    var token = await context.AcquireTokenAsync(
        "https://management.core.windows.net/",
        new ClientCredential(Configuration["AppSettings:AzureManagement:ClientId"], Configuration["AppSettings:AzureManagement:ClientSecret"]));
    var creds = new TokenCredentials(token.AccessToken);
    using (var sbClient = new ServiceBusManagementClient(creds)
    {
        SubscriptionId = VariableHelper.Configuration["AppSettings:AzureManagement:AzureSubscriptionId"]
    })
    {                
        var queueParams = new SBSubscription
        {
            LockDuration = TimeSpan.FromSeconds(10)
        };          
        await sbClient.Subscriptions.CreateOrUpdateAsync(
            Configuration["AppSettings:ServiceBus:ResourceGroup"],
            Configuration["AppSettings:ServiceBus:Namespace"],
            Configuration["AppSettings:ServiceBus:TopicName"],
            subName,
            queueParams);                
    }
    // add filter-rule using Azure ServiceBus API
    var client = new SubscriptionClient(ServiceBusConnectionString, Configuration["AppSettings:ServiceBus:TopicName"], subName);
    await client.AddRuleAsync("default", new SqlFilter("1=1"));
    // That's it        
}

推荐答案

Microsoft.Azure.ServiceBus 中的 SubscriptionClient 下有类似的规则管理方法.在此处查看示例.

There are similar methods available for rules management under SubscriptionClient in Microsoft.Azure.ServiceBus. Look here for samples.

这篇关于使用 .NET Core SDK 创建过滤的服务总线订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆