代码之家  ›  专栏  ›  技术社区  ›  Alexander Schmidt VPP

使用.NET Core sdk创建筛选的服务总线订阅

  •  4
  • Alexander Schmidt VPP  · 技术社区  · 7 年前

    我想从.NET Core 2.1应用程序内部使用Azure服务总线。我熟悉Nuget包附带的SDK Microsoft.Azure.ServiceBus 利用这个我正在写一个主题并从那里接收信息。

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

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

    我明白了 Microsoft.Azure.ServiceBus服务总线 无法创建资源,因此我转到了Nuget包 Microsoft.Azure.Management.ServiceBus . 所以我现在可以从代码创建一个新订阅,如下所示:

    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 .

    我在代码中找到的唯一方法是基于旧的Nuget WindowsAzure.ServiceBus 它不适用于.NET Core。所以我会用一个 NamespaceManager 这样地:

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

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

    编辑:Arunprabhu建议的解决方案

    我只想完整地提出解决方案。

    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        
    }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Arunprabhu    7 年前

    在Microsoft.Azure.ServiceBus的SubscriptionClient下也有类似的方法可用于规则管理。看 here 对于样品。

        2
  •  5
  •   Sean Feldman    7 年前

    我知道,Microsoft.Azure.ServiceBus无法创建资源

    从3.1.0版起-预览 Microsoft.Azure.ServiceBus 可以使用 ManagementClient 不需要Microsoft.Azure.Management.ServiceBus库。它有超负荷的 CreateSubscriptionAsync() 那就接受了 RuleDescription 作为默认规则创建。或者,提供 规则描述 将自动为您设置默认规则。