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

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

问题描述

我想从.NET Core 2.1应用程序内部使用Azure Service Bus.我对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 Service Bus根本无法为.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. 按如下方式创建子项:
  1. Add reference to NuGet Microsoft.Azure.Management.ServiceBus.
  2. Add reference to NuGet Microsoft.Azure.ServiceBus.
  3. Create the sub as follows:

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天全站免登陆