gpt4 book ai didi

azure - 如何让Azure Event Grid触发Azure Function?

转载 作者:行者123 更新时间:2023-12-04 23:14:38 26 4
gpt4 key购买 nike

我应该使用哪种触发器类型来运行 Azure Function 作为 Azure 事件网格主题的订阅?

与事件网格相关的各处都提到了此功能,但我没有看到任何教程或代码示例。

最佳答案

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Newtonsoft.Json;

namespace FunctionApp3
{
public static class Function2
{

[FunctionName("Function2")]
public static void Run([EventGridTrigger()]EventGridEvent eventGridEvent, TraceWriter log)
{
log.Info($"EventGridEvent\n\tId:{eventGridEvent.Id}\n\tTopic:{eventGridEvent.Topic}\n\tSubject:{eventGridEvent.Subject}\n\tType:{eventGridEvent.EventType}\n\tData:{JsonConvert.SerializeObject(eventGridEvent.Data)}");

}
}
}

使用 azure 门户:

运行.cs:

#r "Microsoft.Azure.WebJobs.Extensions.EventGrid"
#r "Newtonsoft.Json"

using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Newtonsoft.Json;

public static void Run(EventGridEvent eventGridEvent, TraceWriter log)
{
//log.Info(eventGridEvent.ToString());

var jsondata = JsonConvert.SerializeObject(eventGridEvent.Data);
var tmp = new { make = "", model = "", test = ""};
var data = JsonConvert.DeserializeAnonymousType(jsondata, tmp);

log.Info($"Data = make:{data.make}, model:{data.model}, test:{data.test}");
log.Info($"EventGridEvent\n\tId:{eventGridEvent.Id}\n\tTopic:{eventGridEvent.Topic}\n\tSubject:{eventGridEvent.Subject}\n\tType:{eventGridEvent.EventType}\n\tData:{jsondata}");
}

函数.json:

   {
"bindings": [
{
"type": "eventGridTrigger",
"name": "eventGridEvent",
"direction": "in"
}
],
"disabled": false
}

测试样本:

{
"Topic": null,
"Subject": "/myapp/vehicles/motorcycles",
"Id": "b68529f3-68cd-4744-baa4-3c0498ec19e2",
"EventType": "recordInserted",
"EventTime": "2017-06-26T18:41:00.9584103Z",
"Data":{
"make": "Ducati",
"model": "Monster",
"test":"-----------"
}
}

最后一步是在集成页面中创建事件网格订阅 URL:

enter image description here

关于azure - 如何让Azure Event Grid触发Azure Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46589263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com