From 8be684aa3d7d3bd7a1dcb979181eb5e5a0d1a1ab Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 2 Oct 2019 12:54:23 +0900 Subject: [PATCH 1/5] Upgrade Microsoft.Extensions.Hosting to 3.0.0. BatchHost's builder is now using default GenericHost's builder. --- .../MultiContainedApp.csproj | 6 --- .../SingleContainedApp.csproj | 6 --- .../SingleContainedAppWithConfig.csproj | 7 ---- src/MicroBatchFramework/BatchHost.cs | 39 ++++++------------- .../MicroBatchFramework.csproj | 5 +-- .../MicroBatchFramework.Tests.csproj | 1 - 6 files changed, 13 insertions(+), 51 deletions(-) diff --git a/sandbox/MultiContainedApp/MultiContainedApp.csproj b/sandbox/MultiContainedApp/MultiContainedApp.csproj index 72d8504..eb52b4a 100644 --- a/sandbox/MultiContainedApp/MultiContainedApp.csproj +++ b/sandbox/MultiContainedApp/MultiContainedApp.csproj @@ -6,12 +6,6 @@ 7.3 - - - - - - diff --git a/sandbox/SingleContainedApp/SingleContainedApp.csproj b/sandbox/SingleContainedApp/SingleContainedApp.csproj index d566b2d..4e3f186 100644 --- a/sandbox/SingleContainedApp/SingleContainedApp.csproj +++ b/sandbox/SingleContainedApp/SingleContainedApp.csproj @@ -16,12 +16,6 @@ - - - - - - diff --git a/sandbox/SingleContainedAppWithConfig/SingleContainedAppWithConfig.csproj b/sandbox/SingleContainedAppWithConfig/SingleContainedAppWithConfig.csproj index 76b5c98..0292440 100644 --- a/sandbox/SingleContainedAppWithConfig/SingleContainedAppWithConfig.csproj +++ b/sandbox/SingleContainedAppWithConfig/SingleContainedAppWithConfig.csproj @@ -28,13 +28,6 @@ - - - - - - - diff --git a/src/MicroBatchFramework/BatchHost.cs b/src/MicroBatchFramework/BatchHost.cs index d86e53c..719396f 100644 --- a/src/MicroBatchFramework/BatchHost.cs +++ b/src/MicroBatchFramework/BatchHost.cs @@ -7,6 +7,7 @@ using System.IO; using System.Reflection; using System.Collections.Generic; +using System.Linq; namespace MicroBatchFramework { @@ -63,10 +64,9 @@ public static class BatchHost /// The initialized . public static IHostBuilder CreateDefaultBuilder(bool useSimpleConsoleLogger, LogLevel minSimpleConsoleLoggerLogLevel, string hostEnvironmentVariable) { - var builder = new HostBuilder(); + var builder = Host.CreateDefaultBuilder(); ConfigureHostConfigurationDefault(builder, hostEnvironmentVariable); - ConfigureAppConfigurationDefault(builder); ConfigureLoggingDefault(builder, useSimpleConsoleLogger, minSimpleConsoleLoggerLogLevel); return builder; @@ -74,11 +74,12 @@ public static IHostBuilder CreateDefaultBuilder(bool useSimpleConsoleLogger, Log internal static void ConfigureHostConfigurationDefault(IHostBuilder builder, string hostEnvironmentVariable) { - builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); - builder.ConfigureHostConfiguration(config => { + // NOTE: This is backward compatibility for v1.3.0 or before. + // It's strongly recommended to use "DOTNET_" prefix expected by GenericHost. (e.g. DOTNET_ENVIRONMENT) config.AddEnvironmentVariables(prefix: "NETCORE_"); + config.AddInMemoryCollection(new[] { new KeyValuePair(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) }); }); @@ -88,35 +89,19 @@ internal static void ConfigureHostConfigurationDefault(IHostBuilder builder, str } } - internal static void ConfigureAppConfigurationDefault(IHostBuilder builder) - { - builder.ConfigureAppConfiguration((hostingContext, config) => - { - var env = hostingContext.HostingEnvironment; - - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); - config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); - - if (env.IsDevelopment()) - { - var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); - if (appAssembly != null) - { - // use https://marketplace.visualstudio.com/items?itemName=guitarrapc.OpenUserSecrets to easily manage UserSecrets with GenericHost. - config.AddUserSecrets(appAssembly, optional: true); - } - } - - config.AddEnvironmentVariables(); - }); - } - internal static void ConfigureLoggingDefault(IHostBuilder builder, bool useSimpleConsoleLogger, LogLevel minSimpleConsoleLoggerLogLevel) { if (useSimpleConsoleLogger) { builder.ConfigureLogging(logging => { + // Use SimpleConsoleLogger instead of the default ConsoleLogger. + var consoleLogger = logging.Services.FirstOrDefault(x => x.ImplementationType?.FullName == "Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider"); + if (consoleLogger != null) + { + logging.Services.Remove(consoleLogger); + } + logging.AddSimpleConsole(); logging.AddFilter((category, level) => { diff --git a/src/MicroBatchFramework/MicroBatchFramework.csproj b/src/MicroBatchFramework/MicroBatchFramework.csproj index 04b811a..b5a7ab5 100644 --- a/src/MicroBatchFramework/MicroBatchFramework.csproj +++ b/src/MicroBatchFramework/MicroBatchFramework.csproj @@ -26,10 +26,7 @@ - - - - + diff --git a/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj b/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj index f6f1a3c..f8d2781 100644 --- a/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj +++ b/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj @@ -11,7 +11,6 @@ - From 8c081376979d6a62ca41dce8a27f06adc09ff79d Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 2 Oct 2019 12:54:53 +0900 Subject: [PATCH 2/5] WebHosting is now running with ASP.NET Core 3.0 --- sandbox/WebHostingApp/WebHostingApp.csproj | 10 ++++----- .../BatchEngineSwaggerMiddleware.cs | 21 +++++++++---------- .../MicroBatchFramework.WebHosting.csproj | 9 +++++--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/sandbox/WebHostingApp/WebHostingApp.csproj b/sandbox/WebHostingApp/WebHostingApp.csproj index 68ad23c..e4caf7b 100644 --- a/sandbox/WebHostingApp/WebHostingApp.csproj +++ b/sandbox/WebHostingApp/WebHostingApp.csproj @@ -2,16 +2,16 @@ Exe - netcoreapp2.1 + netcoreapp3.0 7.3 - - - - + + + + diff --git a/src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs b/src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs index 894c4b8..d314503 100644 --- a/src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs +++ b/src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs @@ -26,7 +26,7 @@ public BatchEngineSwaggerMiddleware(RequestDelegate next, TargetBatchTypeCollect this.options = options; } - public Task Invoke(HttpContext httpContext) + public async Task Invoke(HttpContext httpContext) { // reference embedded resouces const string prefix = "MicroBatchFramework.WebHosting.Swagger.SwaggerUI."; @@ -42,8 +42,8 @@ public Task Invoke(HttpContext httpContext) var bytes = builder.BuildSwaggerJson(); httpContext.Response.Headers["Content-Type"] = new[] { "application/json" }; httpContext.Response.StatusCode = 200; - httpContext.Response.Body.Write(bytes, 0, bytes.Length); - return EmptyTask; + await httpContext.Response.Body.WriteAsync(bytes, 0, bytes.Length); + return; } var myAssembly = typeof(BatchEngineSwaggerMiddleware).GetTypeInfo().Assembly; @@ -55,13 +55,14 @@ public Task Invoke(HttpContext httpContext) if (stream == null) { // not found, standard request. - return next(httpContext); + await next(httpContext); + return; } httpContext.Response.Headers["Content-Type"] = new[] { mediaType }; httpContext.Response.StatusCode = 200; var response = httpContext.Response.Body; - stream.CopyTo(response); + await stream.CopyToAsync(response); } else { @@ -74,7 +75,7 @@ public Task Invoke(HttpContext httpContext) { using (var ms = new MemoryStream()) { - stream.CopyTo(ms); + await stream.CopyToAsync(ms); bytes = options.ResolveCustomResource(path, ms.ToArray()); } } @@ -82,18 +83,16 @@ public Task Invoke(HttpContext httpContext) if (bytes == null) { // not found, standard request. - return next(httpContext); + await next(httpContext); + return; } httpContext.Response.Headers["Content-Type"] = new[] { mediaType }; httpContext.Response.StatusCode = 200; var response = httpContext.Response.Body; - response.Write(bytes, 0, bytes.Length); + await response.WriteAsync(bytes, 0, bytes.Length); } } - - - return EmptyTask; } static string GetMediaType(string path) diff --git a/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj b/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj index efb372b..e7699f9 100644 --- a/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj +++ b/src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netcoreapp3.0 @@ -9,8 +9,11 @@ - - + + + + + From 26e9fa8bde953489a3196d6634f4ccee8439680b Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 2 Oct 2019 14:11:29 +0900 Subject: [PATCH 3/5] Use .NET Core 3.0 SDK --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 99ecd91..5866c14 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2.1 executors: dotnet: docker: - - image: mcr.microsoft.com/dotnet/core/sdk:2.2 + - image: mcr.microsoft.com/dotnet/core/sdk:3.0 environment: DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true NUGET_XMLDOC_MODE: skip From aa7dc923ae56090e5115355ac66a63889dd29cf3 Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 2 Oct 2019 15:23:22 +0900 Subject: [PATCH 4/5] Use "DOTNET_" prefix. --- ReadMe.md | 2 +- src/MicroBatchFramework/BatchHost.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 4772a2d..983d7ac 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -422,7 +422,7 @@ builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Loc // set the host configuration builder.ConfigureHostConfiguration(config => { - config.AddEnvironmentVariables(prefix: "NETCORE_"); + config.AddEnvironmentVariables(prefix: "DOTNET_"); config.AddInMemoryCollection(new[] { new KeyValuePair(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) }); }); diff --git a/src/MicroBatchFramework/BatchHost.cs b/src/MicroBatchFramework/BatchHost.cs index 719396f..4b5aae3 100644 --- a/src/MicroBatchFramework/BatchHost.cs +++ b/src/MicroBatchFramework/BatchHost.cs @@ -22,7 +22,7 @@ public static class BatchHost /// /// /// The following defaults are applied to the returned : - /// set the to the NETCORE_ENVIRONMENT, + /// set the to the DOTNET_ENVIRONMENT, /// load from 'appsettings.json' and 'appsettings.[].json', /// load from User Secrets when is 'Development' using the entry assembly, /// load from environment variables, @@ -36,7 +36,7 @@ public static class BatchHost /// /// /// The following defaults are applied to the returned : - /// set the to the NETCORE_ENVIRONMENT, + /// set the to the DOTNET_ENVIRONMENT, /// load from 'appsettings.json' and 'appsettings.[].json', /// load from User Secrets when is 'Development' using the entry assembly, /// load from environment variables, From 4e3b2b9a9c92200c35554f0c277a770ca67562e0 Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 2 Oct 2019 15:25:22 +0900 Subject: [PATCH 5/5] Upgrade TargetFramework of the tests project. --- .../MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj b/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj index f8d2781..93b548c 100644 --- a/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj +++ b/tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp3.0 false