Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

docs: publish v3.2 feature docs #232

Merged
Merged
25 changes: 25 additions & 0 deletions docs/versioned_docs/version-v3.2.0/01-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: "Arcus Event Grid"
layout: default
slug: /
sidebar_label: Welcome
---

# Installation

```shell
PM > Install-Package Arcus.EventGrid.All
```

# Features

- Support for publishing events to an Event Grid Topic ([docs](./02-Features/publishing-events.md))
- Support for handling endpoint validation handshake ([docs](./02-Features/endpoint-validation.md))
- Provides tooling for writing integration tests ([docs](./02-Features/running-integration-tests.md))
- Support for deserializing official Azure or custom events according to Event Grid & CloudEvent schema ([docs](./02-Features/deserializing-events.md))
- Support for creating your own events ([docs](./02-Features/create-custom-events.md))

# License
This is licensed under The MIT License (MIT). Which means that you can use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the web application. But you always need to state that Codit is the original author of this web application.

*[Full license here](https://github.com/arcus-azure/arcus.eventgrid/blob/master/LICENSE)*
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: "Create custom events"
layout: default
---

## Create Custom Events

![](https://img.shields.io/badge/Available%20starting-v3.0-green?link=https://github.com/arcus-azure/arcus.eventgrid/releases/tag/v3.0.0)

We allow you to create custom events next to the [official Azure SDK event schemas](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.eventgrid.models?view=azure-dotnet).

The official SDK provides a class called `EventGridEvent` which provides your event data as an ordinary .NET `object`.
We provide a variation of the official `EventGridEvent` which provides your data as an typed instance instead.

This makes sure that your new custom events are both type-safe and are supported by any official functions that uses the official `EventGridEvent`.

```csharp
using Arcus.EventGrid.Contracts;
using Newtonsoft.Json;

public class CarEventData
{
public CarEventData(string licensePlate)
{
LicensePlate = licensePlate;
}

public string LicensePlate { get; }
}

public class NewCarRegistered : EventGridEvent<CarEventData>
{
private const string DefaultDataVersion = "1",
DefaultEventType = "Arcus.Samples.Cars.NewCarRegistered";

// `JsonConstructor` attribute is not necessary but can help as documentation for the event.
[JsonConstructor]
private NewCarRegistered()
{
}

public NewCarRegistered(string id, string licensePlate) : this(id, "New registered car", licensePlate)
{
}

public NewCarRegistered(string id, string subject, string licensePlate)
: base(id, subject, new CarEventData(licensePlate), DefaultDataVersion, DefaultEventType)
{
}
}
```

Read ["Deserializing Events"](./deserializing-events.md) for information on how custom events are deserialized.
110 changes: 110 additions & 0 deletions docs/versioned_docs/version-v3.2.0/02-Features/deserializing-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: "Deserializing Events"
layout: default
---

## Deserializing Events

![](https://img.shields.io/badge/Available%20starting-v3.0-green?link=https://github.com/arcus-azure/arcus.eventgrid/releases/tag/v3.0.0)

The `Arcus.EventGrid` package provides several ways to deserializing events.

Following paragraphs describe each supported type of event.

- [Deserializing Events](#deserializing-events)
- [Deserializing Built-In Azure Events](#deserializing-built-in-azure-events)
- [Deserializing CloudEvent Events](#deserializing-cloudevent-events)
- [Deserializing Event Grid Events](#deserializing-event-grid-events)
- [Deserializing Custom Events](#deserializing-custom-events)

### Deserializing Built-In Azure Events

When using official Azure events, you can use `.ParseFromData<>` to deserialize them based on the built-in types as shown in the example:

```csharp
using Arcus.EventGrid;
using Arcus.EventGrid.Parsers;
using Microsoft.Azure.EventGrid.Models;

// Parse directly from an event data type with the `.Parse` function.
byte[] rawEvent = ...
EventBatch<Event> eventBatch = EventParser.Parse(rawEvent);

// The type `EventGridEvent` comes directly from the official SDK package
// and can be cast implicitly like so,
EventGridEvent eventGridEvent = eventBatch.Events.Single();

// Or explicitly.
EventGridEvent eventGridEvent = eventBatch.Events.Single().AsEventGridEvent();

// The actual EventGrid payload can be retrieved by passing along the Azure SDK model type.
var storageEventData = eventGridEvent.GetPayload<StorageBlobCreatedEventData>();
```

You can find a list of supported built-in Azure events [in the official documentation](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.eventgrid.models?view=azure-dotnet).

### Deserializing CloudEvent Events

We provide support for deserializing CloudEvents using the [official SDK for C#](https://github.com/cloudevents/sdk-csharp).

Upon receiving of CloudEvent events:

```csharp
using Arcus.EventGrid;
using Arcus.EventGrid.Parsers;
using CloudNative.CloudEvents;

string cloudEventJson = ...
EventBatch<Event> eventBatch = EventParser.Parse(cloudEventJson);
var events = eventBatch.Events;

// The type `CloudEvent` comes directly from the official SDK package
// and can be cast implicitly like so,
CloudEvent cloudEvent = events.First();

// Or explicitly.
CloudEvent cloudEvent = events.First().AsCloudEvent();
```

### Deserializing Event Grid Events

We provide support for deserializing [EventGrid events](https://docs.microsoft.com/en-us/azure/event-grid/event-schema).

```csharp
using Arcus.EventGrid;
using Arcus.EventGrid.Parsers;
using Microsoft.Azure.EventGrid.Models;

string eventGridEventJson = ...
EventBatch<Event> eventBatch = EventParser.Parse(eventGridEventJson);
var events = eventBatch.Events;

// The `EventGridEvent` can be cast implicitly like so,
EventGridEvent eventGridEvent = events.First();

// Or explicitly.
EventGridEvent eventGridEvent = events.First().AsEventGridEvent();
```

### Deserializing Custom Events

We provide support for deserializing events to typed event objects where the custom event payload is available via the `.GetPayload()` method.

If you want to have the original raw JSON event payload, you can get it via the `.Data` property.

```csharp
using Arcus.EventGrid;
using Arcus.EventGrid.Parsers;

// Parse from your custom event implementation with the `.Parse<>` function.
EventGridBatch<NewCarRegistered> eventGridBatch = EventGridParser.Parse<NewCarRegistered>(rawEvent);

// The event data type will be wrapped inside an `EventGridEvent<>` instance.
NewCarRegistered eventGridMessage = eventGridBatch.Events.First();

// The original event payload can now be accessed.
CarEventData typedEventPayload = eventGridMessage.GetPayload();
object untypedEventPayload = eventGridMessage.Data;
```

[&larr; back](/)
Loading