-
Notifications
You must be signed in to change notification settings - Fork 86
Modernize ASP.NET Core samples #326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
samples/CloudNative.CloudEvents.AspNetCoreSample/CloudEventBinding.cs
This file contains hidden or 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,56 @@ | ||
| // Copyright (c) Cloud Native Foundation. | ||
| // Licensed under the Apache 2.0 license. | ||
| // See LICENSE file in the project root for full license information. | ||
|
|
||
| using CloudNative.CloudEvents.AspNetCore; | ||
| using CloudNative.CloudEvents.Core; | ||
| using CloudNative.CloudEvents.SystemTextJson; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.HttpResults; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace CloudNative.CloudEvents.AspNetCoreSample; | ||
|
|
||
| public class CloudEventBinding : IBindableFromHttpContext<CloudEventBinding> | ||
| { | ||
| public CloudEvent Value { get; init; } | ||
|
|
||
| public ProblemHttpResult Error { get; init; } | ||
|
|
||
| public static async ValueTask<CloudEventBinding> BindAsync(HttpContext context, ParameterInfo parameter) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just as a heads-up, I'm really not familiar with IBindableFromHttpContext. I'm mostly following along, but I could easily miss things. |
||
| { | ||
| Validation.CheckNotNull(context, nameof(context)); | ||
| Validation.CheckNotNull(parameter, nameof(parameter)); | ||
|
|
||
| var request = context.Request; | ||
|
|
||
| // Even though we're not allowing non-JSON content in this binding, | ||
| // types such as "text/xml" could still be parsed with the current JsonEventFormatter, | ||
| // but it's just not making it strongly typed, or anything structured (XmlNode). | ||
| // Depending on your use-case, it may or may not be desirable to allow that. | ||
| if (request.ContentLength != 0 && !request.HasJsonContentType()) | ||
| { | ||
| return new CloudEventBinding | ||
| { | ||
| Error = TypedResults.Problem( | ||
| statusCode: StatusCodes.Status415UnsupportedMediaType, | ||
| title: "Unsupported media type", | ||
| detail: "Request content type is not JSON and not fully supported in this binding. " + | ||
| "Please note: the CloudEvents specification does allow for any data content, " + | ||
| "as long as it adheres to the provided datacontenttype." | ||
| ) | ||
| }; | ||
| } | ||
|
|
||
| var formatter = context.RequestServices.GetRequiredService<JsonEventFormatter>(); | ||
|
|
||
| var cloudEvent = await request.ToCloudEventAsync(formatter); | ||
|
|
||
| return new CloudEventBinding | ||
| { | ||
| Value = cloudEvent | ||
| }; | ||
| } | ||
| } | ||
63 changes: 0 additions & 63 deletions
63
samples/CloudNative.CloudEvents.AspNetCoreSample/CloudEventJsonInputFormatter.cs
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
samples/CloudNative.CloudEvents.AspNetCoreSample/CloudEventOperations.cs
This file contains hidden or 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,60 @@ | ||
| // Copyright (c) Cloud Native Foundation. | ||
| // Licensed under the Apache 2.0 license. | ||
| // See LICENSE file in the project root for full license information. | ||
|
|
||
| using CloudNative.CloudEvents.AspNetCore; | ||
| using CloudNative.CloudEvents.SystemTextJson; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.HttpResults; | ||
| using System; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace CloudNative.CloudEvents.AspNetCoreSample; | ||
|
|
||
| public static class CloudEventOperations | ||
| { | ||
| public static async Task<Results<ProblemHttpResult, JsonHttpResult<object>>> ReceiveCloudEvent(CloudEventBinding cloudEventBinding) | ||
| { | ||
| if (cloudEventBinding.Error is not null) | ||
| { | ||
| return cloudEventBinding.Error; | ||
| } | ||
|
|
||
| var cloudEvent = cloudEventBinding.Value; | ||
|
|
||
| var cloudEventAttributes = cloudEvent.GetPopulatedAttributes() | ||
| .ToDictionary(pair => pair.Key.Name, pair => pair.Key.Format(pair.Value)); | ||
|
|
||
| return TypedResults.Json<object>(new | ||
| { | ||
| note = "wow, such event, much disassembling, very skill", | ||
| cloudEvent.SpecVersion.VersionId, | ||
| cloudEventAttributes, | ||
| cloudEvent.Data | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Generates a CloudEvent. | ||
| /// </summary> | ||
| public static async Task GenerateCloudEvent(HttpResponse response, JsonEventFormatter formatter, ContentMode contentMode = ContentMode.Structured) | ||
| { | ||
| var evt = new CloudEvent | ||
| { | ||
| Type = "CloudNative.CloudEvents.AspNetCoreSample", | ||
| Source = new Uri("https://github.com/cloudevents/sdk-csharp"), | ||
| Time = DateTimeOffset.Now, | ||
| DataContentType = "application/json", | ||
| Id = Guid.NewGuid().ToString(), | ||
| Data = new | ||
| { | ||
| Language = "C#", | ||
| EnvironmentVersion = Environment.Version.ToString() | ||
| } | ||
| }; | ||
|
|
||
| response.StatusCode = StatusCodes.Status200OK; | ||
| await evt.CopyToHttpResponseAsync(response, contentMode, formatter); | ||
| } | ||
| } |
This file contains hidden or 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
44 changes: 42 additions & 2 deletions
44
...es/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.http
This file contains hidden or 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 |
|---|---|---|
| @@ -1,16 +1,56 @@ | ||
| @HostAddress = https://localhost:5001 | ||
|
|
||
| ### Send via Structured mode | ||
|
|
||
| POST {{HostAddress}}/api/events/receive | ||
| Content-Type: application/cloudevents+json; charset=utf-8 | ||
|
|
||
| { | ||
| "specversion": "1.0", | ||
| "type": "com.example.myevent", | ||
| "source": "urn:example-com:mysource:abc", | ||
| "id": "{{$guid}}", | ||
| "data": { | ||
| "message": "Hello world!" | ||
| } | ||
| } | ||
|
|
||
| ### Send via Binary mode | ||
|
|
||
| POST {{HostAddress}}/api/events/receive | ||
| Content-Type: application/json | ||
| CE-SpecVersion: 1.0 | ||
| CE-Type: "com.example.myevent" | ||
| CE-Source: "urn:example-com:mysource:abc" | ||
| CE-Id: "c457b7c5-c038-4be9-98b9-938cb64a4fbf" | ||
| CE-Id: "{{$guid}}" | ||
|
|
||
| { | ||
| "message": "Hello world!" | ||
| } | ||
|
|
||
| ### | ||
| ### Send content-less via Binary mode | ||
|
|
||
| POST {{HostAddress}}/api/events/receive | ||
| CE-SpecVersion: 1.0 | ||
| CE-Type: "com.example.myevent" | ||
| CE-Source: "urn:example-com:mysource:abc" | ||
| CE-Id: "{{$guid}}" | ||
|
|
||
| ### Send unsupported media type (XML) via Binary mode | ||
|
|
||
| POST {{HostAddress}}/api/events/receive | ||
| Content-Type: text/xml | ||
| CE-SpecVersion: 1.0 | ||
| CE-Type: "com.example.myevent" | ||
| CE-Source: "urn:example-com:mysource:abc" | ||
| CE-Id: "{{$guid}}" | ||
|
|
||
| <message>Hello world!</message> | ||
|
|
||
| ### Generate via Structured mode | ||
|
|
||
| GET {{HostAddress}}/api/events/generate | ||
|
|
||
| ### Generate via Binary mode | ||
|
|
||
| GET {{HostAddress}}/api/events/generate?contentMode=Binary |
66 changes: 0 additions & 66 deletions
66
samples/CloudNative.CloudEvents.AspNetCoreSample/Controllers/CloudEventController.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.