azure - 如何在 Azure Function 中写入 Azure 事件网格主题

标签 azure azure-functions azure-eventhub azure-eventgrid

我注意到,主题突然不再作为服务总线的一部分提供。是否可以将消息写入 Azure 函数内的事件网格主题?

最佳答案

以下代码片段是使用 azure 函数将遥测流推送到事件模型的示例:

enter image description here

#r "Microsoft.ServiceBus"
#r "Newtonsoft.Json"


using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;

// reusable client proxy
static HttpClient client = HttpClientHelper.Client(ConfigurationManager.AppSettings["TopicEndpointEventGrid"], ConfigurationManager.AppSettings["aeg-sas-key"]);

// AF
public static async Task Run(EventData ed, TraceWriter log)
{
    log.Info($"C# Event Hub trigger function processed a message:{ed.SequenceNumber}"); 
    //foreach(var prop in ed.SystemProperties)
    //   log.Info($"{prop.Key} = {prop.Value}");

    // fire EventGrid Custom Topic
    var egevent = new 
    {
        Id = ed.SequenceNumber.ToString(),
        Subject = $"/iothub/events/{ed.SystemProperties["iothub-message-source"] ?? "?"}/{ed.SystemProperties["iothub-connection-device-id"] ?? "?"}",
        EventType = "telemetryDataInserted",
        EventTime = ed.EnqueuedTimeUtc,
        Data = new
        {
            sysproperties = ed.SystemProperties,
            properties = ed.Properties,
            body = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(ed.GetBytes()))
        }
    };
    await client.PostAsJsonAsync("", new[] { egevent });  
}

// helper
class HttpClientHelper
{
    public static HttpClient Client(string address, string key)
    {      
        var client = new HttpClient() { BaseAddress = new Uri(address) };
        client.DefaultRequestHeaders.Add("aeg-sas-key", key);
        return client;      
    }
}

函数.json:

    {
      "bindings": [
       {
         "type": "eventHubTrigger",
         "name": "ed",
         "direction": "in",
         "path": "myIoTHubName",
         "connection": "connectionstringIOTHUB",
         "consumerGroup": "eventing",
         "cardinality": "many"
        }
      ],
      "disabled": false
    }

请注意,AEG 自定义主题的负载取决于其inputSchema 属性。基本上,当前的 AEG 版本(还包括预览版)允许从以下选择中声明输入模式:

  • EventGridSchema(默认架构)
  • CloudEventV01架构
  • CustomEventSchema(仍在预览版中)

更多详情请参见:

关于azure - 如何在 Azure Function 中写入 Azure 事件网格主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52348802/

相关文章:

node.js - 保持 Node 应用程序在azure应用程序服务上运行

node.js - Azure Functions : Nodejs, 使用文件系统时有哪些限制/限制?

c# - Azure 函数忽略的functions.json graphToken 绑定(bind)

azure - 目前是否可以从 Service Fabric 群集诊断扩展将 ETW 流式传输到事件中心?

azure - 未找到事件 - Azure 事件中心

azure - 代表授权用户从浏览器中的客户端应用程序调用 azure 函数

azure - 在 Kusto - Azure 数据资源管理器中生成唯一字符串

c# - SlowCheetah 未预览部署配置文件上的转换

azure - Azure函数是否可以监听多个blob以进行blob触发器

c# - 升级 ‘Azure.Messaging.EventHubs’ 到 5.6.2 构建时返回 'EventHubs' 命名空间中不存在 'Microsoft.Azure' 错误