Skip to content

Commit

Permalink
Merge pull request #16 from Cysharp/UpgradeHostingAndNetCore
Browse files Browse the repository at this point in the history
Upgrade Microsoft.Extensions.Hosting and .NET Core
  • Loading branch information
neuecc authored Oct 3, 2019
2 parents f871920 + 4e3b2b9 commit 4b29a5a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) });
});

Expand Down
6 changes: 0 additions & 6 deletions sandbox/MultiContainedApp/MultiContainedApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
</ItemGroup>
Expand Down
6 changes: 0 additions & 6 deletions sandbox/SingleContainedApp/SingleContainedApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
</ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions sandbox/WebHostingApp/WebHostingApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MicroBatchFramework.WebHosting\MicroBatchFramework.WebHosting.csproj" />
<ProjectReference Include="..\..\src\MicroBatchFramework\MicroBatchFramework.csproj" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
21 changes: 10 additions & 11 deletions src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -74,26 +75,24 @@ public Task Invoke(HttpContext httpContext)
{
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
await stream.CopyToAsync(ms);
bytes = options.ResolveCustomResource(path, ms.ToArray());
}
}

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Swagger\SwaggerUI\*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
43 changes: 14 additions & 29 deletions src/MicroBatchFramework/BatchHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace MicroBatchFramework
{
Expand All @@ -21,7 +22,7 @@ public static class BatchHost
/// </summary>
/// <remarks>
/// The following defaults are applied to the returned <see cref="HostBuilder"/>:
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the NETCORE_ENVIRONMENT,
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the DOTNET_ENVIRONMENT,
/// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json',
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
/// load <see cref="IConfiguration"/> from environment variables,
Expand All @@ -35,7 +36,7 @@ public static class BatchHost
/// </summary>
/// <remarks>
/// The following defaults are applied to the returned <see cref="HostBuilder"/>:
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the NETCORE_ENVIRONMENT,
/// set the <see cref="IHostingEnvironment.EnvironmentName"/> to the DOTNET_ENVIRONMENT,
/// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json',
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
/// load <see cref="IConfiguration"/> from environment variables,
Expand Down Expand Up @@ -63,22 +64,22 @@ public static class BatchHost
/// <returns>The initialized <see cref="IHostBuilder"/>.</returns>
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;
}

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<string, string>(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) });
});

Expand All @@ -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<SimpleConsoleLoggerProvider>((category, level) =>
{
Expand Down
5 changes: 1 addition & 4 deletions src/MicroBatchFramework/MicroBatchFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -11,7 +11,6 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" version="2.2.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" version="4.5.2" />
<PackageReference Include="Utf8Json" version="1.3.7" />
</ItemGroup>
Expand Down

0 comments on commit 4b29a5a

Please sign in to comment.