提问人:James 提问时间:11/17/2023 最后编辑:James 更新时间:11/17/2023 访问量:54
将响应标头和/或自定义消息属性添加到独立的 Azure 函数响应
Adding response headers and/or custom message properties to isolated azure function response
问:
我有一个类似于下面的函数签名,我希望包含 HTTP 响应标头。但是,我不确定它的可行性,因为输出涉及将另一个自定义类型放置在不同的主题上,而不是基于 HTTP 的。如果在此上下文中无法添加 HTTP 响应标头,那么将消息属性与消息正文 (MyResult) 一起包含的方法是什么?
[Function(FunctionName)]
[ServiceBusOutput(OutputTopicName, Connection = "TriggerConnectionString")]
public async Task<MyResult?> RunAsync(
[ServiceBusTrigger("%TriggerSource%", TriggerSubscriptionName, Connection =
"TriggerConnectionString")]
MyEvent? myEvent, CancellationToken cancellationToken)
{
}
我期待/希望有这样的事情:
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
var messageProperties = context.GetMessageProperties();
messageProperties.Add("new property, "value");
}
答:
0赞
Sampath
11/17/2023
#1
我遵循了 Azure Functions 的 Azure 服务总线输出绑定的 MSDOC
- 以下示例可以在下面将消息发送到 Azure 服务总线队列。
var logger = context.GetLogger<ServiceBusOutputFunction>();
try
{
// Send a new message to the output queue
var outputMessage = new ServiceBusMessage("Hello, Azure Service Bus! This is an output message.");
// Add custom properties to the message
outputMessage.ApplicationProperties.Add("CustomProperty1", "Value1");
outputMessage.ApplicationProperties.Add("CustomProperty2", 42);
await SendOutputMessage(outputMessage,context);
return "Message processed successfully.";
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while processing the message.");
throw;
}
}
private static async Task SendOutputMessage(ServiceBusMessage message, FunctionContext context)
{
var logger = context.GetLogger<ServiceBusOutputFunction>();
string connectionString = " ServicebusConnectionString ";
string outputQueueName = "ServiceBusQueueName"; // Replace with your output queue name
// Create a Service Bus sender for the output queue
await using (ServiceBusSender sender = new ServiceBusClient(connectionString).CreateSender(outputQueueName))
{
// Send the message to the output queue
await sender.SendMessageAsync(message);
logger.LogInformation($"Message sent to {outputQueueName}.");
}
}
输出:
评论