Skip to content

Commit 4b29a5a

Browse files
authored
Merge pull request #16 from Cysharp/UpgradeHostingAndNetCore
Upgrade Microsoft.Extensions.Hosting and .NET Core
2 parents f871920 + 4e3b2b9 commit 4b29a5a

File tree

11 files changed

+39
-75
lines changed

11 files changed

+39
-75
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2.1
22
executors:
33
dotnet:
44
docker:
5-
- image: mcr.microsoft.com/dotnet/core/sdk:2.2
5+
- image: mcr.microsoft.com/dotnet/core/sdk:3.0
66
environment:
77
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
88
NUGET_XMLDOC_MODE: skip

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Loc
422422
// set the host configuration
423423
builder.ConfigureHostConfiguration(config =>
424424
{
425-
config.AddEnvironmentVariables(prefix: "NETCORE_");
425+
config.AddEnvironmentVariables(prefix: "DOTNET_");
426426
config.AddInMemoryCollection(new[] { new KeyValuePair<string, string>(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) });
427427
});
428428

sandbox/MultiContainedApp/MultiContainedApp.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
<LangVersion>7.3</LangVersion>
77
</PropertyGroup>
88

9-
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
11-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
12-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
13-
</ItemGroup>
14-
159
<ItemGroup>
1610
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
1711
</ItemGroup>

sandbox/SingleContainedApp/SingleContainedApp.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
</Content>
1717
</ItemGroup>
1818

19-
<ItemGroup>
20-
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
21-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
22-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
23-
</ItemGroup>
24-
2519
<ItemGroup>
2620
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
2721
</ItemGroup>

sandbox/SingleContainedAppWithConfig/SingleContainedAppWithConfig.csproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@
2828
</Content>
2929
</ItemGroup>
3030

31-
<ItemGroup>
32-
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
33-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
34-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
35-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
36-
</ItemGroup>
37-
3831
<ItemGroup>
3932
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
4033
</ItemGroup>

sandbox/WebHostingApp/WebHostingApp.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
66
<LangVersion>7.3</LangVersion>
77
</PropertyGroup>
88

9-
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
11-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
12-
</ItemGroup>
139
<ItemGroup>
1410
<ProjectReference Include="..\..\src\MicroBatchFramework.WebHosting\MicroBatchFramework.WebHosting.csproj" />
1511
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
1612
</ItemGroup>
13+
14+
<ItemGroup>
15+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
16+
</ItemGroup>
1717
</Project>

src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public BatchEngineSwaggerMiddleware(RequestDelegate next, TargetBatchTypeCollect
2626
this.options = options;
2727
}
2828

29-
public Task Invoke(HttpContext httpContext)
29+
public async Task Invoke(HttpContext httpContext)
3030
{
3131
// reference embedded resouces
3232
const string prefix = "MicroBatchFramework.WebHosting.Swagger.SwaggerUI.";
@@ -42,8 +42,8 @@ public Task Invoke(HttpContext httpContext)
4242
var bytes = builder.BuildSwaggerJson();
4343
httpContext.Response.Headers["Content-Type"] = new[] { "application/json" };
4444
httpContext.Response.StatusCode = 200;
45-
httpContext.Response.Body.Write(bytes, 0, bytes.Length);
46-
return EmptyTask;
45+
await httpContext.Response.Body.WriteAsync(bytes, 0, bytes.Length);
46+
return;
4747
}
4848

4949
var myAssembly = typeof(BatchEngineSwaggerMiddleware).GetTypeInfo().Assembly;
@@ -55,13 +55,14 @@ public Task Invoke(HttpContext httpContext)
5555
if (stream == null)
5656
{
5757
// not found, standard request.
58-
return next(httpContext);
58+
await next(httpContext);
59+
return;
5960
}
6061

6162
httpContext.Response.Headers["Content-Type"] = new[] { mediaType };
6263
httpContext.Response.StatusCode = 200;
6364
var response = httpContext.Response.Body;
64-
stream.CopyTo(response);
65+
await stream.CopyToAsync(response);
6566
}
6667
else
6768
{
@@ -74,26 +75,24 @@ public Task Invoke(HttpContext httpContext)
7475
{
7576
using (var ms = new MemoryStream())
7677
{
77-
stream.CopyTo(ms);
78+
await stream.CopyToAsync(ms);
7879
bytes = options.ResolveCustomResource(path, ms.ToArray());
7980
}
8081
}
8182

8283
if (bytes == null)
8384
{
8485
// not found, standard request.
85-
return next(httpContext);
86+
await next(httpContext);
87+
return;
8688
}
8789

8890
httpContext.Response.Headers["Content-Type"] = new[] { mediaType };
8991
httpContext.Response.StatusCode = 200;
9092
var response = httpContext.Response.Body;
91-
response.Write(bytes, 0, bytes.Length);
93+
await response.WriteAsync(bytes, 0, bytes.Length);
9294
}
9395
}
94-
95-
96-
return EmptyTask;
9796
}
9897

9998
static string GetMediaType(string path)

src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
88
<EmbeddedResource Include="Swagger\SwaggerUI\*" />
99
</ItemGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
13-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
12+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
1417
</ItemGroup>
1518

1619
<ItemGroup>

src/MicroBatchFramework/BatchHost.cs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Reflection;
99
using System.Collections.Generic;
10+
using System.Linq;
1011

1112
namespace MicroBatchFramework
1213
{
@@ -21,7 +22,7 @@ public static class BatchHost
2122
/// </summary>
2223
/// <remarks>
2324
/// The following defaults are applied to the returned <see cref="HostBuilder"/>:
24-
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the NETCORE_ENVIRONMENT,
25+
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the DOTNET_ENVIRONMENT,
2526
/// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json',
2627
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
2728
/// load <see cref="IConfiguration"/> from environment variables,
@@ -35,7 +36,7 @@ public static class BatchHost
3536
/// </summary>
3637
/// <remarks>
3738
/// The following defaults are applied to the returned <see cref="HostBuilder"/>:
38-
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the NETCORE_ENVIRONMENT,
39+
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the DOTNET_ENVIRONMENT,
3940
/// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json',
4041
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
4142
/// load <see cref="IConfiguration"/> from environment variables,
@@ -63,22 +64,22 @@ public static class BatchHost
6364
/// <returns>The initialized <see cref="IHostBuilder"/>.</returns>
6465
public static IHostBuilder CreateDefaultBuilder(bool useSimpleConsoleLogger, LogLevel minSimpleConsoleLoggerLogLevel, string hostEnvironmentVariable)
6566
{
66-
var builder = new HostBuilder();
67+
var builder = Host.CreateDefaultBuilder();
6768

6869
ConfigureHostConfigurationDefault(builder, hostEnvironmentVariable);
69-
ConfigureAppConfigurationDefault(builder);
7070
ConfigureLoggingDefault(builder, useSimpleConsoleLogger, minSimpleConsoleLoggerLogLevel);
7171

7272
return builder;
7373
}
7474

7575
internal static void ConfigureHostConfigurationDefault(IHostBuilder builder, string hostEnvironmentVariable)
7676
{
77-
builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
78-
7977
builder.ConfigureHostConfiguration(config =>
8078
{
79+
// NOTE: This is backward compatibility for v1.3.0 or before.
80+
// It's strongly recommended to use "DOTNET_" prefix expected by GenericHost. (e.g. DOTNET_ENVIRONMENT)
8181
config.AddEnvironmentVariables(prefix: "NETCORE_");
82+
8283
config.AddInMemoryCollection(new[] { new KeyValuePair<string, string>(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) });
8384
});
8485

@@ -88,35 +89,19 @@ internal static void ConfigureHostConfigurationDefault(IHostBuilder builder, str
8889
}
8990
}
9091

91-
internal static void ConfigureAppConfigurationDefault(IHostBuilder builder)
92-
{
93-
builder.ConfigureAppConfiguration((hostingContext, config) =>
94-
{
95-
var env = hostingContext.HostingEnvironment;
96-
97-
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
98-
config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
99-
100-
if (env.IsDevelopment())
101-
{
102-
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
103-
if (appAssembly != null)
104-
{
105-
// use https://marketplace.visualstudio.com/items?itemName=guitarrapc.OpenUserSecrets to easily manage UserSecrets with GenericHost.
106-
config.AddUserSecrets(appAssembly, optional: true);
107-
}
108-
}
109-
110-
config.AddEnvironmentVariables();
111-
});
112-
}
113-
11492
internal static void ConfigureLoggingDefault(IHostBuilder builder, bool useSimpleConsoleLogger, LogLevel minSimpleConsoleLoggerLogLevel)
11593
{
11694
if (useSimpleConsoleLogger)
11795
{
11896
builder.ConfigureLogging(logging =>
11997
{
98+
// Use SimpleConsoleLogger instead of the default ConsoleLogger.
99+
var consoleLogger = logging.Services.FirstOrDefault(x => x.ImplementationType?.FullName == "Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider");
100+
if (consoleLogger != null)
101+
{
102+
logging.Services.Remove(consoleLogger);
103+
}
104+
120105
logging.AddSimpleConsole();
121106
logging.AddFilter<SimpleConsoleLoggerProvider>((category, level) =>
122107
{

src/MicroBatchFramework/MicroBatchFramework.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
</PropertyGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" />
30-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
31-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
32-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
29+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
3330
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" />
3431
<PackageReference Include="Utf8Json" Version="1.3.7" />
3532
</ItemGroup>

0 commit comments

Comments
 (0)