forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Design and Implement Consumer for EventGrid (Azure#14048)
* Added new EventGridConsumer and EventGridConsumerOptions classes * Added unit tests for consuming events * Renamed publish events methods to SendEvents and SendEventsAsync (with overloads for EventGridEvent, CloudEvent, and object)
- Loading branch information
Showing
26 changed files
with
2,215 additions
and
167 deletions.
There are no files selected for viewing
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
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
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
49 changes: 49 additions & 0 deletions
49
sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CustomModelSerializer.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using Azure.Core; | ||
using Azure.Core.Serialization; | ||
|
||
namespace Azure.Messaging.EventGrid | ||
{ | ||
/// <summary> | ||
/// UTF-8 JSON-serializable wrapper for objects such as custom schema events. | ||
/// Takes a custom ObjectSerializer to use when writing the object as JSON text. | ||
/// </summary> | ||
internal class CustomModelSerializer : IUtf8JsonSerializable | ||
{ | ||
public object _payload; | ||
public CancellationToken _cancellationToken; | ||
public ObjectSerializer _serializer; | ||
|
||
/// <summary> | ||
/// Initializes an instance of the CustomModelSerializer class. | ||
/// </summary> | ||
/// <param name="payload"> | ||
/// Object that can represent an event with a custom schema, or additional properties | ||
/// added to the event envelope. | ||
/// </param> | ||
/// <param name="serializer"> | ||
/// Custom ObjectSerializer to use when writing the object as JSON text. | ||
/// </param> | ||
/// <param name="cancellationToken"> The cancellation token to use. </param> | ||
public CustomModelSerializer(object payload, ObjectSerializer serializer, CancellationToken cancellationToken) | ||
{ | ||
_payload = payload; | ||
_serializer = serializer; | ||
_cancellationToken = cancellationToken; | ||
} | ||
public void Write(Utf8JsonWriter writer) | ||
{ | ||
var stream = new MemoryStream(); | ||
_serializer.Serialize(stream, _payload, _payload.GetType(), _cancellationToken); | ||
stream.Seek(0, SeekOrigin.Begin); | ||
JsonDocument.Parse(stream).WriteTo(writer); | ||
} | ||
} | ||
} |
Oops, something went wrong.