【问题标题】:Setting Azure Function Service Bus Topic and Subscription Output Binding via C#通过 C# 设置 Azure Function Service Bus 主题和订阅输出绑定
【发布时间】:2020-05-30 18:18:39
【问题描述】:

我有一个带有多个服务总线输出绑定的简单 HTTP 触发器 Azure 函数。所有绑定都指向同一个主题,但它们有不同的订阅。如果我要通过 function.json 设置这个函数应用程序,那就很简单了:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "serviceBus",
      "connection": "SERVICEBUS",
      "name": "output",
      "topicName": "outtopic",
      "subscriptionName": "sub",
      "direction": "out"
    },
    {
      "type": "serviceBus",
      "connection": "SERVICEBUS",
      "name": "output",
      "topicName": "outtopic",
      "subscriptionName": "sub2",
      "direction": "out"
    }
  ],
  "disabled": false
}

但我通过 Visual Studio 发布我的函数,因此我的 Azure Functions 在门户中是只读的,并且 function.json 在发布时由 VS 自动生成。 问题是我无法弄清楚如何设置指向不同订阅的多个输出绑定。目前我有这样的事情:

[FunctionName("Function2")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    [ServiceBus("outtopic", entityType:EntityType.Topic)] IAsyncCollector<string> output,
    [ServiceBus("outtopic", entityType: EntityType.Topic)] IAsyncCollector<string> output2,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

    await output.AddAsync(requestBody);

    return new OkObjectResult("OK");
}

你可以看到 output 和 output2 指向同一个主题,但是没有指定订阅的选项。 在这一点上,我非常有信心这还没有实施。但我希望可能有一个解决方法?

【问题讨论】:

    标签: azure azure-functions azureservicebus


    【解决方案1】:

    不可能将消息直接放入主题订阅中,而是每条消息都必须通过一个主题。

    为确保只有特定订阅接收消息,您需要配置主题订阅规则。您可以在blog post here 中阅读有关规则的更多信息。

    【讨论】:

      【解决方案2】:

      试试这个,按照这个例子在定义中添加 Connection 属性 - 如果订阅是指在 azure 订阅上:

      public static void Run([BlobTrigger("inputvideo/{customername}/{date}/{filename}", Connection = "AzureWebJobsStorage")]Stream myBlob, 
                                      string customername, 
                                      string date, 
                                      string filename,
                                      [ServiceBus("detectobjectsqueue",EntityType.Queue, Connection="ServiceBusConnectionString")] IAsyncCollector<string> output,
                                      ILogger log)
      

      更新 根据您的评论,我理解订阅是指主题订阅。在这种情况下,主题的想法是所有订阅都接收消息。所以你有一个发布者,任何订阅主题的人都会收到消息。如果您想确保特定订阅者收到消息,请在接收端点上实施消息过滤(例如,按类型)或为每个订阅者使用专用队列。

      此外,从概念上讲,发布者不应该知道谁是订阅者,订阅者也不应该知道谁是发布者。如果您知道谁是订阅者,为什么不使用 REST 调用,例如接收端点?

      【讨论】:

      • 抱歉,我不确定这应该如何提供帮助? Connection 属性指向特定的 ServiceBus,无法将其指向主题的订阅。
      • Connection 属性是连接字符串,例如来自您的 local.settings.json 文件。
      • 如果您添加来自不同订阅的服务总线连接字符串,它应该可以工作。只需添加多个输出,如示例中所示,连接指向不同的订阅。我不明白为什么这不能回答问题,或者我误解了你的问题
      • 对不起,我是从您的评论中得知的。您的意思是主题订阅而不是天蓝色订阅。这应该
      • 感谢您的澄清。我从你的解释中得到了正确的想法。我想要指定主题的主要原因是避免将消息放入主题中以便每个 AF 对其做出反应(要花钱)。我看到我从错误的角度处理这个问题,并且发送消息的 AF 不应该知道订阅者。我可以在订阅上设置过滤器,并确保 AF InputTriggers 与订阅一起设置。
      猜你喜欢
      • 1970-01-01
      • 2022-10-08
      • 2021-07-19
      • 2022-12-27
      • 1970-01-01
      • 2018-08-27
      • 2022-08-17
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多