Skip to content

Repository files navigation

SharpEventGridServer

Receiving EventGrid Events via WebHook in your application? This is your library.

From Microsoft:

Azure Event Grid is an innovative offering that makes an event a first-class object in Azure. With Azure Event Grid, you can subscribe to any event that is happening across your Azure resources and react using serverless platforms like Functions or Logic Apps. In addition to having built-in publishing support for events with services like Blob Storage and Resource Groups, Event Grid provides flexibility and allows you to create your own custom events to publish directly to the service.

This library simplify the receiving of events in your ASP.NET application.

Nuget

Install-Package sharpeventgridserver

How to use

After adding the nuget package, open your Startup class and go to the Configure method:

varopt=newEventGridOptions();//Automatically validate Azure EventGrid subscription confirmation event//to prove that you own the subscriberopt.AutoValidateSubscription=true;//Callback for Azure Event Grid validation attempts (debugging)opt.AutoValidateSubscriptionAttemptNotifier=(url,success,message)=>{Debug.WriteLine($"Validation attempt: {url} -> Success: {success}: {message}");};//Path to receive your eventsopt.EventsPath="api/events";//Map each kind of event to an IEventGridHandler class opt.MapEvent<SomeEventHandler>("someEventType");opt.MapEvent<SomeOtherEventHandler>("someOtherEventType");//Any event type without a mapping will be mapped to the default //handler specified hereopt.MapDefault<DefaultMappingHandler>();//If you decided to add a key to every Azure EventGrid request//to certify its authenticity, set the key name and value here//so SharpEventGridServer will automatically verify it for youopt.SetValidationKey("key","foo");//Finally, add SharpEventGridServer middleware to your pipelineapp.UseEventGrid(opt);

And that's it: configure Azure EventGrid to send events to https://[yourserver]/api/events and you're all set.

EventGrid Event Handlers

Every EventGrid event handler has to implement the IEventGridHandler interface. Ex:

publicclassSomeEventHandler:IEventGridHandler{publicasyncTaskProcessEvent(EventeventItem){//do something}}

An IEventGridHandler is instanciated calling its default constructor every time a new event arrives for it. If you want to use the same instance or have parameters in its constructor, register it in the default asp.net DI.

Dependency Injection on EventGrid Event Handlers

When mapping event handlers, SharpEventGridServer will call the default service locator to instanciate the required object.

Ex: Startup.cs

publicvoidConfigureServices(IServiceCollectionservices){services.AddSingleton<NewCustomerEventHandler>();services.AddSingleton<IDatabase,Database>();services.AddMvc();}

NewCustomerEventHandler.cs

publicclassNewCustomerEventHandler:IEventGridHandler{privateIDatabase_database;publicNewCustomerEventHandler(IDatabasedatabase){_database=database;}publicasyncTaskProcessEvent(EventeventItem){varnewCustomerEvent=eventItem.DeserializeEvent<NewCustomerEvent>();await_database.SaveAsync(newCustomerEvent);}}

About

Receiving EventGrid Events via WebHook in your application? This is your library.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages