简体   繁体   English

获取 azure function 中的队列名称或主题+订阅者名称(Azure 服务总线触发函数)

[英]Get Queue name or Topic+Subscriber name in azure function (Azure Service Bus triggered functions)

Is it possible to get the queue name in a service bus triggered Azure Function?是否可以在服务总线触发 Azure Function 中获取队列名称?

I know it is possible if the QueueName or TopicName+SubscriberName are hard coded.我知道如果 QueueName 或 TopicName+SubscriberName 是硬编码的,这是可能的。 In that case I can make a const string and use it inside the function.在这种情况下,我可以创建一个 const 字符串并在 function 中使用它。 However my service bus trigger gets the name from the settings using "%ServiceBusSettings:QueueName%" , "%ServiceBusSettings:TopicName%" and "%ServiceBusSettings:SubscriberName%" .但是,我的服务总线触发器使用"%ServiceBusSettings:QueueName%""%ServiceBusSettings:TopicName%""%ServiceBusSettings:SubscriberName%"从设置中获取名称。

How to get the Queue or Topic+Subscriber name in this configurable case?在这种可配置的情况下,如何获取队列或主题+订阅者名称?

[FunctionName("topic-and-subscriber-function")]
public async Task Run(
    [ServiceBusTrigger("%ServiceBusSettings:TopicName%", "%ServiceBusSettings:SubscriptionName%", Connection = "ServiceBusSettings:ServiceBusConnection")] Message message,
    ILogger log, MessageReceiver messageReceiver)
{
    // Get TopicName and SubscriberName
}

[FunctionName("queue-function")]
public async Task Run(
    [ServiceBusTrigger("%ServiceBusSettings:QueueName%", Connection = "ServiceBusSettings:ServiceBusConnection")] Message message,
    ILogger log, MessageReceiver messageReceiver)
{
    // Get QueueName
}

What you need to get is the name of the queue, which is a property of QueueClient, but Microsoft.Azure.ServiceBus.QueueClient itself cannot be serialized.你需要得到的是队列的名字,它是QueueClient的一个属性,但是Microsoft.Azure.ServiceBus.QueueClient本身是不能序列化的。 The Microsoft.Azure.ServiceBus.Message can be serialized but does not have a queuename attribute. Microsoft.Azure.ServiceBus.Message 可以序列化,但没有 queuename 属性。 (And Microsoft.Azure.ServiceBus.Message has no parent class. This means that it is generally impossible to obtain queuename through this class.) (并且Microsoft.Azure.ServiceBus.Message没有父class。这意味着一般无法通过这个class获取queuename。)

You can pass in queuename as part of the message, such as input json format message:您可以将 queuename 作为消息的一部分传入,例如输入 json 格式的消息:

{
   "queuename":"testname",
   ...
}

then you can get the queuename in code.(Also You can get this value in bindings by this way. All key values passed in in json format can be obtained in the binding.)然后就可以在code中获取queuename了。(也可以通过这种方式在bindings中获取这个值。所有以json格式传入的key值都可以在binding中获取。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM