-
Notifications
You must be signed in to change notification settings - Fork 87
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
Open
erwinkramer
wants to merge
24
commits into
cloudevents:main
Choose a base branch
from
erwinkramer:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3277dc1
.net 10
erwinkramer 262f7f9
.net 10
erwinkramer c56f814
minimal api approach
erwinkramer cf42e8d
use system.text.json
erwinkramer 44f7a51
leverage CopyToHttpResponseAsync
erwinkramer 17d743a
streamlining
erwinkramer 227a2a1
system.text for httpsend
erwinkramer a4cf801
sdk fix
erwinkramer e3d4bc7
rollback framework version for test projects
erwinkramer 63540da
rollback csharp reference
erwinkramer fc900e3
fix integration tests
erwinkramer f0997a0
structured and binary mode improvements
erwinkramer f1f4d95
some global testing packages updates
erwinkramer 6044561
allow for content-less via binary mode for CloudEventBinding
erwinkramer cec2d05
single line jsonOptions
erwinkramer f496b08
static cloudEventOperations
erwinkramer 140b9a3
copyright
erwinkramer f05a373
streamline namespace usage in samples
erwinkramer 34f356f
Linq for getting attributes
erwinkramer 4a4227e
Modernize infrastructure
jskeet 587f867
drop version override for testing lib
erwinkramer 6637372
Revert "drop version override for testing lib"
erwinkramer b6eeea3
Merge branch 'main' into main
erwinkramer c5a16b0
revert enter on packages props
erwinkramer 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.
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.