我正在尝试使用 EventHub 和 Azure Functions 进行一些概念验证。我有一个 C# 通用 Webhook 函数,我想将消息传递到我的 EventHub。
我陷入了“集成”选项卡上给出的参数名称的困境。如果我在参数中声明该名称,则必须为其指定类型。我不知道是什么样的类型...我已经尝试过了:
- 字符串(其他函数将其与方向一起使用,不适用于 Webhooks)。
- 事件数据
- IEnumerable
我无法让它工作。如果我不这样做,我会收到错误消息: “缺少名为‘outputEventHubMessage’的绑定(bind)参数”
如果我给了错误的类型,我会收到消息: “索引方法‘Functions.GenericWebhookCSharp1’出错。Microsoft.Azure.WebJobs.Host:无法绑定(bind)到参数。”
我可能有点迷失在文档中,或者只是有点累,但我很感激这里的任何帮助!
/乔金
最佳答案
您可能只是错过了 out
您的参数上的关键字。下面是一个有效的 WebHook 函数,它声明了 out string message
映射到EventHub输出的参数,并通过message = "Test Message"
写入EventHub消息.
由于异步函数无法返回参数,因此我将此函数设为同步函数(返回 object
而不是 Task<object>
)。如果您想保持异步,请不要使用 out
参数,您可以改为绑定(bind)到 IAsyncCollector<string>
范围。然后,您可以通过调用 AddAsync
将一条或多条消息排入队列。收集器上的方法。
有关 EventHub 绑定(bind)及其支持的类型的更多详细信息,请参阅 here 。请注意,其他绑定(bind)遵循相同的一般模式。
#r "Newtonsoft.Json"
using System;
using System.Net;
using Newtonsoft.Json;
public static object Run(HttpRequestMessage req, out string message, TraceWriter log)
{
string jsonContent = req.Content.ReadAsStringAsync().Result;
dynamic data = JsonConvert.DeserializeObject(jsonContent);
log.Info($"Webhook was triggered! Name = {data.first}");
message = "Test Message";
var res = req.CreateResponse(HttpStatusCode.OK, new {
greeting = $"Hello {data.first} {data.last}!"
});
return res;
}
关于azure-eventhub - 在 Azure Functions 中创建 Azure EventHub 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504004/