Last active
July 6, 2022 07:22
-
-
Save subhankars/4fc66584a9d00d8da85afdab6083925b to your computer and use it in GitHub Desktop.
Microsoft Azure EventGrid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace EventGridEventTrigger.Library | |
{ | |
public static class CustomEvent<T> | |
{ | |
public static EventGridEvent CreateCustomEvent(T obj) | |
{ | |
return new EventGridEvent() | |
{ | |
Id = Guid.NewGuid().ToString(), | |
EventTime = DateTime.UtcNow, | |
EventType = "MyCustomEventType", | |
Subject = "MyCustomEventSubject", | |
Data = obj | |
}; | |
} | |
} | |
public class CustomData | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public double Height { get; set; } | |
public double Weight { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env | |
WORKDIR /app | |
# Copy csproj and restore as distinct layers | |
COPY *.csproj ./ | |
RUN dotnet restore | |
# Copy everything else and build | |
COPY . ./ | |
RUN dotnet publish -c Release -o out | |
# Build runtime image | |
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 | |
WORKDIR /app | |
COPY --from=build-env /app/out . | |
ENTRYPOINT ["dotnet", "first-docker.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"id": "2d1781af-3a4c-4d7c-bd0c-e34b19da4e66", | |
"topic": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", | |
"subject": "MyCustomEventSubject", | |
"data": { | |
"name" : "John Doe", | |
"age" : 28, | |
"weight" : 72, | |
"height" : 178 | |
}, | |
"eventType": "MyCustomEventType", | |
"eventTime": "2018-07-15T22:12:19.4556811Z", | |
"metadataVersion": "1", | |
"dataVersion": "1" | |
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace EventGridEventTrigger.Library | |
{ | |
public class EventGridEvent | |
{ | |
public string Id { get; set; } | |
public string EventType { get; set; } | |
public string Subject { get; set; } | |
public DateTime EventTime { get; set; } | |
public object Data { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using EventGridEventTrigger.Library; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace EventGridEventFunctionApp | |
{ | |
public static class EventGridEventHandler | |
{ | |
[FunctionName("EventGridEventHandler")] | |
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage request, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
var requestMessageContent = await request.Content.ReadAsStringAsync(); | |
var eventGridEvent = JsonConvert.DeserializeObject<EventGridEvent[]>(requestMessageContent) | |
.FirstOrDefault(); | |
var data = eventGridEvent.Data as JObject; | |
// Validate whether EventType is of "Microsoft.EventGrid.SubscriptionValidationEvent" | |
if (string.Equals(eventGridEvent.EventType, Constants.SubscriptionValidationEvent, StringComparison.OrdinalIgnoreCase)) | |
{ | |
var eventData = data.ToObject<SubscriptionValidationEventData>(); | |
var responseData = new SubscriptionValidationResponseData | |
{ | |
ValidationResponse = eventData.ValidationCode | |
}; | |
if (responseData.ValidationResponse != null) | |
{ | |
return request.CreateResponse(HttpStatusCode.OK, responseData); | |
} | |
} | |
else | |
{ | |
// Handle your custom event | |
var eventData = data.ToObject<CustomData>(); | |
var customEvent = CustomEvent<CustomData>.CreateCustomEvent(eventData); | |
return request.CreateResponse(HttpStatusCode.OK, customEvent); | |
} | |
return request.CreateResponse(HttpStatusCode.OK); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Azure.EventGrid.Models; | |
using Microsoft.Azure.WebJobs.Extensions.EventGrid; | |
using EventGridEventTrigger.Library; | |
namespace EventGridEventTrigger.AzureFunctionAppV2 | |
{ | |
public static class EventGridEventHandler | |
{ | |
[FunctionName("EventGridEventHandler")] | |
public static void Run([EventGridTrigger] Microsoft.Azure.EventGrid.Models.EventGridEvent eventGridEvent, TraceWriter log) | |
{ | |
log.Info(eventGridEvent.Data.ToString()); | |
var eventData = eventGridEvent.Data as CustomData; | |
var customEvent = CustomEvent<CustomData>.CreateCustomEvent(eventData); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EventGridEventTrigger.Library; | |
using Microsoft.AspNetCore.Mvc; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.Linq; | |
using System.Net.Http; | |
namespace EventGridEventTrigger.DotNetCoreAPIApp.Controllers | |
{ | |
[Produces("application/json")] | |
public class EventGridEventHandlerController : Controller | |
{ | |
[HttpPost] | |
[Route("api/EventGridEventHandler")] | |
public JObject Post([FromBody]object request) | |
{ | |
//Deserializing the request | |
var eventGridEvent = JsonConvert.DeserializeObject<EventGridEvent[]>(request.ToString()) | |
.FirstOrDefault(); | |
var data = eventGridEvent.Data as JObject; | |
// Validate whether EventType is of "Microsoft.EventGrid.SubscriptionValidationEvent" | |
if (string.Equals(eventGridEvent.EventType, Constants.SubscriptionValidationEvent, StringComparison.OrdinalIgnoreCase)) | |
{ | |
var eventData = data.ToObject<SubscriptionValidationEventData>(); | |
var responseData = new SubscriptionValidationResponseData | |
{ | |
ValidationResponse = eventData.ValidationCode | |
}; | |
if (responseData.ValidationResponse != null) | |
{ | |
return JObject.FromObject(responseData); | |
} | |
} | |
else | |
{ | |
// Handle your custom event | |
var eventData = data.ToObject<CustomData>(); | |
var customEvent = CustomEvent<CustomData>.CreateCustomEvent(eventData); | |
return JObject.FromObject(customEvent); | |
} | |
return new JObject(new HttpResponseMessage(System.Net.HttpStatusCode.OK)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EventGridEventTrigger.Library; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
namespace EventGridEventTriggerNetFrameworkAPIApp.Controllers | |
{ | |
public class EventGridEventHandlerController : ApiController | |
{ | |
[HttpPost] | |
[AcceptVerbs("POST")] | |
[Route("api/EventGridEventHandler")] | |
public async Task<HttpResponseMessage> Post(HttpRequestMessage request) | |
{ | |
var requestMessageContent = await request.Content.ReadAsStringAsync(); | |
var eventGridEvent = JsonConvert.DeserializeObject<EventGridEvent[]>(requestMessageContent) | |
.FirstOrDefault(); | |
var data = eventGridEvent.Data as JObject; | |
// Validate whether EventType is of "Microsoft.EventGrid.SubscriptionValidationEvent" | |
if (string.Equals(eventGridEvent.EventType, Constants.SubscriptionValidationEvent, StringComparison.OrdinalIgnoreCase)) | |
{ | |
var eventData = data.ToObject<SubscriptionValidationEventData>(); | |
var responseData = new SubscriptionValidationResponseData | |
{ | |
ValidationResponse = eventData.ValidationCode | |
}; | |
if (responseData.ValidationResponse != null) | |
{ | |
return request.CreateResponse(HttpStatusCode.OK, responseData); | |
} | |
} | |
else | |
{ | |
// Handle your custom event | |
var eventData = data.ToObject<CustomData>(); | |
var customEvent = CustomEvent<CustomData>.CreateCustomEvent(eventData); | |
return request.CreateResponse(HttpStatusCode.OK, customEvent); | |
} | |
return request.CreateResponse(HttpStatusCode.OK); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"topic": string, | |
"subject": string, | |
"id": string, | |
"eventType": string, | |
"eventTime": string, | |
"data":{ | |
object-unique-to-each-publisher | |
} | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"id": "2d1781af-3a4c-4d7c-bd0c-e34b19da4e66", | |
"topic": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", | |
"subject": "", | |
"data": { | |
"validationCode": "512d38b6-c7b8-40c8-89fe-f46f9e9622b6", | |
"validationUrl": "https://rp-eastus2.eventgrid.azure.net:553/eventsubscriptions/estest/validate?id=B2E34264-7D71-453A-B5FB-B62D0FDC85EE&t=2018-04-26T20:30:54.4538837Z&apiVersion=2018-05-01-preview&token=1BNqCxBBSSE9OnNSfZM4%2b5H9zDegKMY6uJ%2fO2DFRkwQ%3d" | |
}, | |
"eventType": "Microsoft.EventGrid.SubscriptionValidationEvent", | |
"eventTime": "2018-07-15T22:12:19.4556811Z", | |
"metadataVersion": "1", | |
"dataVersion": "1" | |
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EventGridEventTrigger.Library | |
{ | |
public class SubscriptionValidationEventData | |
{ | |
public string ValidationCode { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EventGridEventTrigger.Library | |
{ | |
public class SubscriptionValidationResponseData | |
{ | |
public string ValidationResponse { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ValidationResponse": "512d38b6-c7b8-40c8-89fe-f46f9e9622b6" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment