From 3b78a265f52974b628c3bb305e6af3c41911f4c0 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 6 Mar 2024 10:58:15 -0800 Subject: [PATCH] Update ASP.NET Core sample to use minimal host (#283) * Update ASP.NET Core sample to use minimal host Signed-off-by: Safia Abdalla --- ...udNative.CloudEvents.AspNetCoreSample.http | 16 +++++++ .../Program.cs | 32 +++++++------ .../Startup.cs | 48 ------------------- samples/README.md | 44 +++++++++++++++++ .../AspNetCore/CloudEventControllerTests.cs | 6 +-- 5 files changed, 80 insertions(+), 66 deletions(-) create mode 100644 samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.http delete mode 100644 samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs create mode 100644 samples/README.md diff --git a/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.http b/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.http new file mode 100644 index 0000000..9eec593 --- /dev/null +++ b/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.http @@ -0,0 +1,16 @@ +@HostAddress = https://localhost:5001 + +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" + +{ + "message": "Hello world!" +} + +### + +GET {{HostAddress}}/api/events/generate diff --git a/samples/CloudNative.CloudEvents.AspNetCoreSample/Program.cs b/samples/CloudNative.CloudEvents.AspNetCoreSample/Program.cs index 7d4e52a..c75e741 100644 --- a/samples/CloudNative.CloudEvents.AspNetCoreSample/Program.cs +++ b/samples/CloudNative.CloudEvents.AspNetCoreSample/Program.cs @@ -2,20 +2,22 @@ // Licensed under the Apache 2.0 license. // See LICENSE file in the project root for full license information. -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using CloudNative.CloudEvents.AspNetCoreSample; +using Microsoft.AspNetCore.Builder; +using CloudNative.CloudEvents.NewtonsoftJson; +using Microsoft.Extensions.DependencyInjection; -namespace CloudNative.CloudEvents.AspNetCoreSample -{ - public static class Program - { - public static void Main(string[] args) - { - CreateWebHostBuilder(args).Build().Run(); - } +var builder = WebApplication.CreateBuilder(args); - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); - } -} +builder.Services.AddControllers(opts => + opts.InputFormatters.Insert(0, new CloudEventJsonInputFormatter(new JsonEventFormatter()))); + +var app = builder.Build(); + +app.MapControllers(); + +app.Run(); + +// Generated `Program` class when using top-level statements +// is internal by default. Make this `public` here for tests. +public partial class Program { } diff --git a/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs b/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs deleted file mode 100644 index 719a30d..0000000 --- a/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs +++ /dev/null @@ -1,48 +0,0 @@ -// 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.NewtonsoftJson; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace CloudNative.CloudEvents.AspNetCoreSample -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddControllers(opts => - { - opts.InputFormatters.Insert(0, new CloudEventJsonInputFormatter(new JsonEventFormatter())); - }); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseRouting(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - } - } -} diff --git a/samples/README.md b/samples/README.md new file mode 100644 index 0000000..cf73e16 --- /dev/null +++ b/samples/README.md @@ -0,0 +1,44 @@ +# Samples + +This directory contains a sample ASP.NET Core application that exposes two endpoints for: + +- Receiving a CloudEvent (`/api/events/receive`) +- Generating a CloudEvent (`/api/events/generate`) + +## Run the sample + +To run the sample, execute the `dotnet run` command in the `CloudNative.CloudEvents.AspNetCoreSample` directory. + +```shell +dotnet run --framework net6.0 +``` + +After running the web service using the command above, there are three strategies for sending requests to the web service. + +### Using the `HttpSend` tool + +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: + +```shell +dotnet run --framework net6.0 --url https://localhost:5001/api/events/receive +``` + +### Using the `.http` file + +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). + +### Using the `curl` command + +Requests to the web service can also be run using the `curl` command line tool. + +```shell +curl --request POST \ + --url https://localhost:5001/api/events/receive \ + --header 'ce-id: "c457b7c5-c038-4be9-98b9-938cb64a4fbf"' \ + --header 'ce-source: "urn:example-com:mysource:abc"' \ + --header 'ce-specversion: 1.0' \ + --header 'ce-type: "com.example.myevent"' \ + --header 'content-type: application/json' \ + --header 'user-agent: vscode-restclient' \ + --data '{"message": "Hello world!"}' +``` diff --git a/test/CloudNative.CloudEvents.IntegrationTests/AspNetCore/CloudEventControllerTests.cs b/test/CloudNative.CloudEvents.IntegrationTests/AspNetCore/CloudEventControllerTests.cs index 2de9ae4..b59ecf2 100644 --- a/test/CloudNative.CloudEvents.IntegrationTests/AspNetCore/CloudEventControllerTests.cs +++ b/test/CloudNative.CloudEvents.IntegrationTests/AspNetCore/CloudEventControllerTests.cs @@ -13,11 +13,11 @@ using Xunit; namespace CloudNative.CloudEvents.IntegrationTests.AspNetCore { - public class CloudEventControllerTests : IClassFixture> + public class CloudEventControllerTests : IClassFixture> { - private readonly WebApplicationFactory _factory; + private readonly WebApplicationFactory _factory; - public CloudEventControllerTests(WebApplicationFactory factory) + public CloudEventControllerTests(WebApplicationFactory factory) { _factory = factory; }