Microsoft provided a simple test application which provides a WebHook URL. You get it from: https://docs.microsoft.com/en-us/samples/azure-samples/azure-event-grid-viewer/azure-event-grid-viewer
Install Nuget package:
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
As functions should share client instances as much as possible, this could be implemented as follows:
public static readonly Lazy<EventGridClient> EventGridClient = new Lazy<EventGridClient>(() => new EventGridClient(new TopicCredentials(Configurations.EventGridTopicKey)));
public static readonly Lazy<string> EventGridTopicHostname = new Lazy<string>(() => new Uri(Configurations.EventGridTopicEndpoint).Host);
public static async Task PublishEvent(EventGridEvent @event, ILogger log)
{
var events = new[] { @event };
try
{
await EventGridClient.Value.PublishEventsAsync(EventGridTopicHostname.Value, events);
}
catch (Exception e)
{
log.LogError(e, "Publish events to EventGrid failed");
}
}
Functions can then dispatch events in a very simple way:
var @event = new EventGridEvent()
{
Id = Guid.NewGuid().ToString(),
EventType = "Namespace.EventType",
Data = results,
EventTime = DateTime.UtcNow,
Subject = $"Sample Event",
DataVersion = "1.0"
};
await FunctionHelper.PublishEvent(@event, log);