-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .NET 7, 8, trimming and AOT support
Also fixed several nullability warnings.
- Loading branch information
Showing
35 changed files
with
846 additions
and
283 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
18 changes: 18 additions & 0 deletions
18
samples/CloudNative.CloudEvents.MinApiSample/CloudNative.CloudEvents.MinApiSample.csproj
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\CloudNative.CloudEvents.AspNetCore\CloudNative.CloudEvents.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\..\src\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj" /> | ||
<ProjectReference Include="..\..\src\CloudNative.CloudEvents.SystemTextJson\CloudNative.CloudEvents.SystemTextJson.csproj" /> | ||
</ItemGroup> | ||
<PropertyGroup Condition="'$(Configuration)'=='Release'"> | ||
<SelfContained>true</SelfContained> | ||
<PublishAot>true</PublishAot> | ||
<DebugType>None</DebugType> | ||
<DebugSymbols>False</DebugSymbols> | ||
</PropertyGroup> | ||
</Project> |
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,85 @@ | ||
// 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; | ||
using CloudNative.CloudEvents.Http; | ||
using CloudNative.CloudEvents.SystemTextJson; | ||
using CloudNative.CloudEvents.AspNetCore; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
using System.Text; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | ||
var formatter = new JsonEventFormatter<Message>(MyJsonContext.Default); | ||
|
||
app.MapPost("/api/events/receive/", async (HttpRequest request) => | ||
{ | ||
var cloudEvent = await request.ToCloudEventAsync(formatter); | ||
using var ms = new MemoryStream(); | ||
using var writer = new Utf8JsonWriter(ms, new() { Indented = true }); | ||
writer.WriteStartObject(); | ||
foreach (var (attribute, value) in cloudEvent.GetPopulatedAttributes()) | ||
writer.WriteString(attribute.Name, attribute.Format(value)); | ||
writer.WriteEndObject(); | ||
await writer.FlushAsync(); | ||
var attributeMap = Encoding.UTF8.GetString(ms.ToArray()); | ||
return Results.Text($"Received event with ID {cloudEvent.Id}, attributes: {attributeMap}"); | ||
}); | ||
|
||
app.MapPost("/api/events/receive2/", (Event e) => Results.Json(e.CloudEvent.Data, MyJsonContext.Default)); | ||
|
||
app.MapPost("/api/events/receive3/", (Message message) => Results.Json(message, MyJsonContext.Default)); | ||
|
||
app.MapGet("/api/events/generate/", () => | ||
{ | ||
var evt = new CloudEvent | ||
{ | ||
Type = "CloudNative.CloudEvents.MinApiSample", | ||
Source = new Uri("https://github.com/cloudevents/sdk-csharp"), | ||
Time = DateTimeOffset.Now, | ||
DataContentType = "application/json", | ||
Id = Guid.NewGuid().ToString(), | ||
Data = new Message("C#", Environment.Version.ToString()) | ||
}; | ||
// Format the event as the body of the response. This is UTF-8 JSON because of | ||
// the CloudEventFormatter we're using, but EncodeStructuredModeMessage always | ||
// returns binary data. We could return the data directly, but for debugging | ||
// purposes it's useful to have the JSON string. | ||
var bytes = formatter.EncodeStructuredModeMessage(evt, out var contentType); | ||
string json = Encoding.UTF8.GetString(bytes.Span); | ||
// Specify the content type of the response: this is what makes it a CloudEvent. | ||
// (In "binary mode", the content type is the content type of the data, and headers | ||
// indicate that it's a CloudEvent.) | ||
return Results.Content(json, contentType.MediaType, Encoding.UTF8); | ||
}); | ||
|
||
app.Run(); | ||
|
||
[JsonSerializable(typeof(Message))] | ||
internal partial class MyJsonContext : JsonSerializerContext { } | ||
|
||
public class Event | ||
{ | ||
private readonly static JsonEventFormatter formatter = new JsonEventFormatter<Message>(MyJsonContext.Default); | ||
// required for receive2 | ||
public static async ValueTask<Event?> BindAsync(HttpContext context) | ||
{ | ||
var cloudEvent = await context.Request.ToCloudEventAsync(formatter); | ||
return new Event { CloudEvent = cloudEvent }; | ||
} | ||
public required CloudEvent CloudEvent { get; init; } | ||
} | ||
|
||
record class Message(string Language, string EnvironmentVersion) | ||
{ | ||
private readonly static JsonEventFormatter formatter = new JsonEventFormatter<Message>(MyJsonContext.Default); | ||
// required for receive3 | ||
public static async ValueTask<Message?> BindAsync(HttpContext context) | ||
{ | ||
var cloudEvent = await context.Request.ToCloudEventAsync(formatter); | ||
return cloudEvent.Data is Message message ? message : null; | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
samples/CloudNative.CloudEvents.MinApiSample/Properties/launchSettings.json
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,14 @@ | ||
{ | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "api/events/generate", | ||
"applicationUrl": "http://localhost:5002", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
samples/CloudNative.CloudEvents.MinApiSample/appsettings.Development.json
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/CloudNative.CloudEvents.MinApiSample/appsettings.json
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="docopt.net" Version="0.8.1" /> | ||
<ProjectReference Include="..\..\src\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj" /> | ||
<ProjectReference Include="..\..\src\CloudNative.CloudEvents.SystemTextJson\CloudNative.CloudEvents.SystemTextJson.csproj" /> | ||
</ItemGroup> | ||
<PropertyGroup Condition="'$(Configuration)'=='Release'"> | ||
<SelfContained>true</SelfContained> | ||
<PublishAot>true</PublishAot> | ||
<DebugType>None</DebugType> | ||
<DebugSymbols>False</DebugSymbols> | ||
</PropertyGroup> | ||
</Project> |
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,70 @@ | ||
// 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; | ||
using CloudNative.CloudEvents.Http; | ||
using CloudNative.CloudEvents.SystemTextJson; | ||
using DocoptNet; | ||
using System.Net.Mime; | ||
using static System.Console; | ||
|
||
// This application uses the docopt.net library for parsing the command | ||
// line and calling the application code. | ||
ProgramArguments programArguments = new(); | ||
var result = await ProgramArguments.CreateParserWithVersion() | ||
.Parse(args) | ||
.Match(RunAsync, | ||
result => { WriteLine(result.Help); return Task.FromResult(1); }, | ||
result => { WriteLine(result.Version); return Task.FromResult(0); }, | ||
result => { Error.WriteLine(result.Usage); return Task.FromResult(1); }); | ||
return result; | ||
|
||
static async Task<int> RunAsync(ProgramArguments args) | ||
{ | ||
var cloudEvent = new CloudEvent | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
Type = args.OptType, | ||
Source = new Uri(args.OptSource), | ||
DataContentType = MediaTypeNames.Application.Json, | ||
Data = System.Text.Json.JsonSerializer.Serialize("hey there!", GeneratedJsonContext.Default.String) | ||
}; | ||
|
||
var content = cloudEvent.ToHttpContent(ContentMode.Structured, new JsonEventFormatter(GeneratedJsonContext.Default)); | ||
|
||
var httpClient = new HttpClient(); | ||
// Your application remains in charge of adding any further headers or | ||
// other information required to authenticate/authorize or otherwise | ||
// dispatch the call at the server. | ||
var result = await httpClient.PostAsync(args.OptUrl, content); | ||
|
||
WriteLine(result.StatusCode); | ||
return 0; | ||
} | ||
|
||
[System.Text.Json.Serialization.JsonSerializable(typeof(string))] | ||
internal partial class GeneratedJsonContext : System.Text.Json.Serialization.JsonSerializerContext | ||
{ | ||
} | ||
|
||
[DocoptArguments] | ||
partial class ProgramArguments | ||
{ | ||
const string Help = @"HttpSendJson. | ||
Usage: | ||
HttpSendJson --url=URL [--type=TYPE] [--source=SOURCE] | ||
HttpSendJson (-h | --help) | ||
HttpSendJson --version | ||
Options: | ||
--url=URL HTTP(S) address to send the event to. | ||
--type=TYPE CloudEvents 'type' [default: com.example.myevent]. | ||
--source=SOURCE CloudEvents 'source' [default: urn:example-com:mysource:abc]. | ||
-h --help Show this screen. | ||
--version Show version. | ||
"; | ||
public static string Version => $"producer {typeof(ProgramArguments).Assembly.GetName().Version}"; | ||
public static IParser<ProgramArguments> CreateParserWithVersion() => CreateParser().WithVersion(Version); | ||
} |
Oops, something went wrong.