1

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

4

3 回答 3

3

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

在此处输入图像描述

#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(默认架构)
  • CloudEventV01Schema
  • CustomEventSchema(仍在预览中)

可以找到更多详细信息:

于 2018-09-16T00:02:27.547 回答
2

这是通过 Visual Studio 代码连接到 Azure 函数中的 Azure 事件网格主题的方法。注意:我不必添加任何 function.json 文件。

由于主题就像一个带有 URL 的 API,因此创建一个指向事件数据网格主题 URL 的 HTTP 客户端,将主题的访问密钥添加到 HTTP 客户端标头。

HttpClient client = new HttpClient() { 
   BaseAddress = new Uri(<YOUR_TOPIC_URL>) 
};
client.DefaultRequestHeaders.Add("aeg-sas-key", "<YOUR_TOPIC_KEY>");

var egevent = new
{
   Id = 1234,
   Subject = "SendEmail",
   EventType = "SendEmail",
   EventTime = DateTime.UtcNow,
   Data = <YOUR_MESSAGE>(Could be anything and object or data literals)
};
var x = await client.PostAsJsonAsync("", new[] { egevent });
log.LogInformation(x.ReasonPhrase);

希望这会有所帮助。

于 2019-01-31T00:08:52.773 回答
2

您可以使用Azure Event Grid Nuget 包轻松完成此操作:

var failedEvent = new EventGridEvent()
        {
            Id = Guid.NewGuid().ToString(),
            Subject = "create-tenant-failed",
            EventType = "create-tenant-failed",
            EventTime = DateTime.UtcNow,
            Data = reason,
            DataVersion = "1.0"
        };

        var topicHostname = new Uri(FailedEventTopicUrl).Host;
        var topicCredentials = new TopicCredentials("my-access-token-from-azure-portal");
        var client = new EventGridClient(topicCredentials);

        await client.PublishEventsAsync(topicHostname, new List<EventGridEvent>() { failedEvent });
于 2020-08-14T13:39:30.477 回答