Skip to content

Commit e4ee7e0

Browse files
authored
Merge pull request cloudevents#21 from TobbenTM/add-aspnet-core-support
Add aspnet core support
2 parents 1b7f22f + bddeb90 commit e4ee7e0

20 files changed

+553
-4
lines changed

CloudEvents.sln

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26124.0
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29001.49
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudNative.CloudEvents", "src\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj", "{C5DC9F44-7C03-4A70-80EF-7A29696455EB}"
77
EndProject
@@ -23,6 +23,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudNative.CloudEvents.Amq
2323
EndProject
2424
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudNative.CloudEvents.Kafka", "src\CloudNative.CloudEvents.Kafka\CloudNative.CloudEvents.Kafka.csproj", "{193D6D9D-C1A0-459E-86CF-F207CDF0FC73}"
2525
EndProject
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudNative.CloudEvents.AspNetCore", "src\CloudNative.CloudEvents.AspNetCore\CloudNative.CloudEvents.AspNetCore.csproj", "{C726DD78-2D56-48D3-928A-D10226E3750B}"
27+
EndProject
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudNative.CloudEvents.AspNetCoreSample", "samples\CloudNative.CloudEvents.AspNetCoreSample\CloudNative.CloudEvents.AspNetCoreSample.csproj", "{9760D744-D1BF-40E3-BD6F-7F639BFB9188}"
29+
EndProject
30+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CloudNative.CloudEvents.IntegrationTests", "test\CloudNative.CloudEvents.IntegrationTests\CloudNative.CloudEvents.IntegrationTests.csproj", "{9639E4FD-0438-4901-B57F-EFF773B19D5A}"
31+
EndProject
2632
Global
2733
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2834
Debug|Any CPU = Debug|Any CPU
@@ -105,6 +111,42 @@ Global
105111
{193D6D9D-C1A0-459E-86CF-F207CDF0FC73}.Release|x64.Build.0 = Release|Any CPU
106112
{193D6D9D-C1A0-459E-86CF-F207CDF0FC73}.Release|x86.ActiveCfg = Release|Any CPU
107113
{193D6D9D-C1A0-459E-86CF-F207CDF0FC73}.Release|x86.Build.0 = Release|Any CPU
114+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Debug|Any CPU.Build.0 = Debug|Any CPU
116+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Debug|x64.ActiveCfg = Debug|Any CPU
117+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Debug|x64.Build.0 = Debug|Any CPU
118+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Debug|x86.ActiveCfg = Debug|Any CPU
119+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Debug|x86.Build.0 = Debug|Any CPU
120+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Release|Any CPU.ActiveCfg = Release|Any CPU
121+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Release|x64.ActiveCfg = Release|Any CPU
123+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Release|x64.Build.0 = Release|Any CPU
124+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Release|x86.ActiveCfg = Release|Any CPU
125+
{C726DD78-2D56-48D3-928A-D10226E3750B}.Release|x86.Build.0 = Release|Any CPU
126+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
127+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Debug|Any CPU.Build.0 = Debug|Any CPU
128+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Debug|x64.ActiveCfg = Debug|Any CPU
129+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Debug|x64.Build.0 = Debug|Any CPU
130+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Debug|x86.ActiveCfg = Debug|Any CPU
131+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Debug|x86.Build.0 = Debug|Any CPU
132+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Release|Any CPU.ActiveCfg = Release|Any CPU
133+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Release|Any CPU.Build.0 = Release|Any CPU
134+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Release|x64.ActiveCfg = Release|Any CPU
135+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Release|x64.Build.0 = Release|Any CPU
136+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Release|x86.ActiveCfg = Release|Any CPU
137+
{9760D744-D1BF-40E3-BD6F-7F639BFB9188}.Release|x86.Build.0 = Release|Any CPU
138+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
139+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
140+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Debug|x64.ActiveCfg = Debug|Any CPU
141+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Debug|x64.Build.0 = Debug|Any CPU
142+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Debug|x86.ActiveCfg = Debug|Any CPU
143+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Debug|x86.Build.0 = Debug|Any CPU
144+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
145+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Release|Any CPU.Build.0 = Release|Any CPU
146+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Release|x64.ActiveCfg = Release|Any CPU
147+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Release|x64.Build.0 = Release|Any CPU
148+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Release|x86.ActiveCfg = Release|Any CPU
149+
{9639E4FD-0438-4901-B57F-EFF773B19D5A}.Release|x86.Build.0 = Release|Any CPU
108150
EndGlobalSection
109151
GlobalSection(SolutionProperties) = preSolution
110152
HideSolutionNode = FALSE

CloudEvents.v3.ncrunchsolution

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<SolutionConfiguration>
2+
<Settings>
3+
<AllowParallelTestExecution>False</AllowParallelTestExecution>
4+
<SolutionConfigured>True</SolutionConfigured>
5+
</Settings>
6+
</SolutionConfiguration>

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,42 @@ await context.Response.CopyFromAsync(cloudEvent,
208208
context.Response.StatusCode = (int)HttpStatusCode.OK;
209209
```
210210

211+
### HTTP - Microsoft.AspNetCore.Http.HttpRequest
212+
213+
On the server-side, you can extract a CloudEvent from the server-side `HttpRequest`
214+
with the `ReadCloudEventAsync()` extension.
215+
216+
```C#
217+
var cloudEvent = await HttpContext.Request.ReadCloudEventAsync();
218+
```
219+
220+
### HTTP - ASP.NET Core MVC
221+
222+
If you would like to deserialize CloudEvents in actions directly, you can register the
223+
`CloudEventJsonInputFormatter` in the MVC options:
224+
225+
```C#
226+
public void ConfigureServices(IServiceCollection services)
227+
{
228+
services.AddMvc(opts =>
229+
{
230+
opts.InputFormatters.Insert(0, new CloudEventJsonInputFormatter());
231+
});
232+
}
233+
```
234+
235+
This formatter will only intercept parameters where CloudEvent is the expected type.
236+
237+
You can then receive CloudEvent objects in controller actions:
238+
239+
```C#
240+
[HttpPost("resource")]
241+
public IActionResult ReceiveCloudEvent([FromBody] CloudEvent cloudEvent)
242+
{
243+
return Ok();
244+
}
245+
```
246+
211247
### AMQP
212248

213249
The SDK provides extensions for the [AMQPNetLite](https://github.com/Azure/amqpnetlite) package.

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# main branch
22
version: 1.0.{build}
33
clone_depth: 15
4+
image: Visual Studio 2019
45

56
branches:
67
only:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\CloudNative.CloudEvents.AspNetCore\CloudNative.CloudEvents.AspNetCore.csproj" />
9+
<ProjectReference Include="..\..\src\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
4+
5+
namespace CloudNative.CloudEvents.AspNetCoreSample.Controllers
6+
{
7+
using System.Collections.Generic;
8+
using CloudNative.CloudEvents;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Newtonsoft.Json;
11+
12+
[Route("api/events")]
13+
[ApiController]
14+
public class CloudEventController : ControllerBase
15+
{
16+
[HttpPost("receive")]
17+
public ActionResult<IEnumerable<string>> ReceiveCloudEvent([FromBody] CloudEvent cloudEvent)
18+
{
19+
return Ok($"Received event with ID {cloudEvent.Id}, attributes: {JsonConvert.SerializeObject(cloudEvent.GetAttributes())}");
20+
}
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
4+
5+
namespace CloudNative.CloudEvents.AspNetCoreSample
6+
{
7+
using Microsoft.AspNetCore;
8+
using Microsoft.AspNetCore.Hosting;
9+
10+
public static class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
CreateWebHostBuilder(args).Build().Run();
15+
}
16+
17+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
18+
WebHost.CreateDefaultBuilder(args)
19+
.UseStartup<Startup>();
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:64908",
7+
"sslPort": 0
8+
}
9+
},
10+
"$schema": "http://json.schemastore.org/launchsettings.json",
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
}
18+
}
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
4+
5+
namespace CloudNative.CloudEvents.AspNetCoreSample
6+
{
7+
using CloudNative.CloudEvents;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Hosting;
13+
14+
public class Startup
15+
{
16+
public Startup(IConfiguration configuration)
17+
{
18+
Configuration = configuration;
19+
}
20+
21+
public IConfiguration Configuration { get; }
22+
23+
// This method gets called by the runtime. Use this method to add services to the container.
24+
public void ConfigureServices(IServiceCollection services)
25+
{
26+
services.AddControllers(opts =>
27+
{
28+
opts.InputFormatters.Insert(0, new CloudEventJsonInputFormatter());
29+
});
30+
}
31+
32+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
33+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
34+
{
35+
if (env.IsDevelopment())
36+
{
37+
app.UseDeveloperExceptionPage();
38+
}
39+
40+
app.UseRouting();
41+
42+
app.UseEndpoints(endpoints =>
43+
{
44+
endpoints.MapControllers();
45+
});
46+
}
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)