【问题标题】:How to make Azure Function triggered by Azure Event Grid?如何让 Azure 事件网格触发 Azure Function?
【发布时间】:2018-03-17 06:54:09
【问题描述】:

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

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

【问题讨论】:

    标签: azure azure-functions azure-eventgrid


    【解决方案1】:

    可以为此目的使用通用 Webhook 触发器。

    这是一个示例函数。

    function.json:

    {
      "bindings": [
        {
          "type": "httpTrigger",
          "direction": "in",
          "webHookType": "genericJson",
          "name": "req"
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ],
      "disabled": false
    }
    

    C# 实现:

    #r "Newtonsoft.Json"
    
    using System;
    using System.Net;
    using Newtonsoft.Json;
    
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    {
        string jsonContent = await req.Content.ReadAsStringAsync();
        var events = JsonConvert.DeserializeObject<GridEvent[]>(jsonContent);
    
        if (req.Headers.GetValues("Aeg-Event-Type").First() == "SubscriptionValidation")
        {
            var code = events[0].Data["validationCode"];
            return req.CreateResponse(HttpStatusCode.OK, new { validationResponse = code });
        }
    
        // Do whatever you need with events    
        foreach (var e in events)
            log.Info(e.Id);
    
        return req.CreateResponse(HttpStatusCode.OK);
    }
    
    public class GridEvent
    {
        public string Id { get; set; }
        public string EventType { get; set; }
        public string Subject { get; set; }
        public DateTime EventTime { get; set; }
        public Dictionary<string,string> Data { get; set; }
        public string Topic { get; set; }
    }
    

    注意两件重要的事情:

    • 自定义 GridEvent 类将事件 JSON 解析为 POCO
    • if 负责端点验证的块(事件网格要求)

    【讨论】:

      【解决方案2】:
      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)}");
      
            }
         }
      }
      

      使用天蓝色门户:

      运行.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}");   
      }
      

      function.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:

      【讨论】:

      • 谢谢! “秘密”是在门户中的Scenario 下拉列表中选择Experimental :)
      • 没错。 VS2017 15.3.5设计的AF项目中怎么配置EventGrid订阅Url没搞清楚。
      • 我猜你必须手动配置它,例如事件网格门户页面?
      • 我在门户中查找事件网格订阅 URL 时遇到同样的问题,我的应用程序被标记为只读我猜是因为 C# 应用程序。有什么想法吗?
      • 在门户/Function_app_settings 页面上将函数应用的编辑模式更改为读/写。然后,转到用于创建 EventGrid 订阅 URL(您的事件订阅)的功能/集成页面。之后,您可以将编辑模式恢复为只读。
      猜你喜欢
      • 2021-06-01
      • 2018-11-27
      • 2021-08-14
      • 2021-10-21
      • 2021-11-27
      • 2021-02-18
      • 2019-10-19
      • 1970-01-01
      • 2020-02-27
      相关资源
      最近更新 更多