通过C设置Azure函数服务总线主题和订阅输出绑定#
我有一个简单的HTTP触发器Azure函数,具有多个服务总线输出绑定。所有绑定都指向同一主题,但它们有不同的订阅。如果我要通过function.json设置此函数应用程序,它非常简单:通过C设置Azure函数服务总线主题和订阅输出绑定#,azure,azure-functions,azureservicebus,Azure,Azure Functions,Azureservicebus,我有一个简单的HTTP触发器Azure函数,具有多个服务总线输出绑定。所有绑定都指向同一主题,但它们有不同的订阅。如果我要通过function.json设置此函数应用程序,它非常简单: { "bindings": [ { "authLevel": "function", "name": "req", "type": "httpTrigger", "direction": "in", "methods": [
{
"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函数在门户中是只读的,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");
}
[FunctionName(“Function2”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[ServiceBus(“outtopic”,entityType:entityType.Topic)]IAsyncCollector输出,
[ServiceBus(“outtopic”,entityType:entityType.Topic)]IAsyncCollector输出2,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
wait output.AddAsync(requestBody);
返回新的OkObjectResult(“OK”);
}
正如您所看到的,output和output2指向同一主题,但没有指定订阅的选项。
在这一点上,我很有信心,这一点尚未得到实施。但我希望可能有一个解决方法?试试这个,在定义中添加连接属性,根据这个例子-如果订阅是指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)
publicstaticvoidrun([BlobTrigger(“inputvideo/{customername}/{date}/{filename}”,Connection=“azurewebjobstorage”)]streammyblob,
字符串客户名称,
字符串日期,
字符串文件名,
[ServiceBus(“detectobjectsqueue”,EntityType.Queue,Connection=“ServiceBusConnectionString”)]IAsyncCollector输出,
ILogger日志)
更新
根据你的评论,我理解订阅是指主题订阅。在这种情况下,topic的思想是所有订阅都接收消息。因此,您有一个发布者,任何订阅该主题的人都将收到该消息。如果要确保特定订阅者接收消息,请在接收端点上实施消息筛选(例如,按类型),或者对每个订阅者使用专用队列
此外,从概念上讲,发布者不应该知道谁是订阅者,订阅者也不应该知道谁是发布者。如果您知道谁是订户,为什么不使用REST呼叫(例如对接收端点的呼叫) 尝试此操作,根据此示例,在定义中添加连接属性-如果您所说的订阅是指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)
publicstaticvoidrun([BlobTrigger(“inputvideo/{customername}/{date}/{filename}”,Connection=“azurewebjobstorage”)]streammyblob,
字符串客户名称,
字符串日期,
字符串文件名,
[ServiceBus(“detectobjectsqueue”,EntityType.Queue,Connection=“ServiceBusConnectionString”)]IAsyncCollector输出,
ILogger日志)
更新
根据你的评论,我理解订阅是指主题订阅。在这种情况下,topic的思想是所有订阅都接收消息。因此,您有一个发布者,任何订阅该主题的人都将收到该消息。如果要确保特定订阅者接收消息,请在接收端点上实施消息筛选(例如,按类型),或者对每个订阅者使用专用队列
此外,从概念上讲,发布者不应该知道谁是订阅者,订阅者也不应该知道谁是发布者。如果您知道谁是订户,为什么不使用REST呼叫(例如对接收端点的呼叫) 不可能直接将消息放入主题订阅,而是每条消息都必须通过一个主题
要确保只有特定订阅接收到消息,您需要配置主题订阅规则。您可以在中阅读有关规则的更多信息。不可能直接将邮件放入主题订阅,而是每条邮件都必须通过主题发送
要确保只有特定订阅接收到消息,您需要配置主题订阅规则。您可以在中阅读更多有关规则的信息。对不起,我不确定这有什么帮助?Connection属性指向特定的ServiceBus,无法将其指向某个主题的订阅。Connection属性是连接字符串,例如来自local.settings.json文件。如果您添加来自不同订阅的service bus连接字符串,它应该可以工作。只需添加多个输出,如示例中所示,连接指向不同的订阅。我不明白这为什么不能回答这些问题,或者我误解了你的提问,我是从你的评论中得到的。你是说主题订阅而不是azure订阅。这应该感谢您的澄清。我从你的解释中得到了正确的想法。我想要指定主题的主要原因是为了避免每个AF都会对主题做出反应而将消息放入主题中(需要花钱)。我看到我从错误的角度来处理这个问题,发送消息的AF不应该知道订阅者的情况。我可以在订阅上设置过滤器,并确保AF InputRiggers已设置订阅。对不起,我不确定这有什么帮助?连接属性指向特定的ServiceBus,无法指向它