diff --git a/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.csproj b/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.csproj index 0f05bc2..5c5448f 100644 --- a/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.csproj +++ b/samples/CloudNative.CloudEvents.AspNetCoreSample/CloudNative.CloudEvents.AspNetCoreSample.csproj @@ -1,15 +1,9 @@  - netcoreapp2.2 - InProcess + netcoreapp3.0 - - - - - diff --git a/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs b/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs index 31c8989..db3eb1f 100644 --- a/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs +++ b/samples/CloudNative.CloudEvents.AspNetCoreSample/Startup.cs @@ -9,6 +9,7 @@ namespace CloudNative.CloudEvents.AspNetCoreSample using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Hosting; public class Startup { @@ -22,21 +23,26 @@ namespace CloudNative.CloudEvents.AspNetCoreSample // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc(opts => + services.AddControllers(opts => { opts.InputFormatters.Insert(0, new CloudEventInputFormatter()); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } - app.UseMvc(); + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } } } diff --git a/src/CloudNative.CloudEvents.AspNetCore/CloudEventInputFormatter.cs b/src/CloudNative.CloudEvents.AspNetCore/CloudEventInputFormatter.cs index 0860156..d7607dc 100644 --- a/src/CloudNative.CloudEvents.AspNetCore/CloudEventInputFormatter.cs +++ b/src/CloudNative.CloudEvents.AspNetCore/CloudEventInputFormatter.cs @@ -21,7 +21,7 @@ namespace CloudNative.CloudEvents SupportedEncodings.Add(Encoding.Unicode); } - public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) + public override async Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { if (context == null) { @@ -37,12 +37,12 @@ namespace CloudNative.CloudEvents try { - var cloudEvent = request.ToCloudEvent(); - return InputFormatterResult.SuccessAsync(cloudEvent); + var cloudEvent = await request.ReadCloudEventAsync(); + return await InputFormatterResult.SuccessAsync(cloudEvent); } catch (Exception) { - return InputFormatterResult.FailureAsync(); + return await InputFormatterResult.FailureAsync(); } } diff --git a/src/CloudNative.CloudEvents.AspNetCore/HttpRequestExtension.cs b/src/CloudNative.CloudEvents.AspNetCore/HttpRequestExtension.cs index 4f25582..e440cee 100644 --- a/src/CloudNative.CloudEvents.AspNetCore/HttpRequestExtension.cs +++ b/src/CloudNative.CloudEvents.AspNetCore/HttpRequestExtension.cs @@ -9,6 +9,7 @@ namespace CloudNative.CloudEvents using Newtonsoft.Json; using System; using System.Net.Mime; + using System.Threading.Tasks; public static class HttpRequestExtension { @@ -26,10 +27,10 @@ namespace CloudNative.CloudEvents /// HTTP request /// List of extension instances /// A CloudEvent instance or 'null' if the request message doesn't hold a CloudEvent - public static CloudEvent ToCloudEvent(this HttpRequest httpRequest, + public static ValueTask ReadCloudEventAsync(this HttpRequest httpRequest, params ICloudEventExtension[] extensions) { - return ToCloudEvent(httpRequest, null, extensions); + return ReadCloudEventAsync(httpRequest, null, extensions); } /// @@ -40,7 +41,7 @@ namespace CloudNative.CloudEvents /// /// List of extension instances /// A CloudEvent instance or 'null' if the request message doesn't hold a CloudEvent - public static CloudEvent ToCloudEvent(this HttpRequest httpRequest, + public static async ValueTask ReadCloudEventAsync(this HttpRequest httpRequest, ICloudEventFormatter formatter = null, params ICloudEventExtension[] extensions) { @@ -63,7 +64,7 @@ namespace CloudNative.CloudEvents } } - return formatter.DecodeStructuredEvent(httpRequest.Body, extensions); + return await formatter.DecodeStructuredEventAsync(httpRequest.Body, extensions); } else { @@ -103,7 +104,9 @@ namespace CloudNative.CloudEvents if (httpRequestHeader.StartsWith(HttpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase)) { string headerValue = httpRequest.Headers[httpRequestHeader]; - if (headerValue.StartsWith("{") && headerValue.EndsWith("}") || + // maps in headers have been abolished in 1.0 + if (version != CloudEventsSpecVersion.V1_0 && + headerValue.StartsWith("{") && headerValue.EndsWith("}") || headerValue.StartsWith("[") && headerValue.EndsWith("]")) { attributes[httpRequestHeader.Substring(3)] = @@ -112,7 +115,6 @@ namespace CloudNative.CloudEvents else { attributes[httpRequestHeader.Substring(3)] = headerValue; - attributes[httpRequestHeader.Substring(3)] = headerValue; } } } @@ -124,6 +126,5 @@ namespace CloudNative.CloudEvents return cloudEvent; } } - } } diff --git a/src/CloudNative.CloudEvents/ICloudEventFormatter.cs b/src/CloudNative.CloudEvents/ICloudEventFormatter.cs index 8f6245b..3735c71 100644 --- a/src/CloudNative.CloudEvents/ICloudEventFormatter.cs +++ b/src/CloudNative.CloudEvents/ICloudEventFormatter.cs @@ -7,6 +7,7 @@ namespace CloudNative.CloudEvents using System.Collections.Generic; using System.IO; using System.Net.Mime; + using System.Threading.Tasks; /// @@ -22,6 +23,13 @@ namespace CloudNative.CloudEvents /// CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensions); /// + /// Decode a structured event from a stream asynchonously + /// + /// + /// + /// + Task DecodeStructuredEventAsync(Stream data, IEnumerable extensions); + /// /// Decode a structured event from a byte array /// /// diff --git a/src/CloudNative.CloudEvents/JsonEventFormatter.cs b/src/CloudNative.CloudEvents/JsonEventFormatter.cs index f62c936..4ddf378 100644 --- a/src/CloudNative.CloudEvents/JsonEventFormatter.cs +++ b/src/CloudNative.CloudEvents/JsonEventFormatter.cs @@ -5,12 +5,12 @@ namespace CloudNative.CloudEvents { using System; - using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mime; using System.Text; + using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -27,6 +27,13 @@ namespace CloudNative.CloudEvents return DecodeStructuredEvent(data, (IEnumerable)extensions); } + public async Task DecodeStructuredEventAsync(Stream data, IEnumerable extensions) + { + var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true)); + var jObject = await JObject.LoadAsync(jsonReader); + return DecodeJObject(jObject, extensions); + } + public CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensions = null) { var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true)); diff --git a/test/CloudNative.CloudEvents.IntegrationTests/CloudNative.CloudEvents.IntegrationTests.csproj b/test/CloudNative.CloudEvents.IntegrationTests/CloudNative.CloudEvents.IntegrationTests.csproj index bce3d2e..0a32970 100644 --- a/test/CloudNative.CloudEvents.IntegrationTests/CloudNative.CloudEvents.IntegrationTests.csproj +++ b/test/CloudNative.CloudEvents.IntegrationTests/CloudNative.CloudEvents.IntegrationTests.csproj @@ -1,14 +1,14 @@  - netcoreapp2.2 + netcoreapp3.0 false - - + +