Skip to content

Commit 3b78a26

Browse files
authored
Update ASP.NET Core sample to use minimal host (#283)
* Update ASP.NET Core sample to use minimal host Signed-off-by: Safia Abdalla <[email protected]>
1 parent 4fec77c commit 3b78a26

File tree

5 files changed

+80
-66
lines changed

5 files changed

+80
-66
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@HostAddress = https://localhost:5001
2+
3+
POST {{HostAddress}}/api/events/receive
4+
Content-Type: application/json
5+
CE-SpecVersion: 1.0
6+
CE-Type: "com.example.myevent"
7+
CE-Source: "urn:example-com:mysource:abc"
8+
CE-Id: "c457b7c5-c038-4be9-98b9-938cb64a4fbf"
9+
10+
{
11+
"message": "Hello world!"
12+
}
13+
14+
###
15+
16+
GET {{HostAddress}}/api/events/generate

samples/CloudNative.CloudEvents.AspNetCoreSample/Program.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

5-
using Microsoft.AspNetCore;
6-
using Microsoft.AspNetCore.Hosting;
5+
using CloudNative.CloudEvents.AspNetCoreSample;
6+
using Microsoft.AspNetCore.Builder;
7+
using CloudNative.CloudEvents.NewtonsoftJson;
8+
using Microsoft.Extensions.DependencyInjection;
79

8-
namespace CloudNative.CloudEvents.AspNetCoreSample
9-
{
10-
public static class Program
11-
{
12-
public static void Main(string[] args)
13-
{
14-
CreateWebHostBuilder(args).Build().Run();
15-
}
10+
var builder = WebApplication.CreateBuilder(args);
1611

17-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
18-
WebHost.CreateDefaultBuilder(args)
19-
.UseStartup<Startup>();
20-
}
21-
}
12+
builder.Services.AddControllers(opts =>
13+
opts.InputFormatters.Insert(0, new CloudEventJsonInputFormatter(new JsonEventFormatter())));
14+
15+
var app = builder.Build();
16+
17+
app.MapControllers();
18+
19+
app.Run();
20+
21+
// Generated `Program` class when using top-level statements
22+
// is internal by default. Make this `public` here for tests.
23+
public partial class Program { }

samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

samples/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Samples
2+
3+
This directory contains a sample ASP.NET Core application that exposes two endpoints for:
4+
5+
- Receiving a CloudEvent (`/api/events/receive`)
6+
- Generating a CloudEvent (`/api/events/generate`)
7+
8+
## Run the sample
9+
10+
To run the sample, execute the `dotnet run` command in the `CloudNative.CloudEvents.AspNetCoreSample` directory.
11+
12+
```shell
13+
dotnet run --framework net6.0
14+
```
15+
16+
After running the web service using the command above, there are three strategies for sending requests to the web service.
17+
18+
### Using the `HttpSend` tool
19+
20+
The `HttpSend` project provides a CLI tool for sending requests to the `/api/events/receive` endpoint exposed by the service. To use the tool, navigate to the `HttpSend` directory and execute the following command:
21+
22+
```shell
23+
dotnet run --framework net6.0 --url https://localhost:5001/api/events/receive
24+
```
25+
26+
### Using the `.http` file
27+
28+
The [CloudNative.CloudEvents.AspNetCore.http file](./CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.http) can be used to send requests to the web service. Native support for executing requests in `.http` file is provided in JetBrains Rider and Visual Studio. Support for sending requests in VS Code is provided via the [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client).
29+
30+
### Using the `curl` command
31+
32+
Requests to the web service can also be run using the `curl` command line tool.
33+
34+
```shell
35+
curl --request POST \
36+
--url https://localhost:5001/api/events/receive \
37+
--header 'ce-id: "c457b7c5-c038-4be9-98b9-938cb64a4fbf"' \
38+
--header 'ce-source: "urn:example-com:mysource:abc"' \
39+
--header 'ce-specversion: 1.0' \
40+
--header 'ce-type: "com.example.myevent"' \
41+
--header 'content-type: application/json' \
42+
--header 'user-agent: vscode-restclient' \
43+
--data '{"message": "Hello world!"}'
44+
```

test/CloudNative.CloudEvents.IntegrationTests/AspNetCore/CloudEventControllerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
namespace CloudNative.CloudEvents.IntegrationTests.AspNetCore
1515
{
16-
public class CloudEventControllerTests : IClassFixture<WebApplicationFactory<Startup>>
16+
public class CloudEventControllerTests : IClassFixture<WebApplicationFactory<Program>>
1717
{
18-
private readonly WebApplicationFactory<Startup> _factory;
18+
private readonly WebApplicationFactory<Program> _factory;
1919

20-
public CloudEventControllerTests(WebApplicationFactory<Startup> factory)
20+
public CloudEventControllerTests(WebApplicationFactory<Program> factory)
2121
{
2222
_factory = factory;
2323
}

0 commit comments

Comments
 (0)