Skip to content

Commit 8be684a

Browse files
committed
Upgrade Microsoft.Extensions.Hosting to 3.0.0.
BatchHost's builder is now using default GenericHost's builder.
1 parent f871920 commit 8be684a

File tree

6 files changed

+13
-51
lines changed

6 files changed

+13
-51
lines changed

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>

src/MicroBatchFramework/BatchHost.cs

Lines changed: 12 additions & 27 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
{
@@ -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>

tests/MicroBatchFramework.Tests/MicroBatchFramework.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
1212
<PackageReference Include="xunit" Version="2.4.1" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
14-
<PackageReference Include="Microsoft.Extensions.Hosting" version="2.2.0" />
1514
<PackageReference Include="System.Threading.Tasks.Extensions" version="4.5.2" />
1615
<PackageReference Include="Utf8Json" version="1.3.7" />
1716
</ItemGroup>

0 commit comments

Comments
 (0)