forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cncf namespace samples (Azure#44994)
* Add CNCF sample * Update readme * new recordings * event * recording * Use dynamic
- Loading branch information
1 parent
f86a69b
commit 8456ad5
Showing
7 changed files
with
100 additions
and
1 deletion.
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
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
36 changes: 36 additions & 0 deletions
36
sdk/eventgrid/Azure.Messaging.EventGrid.Namespaces/samples/Sample2_CNCF.md
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,36 @@ | ||
# Using the Cloud Native CloudEvent type | ||
|
||
It is possible to publish and receive the CloudNative CloudEvent type found in the [CloudNative.CloudEvents library](https://www.nuget.org/packages/CloudNative.CloudEvents) using the Azure Event Grid Namespaces client library. | ||
First we create a `CloudEvent` object and publish it to the namespace topic using the `EventGridSenderClient`. | ||
|
||
```C# Snippet:SendCNCFEvent | ||
var evt = new CloudNative.CloudEvents.CloudEvent | ||
{ | ||
Source = new Uri("http://localHost"), | ||
Type = "type", | ||
Data = new TestModel { Name = "Bob", Age = 18 }, | ||
Id = Recording.Random.NewGuid().ToString() | ||
}; | ||
var jsonFormatter = new JsonEventFormatter(); | ||
var sender = new EventGridSenderClient(new Uri(namespaceTopicHost), topicName, new AzureKeyCredential(namespaceKey)); | ||
await sender.SendEventAsync(RequestContent.Create(jsonFormatter.EncodeStructuredModeMessage(evt, out _))); | ||
``` | ||
|
||
Next, we receive the events using the `EventGridReceiverClient`. | ||
|
||
```C# Snippet:ReceiveCNCFEvent | ||
var receiver = new EventGridReceiverClient(new Uri(namespaceTopicHost), topicName, subscriptionName, new AzureKeyCredential(namespaceKey)); | ||
Response response = await receiver.ReceiveAsync(maxEvents: 1, maxWaitTime: TimeSpan.FromSeconds(10), new RequestContext()); | ||
|
||
var eventResponse = response.Content.ToDynamicFromJson(JsonPropertyNames.CamelCase).Value[0]; | ||
var receivedCloudEvent = jsonFormatter.DecodeStructuredModeMessage( | ||
Encoding.UTF8.GetBytes(eventResponse.Event.ToString()), | ||
new ContentType("application/cloudevents+json"), | ||
null); | ||
``` | ||
|
||
Finally, we acknowledge the event using the lock token. | ||
|
||
```C# Snippet:AcknowledgeCNCFEvent | ||
AcknowledgeResult acknowledgeResult = await receiver.AcknowledgeAsync(new string[] { eventResponse.BrokerProperties.LockToken.ToString() }); | ||
``` |
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