diff --git a/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj b/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj
index 5781b49..1bb4cac 100644
--- a/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj
+++ b/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj
@@ -16,7 +16,6 @@ You can specify all necessary event details you need.
icon-logo.png
-
diff --git a/AzureFunctions.EventGrid/EventGridAsyncCollector.cs b/AzureFunctions.EventGrid/EventGridAsyncCollector.cs
index 046e8e9..6ad167b 100644
--- a/AzureFunctions.EventGrid/EventGridAsyncCollector.cs
+++ b/AzureFunctions.EventGrid/EventGridAsyncCollector.cs
@@ -1,29 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Net.Http;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
-using Microsoft.Azure.EventGrid;
-using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.WebJobs;
+using Newtonsoft.Json;
namespace AzureFunctions.EventGrid
{
public class EventGridAsyncCollector : IAsyncCollector
{
- private readonly string topicHostname;
-
+ private readonly string topicEndPoint;
+ private readonly string topicKey;
+
private readonly IList eventCollection;
- private readonly TopicCredentials topicCredentials;
+ private readonly EventGridAttribute attribute;
+
+ private static HttpClient httpClient;
+
public EventGridAsyncCollector(EventGridAttribute attribute)
{
this.eventCollection = new List();
- string topicEndpoint = attribute.TopicEndpoint;
- string topicKey = attribute.TopicKey;
+ this.topicEndPoint = attribute.TopicEndpoint;
+ this.topicKey = attribute.TopicKey;
+ this.attribute = attribute;
- this.topicHostname = new Uri(topicEndpoint).Host;
- this.topicCredentials = new TopicCredentials(topicKey);
}
///
@@ -34,6 +38,11 @@ public EventGridAsyncCollector(EventGridAttribute attribute)
/// .
public Task AddAsync(Event item, CancellationToken cancellationToken = new CancellationToken())
{
+ if (httpClient == null)
+ {
+ httpClient = attribute.HttpClient ?? new HttpClient();
+ }
+
this.eventCollection.Add(item);
return Task.CompletedTask;
@@ -48,22 +57,19 @@ public EventGridAsyncCollector(EventGridAttribute attribute)
var eventGridEventCollection = CreateEventGridEventCollection();
if (eventGridEventCollection.Any())
{
- using (var eventGridClient = new EventGridClient(topicCredentials))
- {
- await eventGridClient.PublishEventsAsync(
- topicHostname,
- eventGridEventCollection,
- cancellationToken);
- }
+ HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(eventGridEventCollection), Encoding.UTF8, "application/json");
+ httpContent.Headers.Add("aeg-sas-key", topicKey);
+ await httpClient.PostAsync(topicEndPoint, httpContent, cancellationToken);
}
}
- private IList CreateEventGridEventCollection()
+ private IList CreateEventGridEventCollection()
{
- var eventGridEventCollection = new List();
+ var eventGridEventCollection = new List();
foreach (var @event in eventCollection)
{
- var eventGridEvent = new EventGridEvent(
+
+ var eventGridEvent = new EventGridModel(
id: Guid.NewGuid().ToString("N"),
subject: @event.Subject,
dataVersion: @event.DataVersion,
diff --git a/AzureFunctions.EventGrid/EventGridAttribute.cs b/AzureFunctions.EventGrid/EventGridAttribute.cs
index 0336283..169a244 100644
--- a/AzureFunctions.EventGrid/EventGridAttribute.cs
+++ b/AzureFunctions.EventGrid/EventGridAttribute.cs
@@ -1,4 +1,5 @@
using System;
+using System.Net.Http;
using Microsoft.Azure.WebJobs.Description;
namespace AzureFunctions.EventGrid
@@ -20,5 +21,10 @@ public class EventGridAttribute : Attribute
///
[AppSetting]
public string TopicKey { get; set; }
+
+ ///
+ /// If you have already instantiated an , pass it to this property.
+ ///
+ public HttpClient HttpClient { get; set; }
}
}
diff --git a/AzureFunctions.EventGrid/EventGridModel.cs b/AzureFunctions.EventGrid/EventGridModel.cs
new file mode 100644
index 0000000..928ae93
--- /dev/null
+++ b/AzureFunctions.EventGrid/EventGridModel.cs
@@ -0,0 +1,27 @@
+using System;
+
+namespace AzureFunctions.EventGrid
+{
+ internal class EventGridModel :Event
+ {
+ //
+ // Summary:
+ // Gets or sets an unique identifier for the event.
+ public string Id { get; set; }
+
+ //
+ // Summary:
+ // Gets or sets the time (in UTC) the event was generated.
+ public DateTime EventTime { get; set; }
+
+ public EventGridModel(string id,string subject, string dataVersion, string eventType, object data,DateTime eventTime)
+ {
+ this.Id = id;
+ this.Subject = subject;
+ this.DataVersion = dataVersion;
+ this.EventType = eventType;
+ this.Data = data;
+ this.EventTime = eventTime;
+ }
+ }
+}
diff --git a/README.md b/README.md
index f798f07..103a0d2 100644
--- a/README.md
+++ b/README.md
@@ -65,4 +65,15 @@ private class MyCustomEvent
```
The publishing of the events will be executed after the Azure Function is finished,
-in the `FlushAsync` method of the `IAsyncCollector`.
\ No newline at end of file
+in the `FlushAsync` method of the `IAsyncCollector`.
+
+In order for this to work you need a `local.settings.json` file with the following values. For `EventGridBindingSampleTopicEndpoint` setting, you have to provide the endpoint along with the [api-version](https://docs.microsoft.com/en-us/rest/api/eventgrid/dataplane/publishevents/publishevents) header.
+
+```
+{
+ "Values": {
+ "EventGridBindingSampleTopicEndpoint": "https://{topicHostname}/api/events?api-version=2018-01-01",
+ "EventGridBindingSampleTopicKey": "{Topic Key}"
+ }
+}
+```
\ No newline at end of file