Skip to content

Commit 5402232

Browse files
authored
🚀 Upgrading to NET50. (#2)
1 parent 6d4eac9 commit 5402232

File tree

19 files changed

+173
-111
lines changed

19 files changed

+173
-111
lines changed

.editorconfig

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ dotnet_naming_symbols.constant_fields.required_modifiers = const
7272

7373
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
7474

75-
# static fields should have s_ prefix
76-
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
77-
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
78-
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
79-
80-
dotnet_naming_symbols.static_fields.applicable_kinds = field
81-
dotnet_naming_symbols.static_fields.required_modifiers = static
82-
83-
dotnet_naming_style.static_prefix_style.required_prefix = s_
84-
dotnet_naming_style.static_prefix_style.capitalization = camel_case
85-
8675
# internal and private fields should be _camelCase
8776
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
8877
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<h1 align="center">Simple: Hosting</h1>
22

33
<div align="center">
4-
Making it simple to <i>customize</i> Hosting for your .NET Core 3.x+ application
4+
Making it simple to <i>customize</i> Hosting for your .NET Core 5.x+ application
55
</div>
66

77
<br />
@@ -26,7 +26,7 @@
2626
- :rocket: Reduces boilerplate ceremony for your `program.cs` file.
2727
- :white_check_mark: Sets up Serilog _around_ the entire application. (:wrench: Configure all settings via your `appsettings.json` file(s))
2828
- :white_check_mark: Simple to add some extra (helpful) log header/footer.
29-
- :white_check_mark: Can still easily create a Web Host or BackgroundService Host.
29+
- :white_check_mark: Can also add logging to your `Startup` class (new with ASP.NET Core 5+)
3030

3131
In summary: this library makes is <b>SIMPLE</b> (by abstracting away most of the boring ceremony) to setup your .NET Core application.
3232

@@ -66,16 +66,7 @@ public static Task Main(string[] args)
6666
FirstLoggingInformationMessage = "~~ Sample Web Application ~~",
6767
LogAssemblyInformation = true,
6868
LastLoggingInformationMessage = "-- Sample Web Application has ended/terminated --",
69-
70-
CustomPreHostRunAsyncAction = new Action<IHost>(host =>
71-
{
72-
using (var scope = host.Services.CreateScope())
73-
{
74-
var services = scope.ServiceProvider;
75-
var logger = services.GetRequiredService<ILogger<Program>>();
76-
logger.LogInformation($"Inside the {nameof(MainOptions.CustomPreHostRunAsyncAction)} method. Woot!");
77-
}
78-
})
69+
StartupActivation = new System.Func<WebHostBuilderContext, ILogger, Startup>((context, logger) => new Startup(context.Configuration, logger))
7970
};
8071
8172
return SimpleHosting.Program.Main<Startup>(options);

SimpleHosting.sln

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ VisualStudioVersion = 16.0.28721.148
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDomination.SimpleHosting", "src\WorldDomination.SimpleHosting\WorldDomination.SimpleHosting.csproj", "{112DA850-B52E-49B6-811D-73237C5A7792}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDomination.SimpleHosting.SampleHostedServiceApplication", "src\WorldDomination.SimpleHosting.SampleHostedServiceApplication\WorldDomination.SimpleHosting.SampleHostedServiceApplication.csproj", "{4AA442D6-5611-408D-9CF2-CCDA63BA21D0}"
9-
EndProject
108
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDomination.SimpleHosting.SampleWebApplication", "src\WorldDomination.SimpleHosting.SampleWebApplication\WorldDomination.SimpleHosting.SampleWebApplication.csproj", "{013F5235-D334-4770-82E4-350BA00F676C}"
119
EndProject
1210
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorldDomination.SimpleHosting.SampleWebApplication.Tests", "src\WorldDomination.SimpleHosting.SampleWebApplication.Tests\WorldDomination.SimpleHosting.SampleWebApplication.Tests.csproj", "{BB527810-A846-43A8-8AD8-A1C2B191C2AE}"
1311
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{3F8381EE-2CFD-4533-8141-5C6EA41E3275}"
13+
ProjectSection(SolutionItems) = preProject
14+
.editorconfig = .editorconfig
15+
appveyor.yml = appveyor.yml
16+
global.json = global.json
17+
README.md = README.md
18+
EndProjectSection
19+
EndProject
20+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B77C557B-74CB-457F-AB66-B0BBCF93FA5C}"
21+
EndProject
1422
Global
1523
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1624
Debug|Any CPU = Debug|Any CPU
@@ -21,10 +29,6 @@ Global
2129
{112DA850-B52E-49B6-811D-73237C5A7792}.Debug|Any CPU.Build.0 = Debug|Any CPU
2230
{112DA850-B52E-49B6-811D-73237C5A7792}.Release|Any CPU.ActiveCfg = Release|Any CPU
2331
{112DA850-B52E-49B6-811D-73237C5A7792}.Release|Any CPU.Build.0 = Release|Any CPU
24-
{4AA442D6-5611-408D-9CF2-CCDA63BA21D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25-
{4AA442D6-5611-408D-9CF2-CCDA63BA21D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{4AA442D6-5611-408D-9CF2-CCDA63BA21D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
27-
{4AA442D6-5611-408D-9CF2-CCDA63BA21D0}.Release|Any CPU.Build.0 = Release|Any CPU
2832
{013F5235-D334-4770-82E4-350BA00F676C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2933
{013F5235-D334-4770-82E4-350BA00F676C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3034
{013F5235-D334-4770-82E4-350BA00F676C}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -37,6 +41,11 @@ Global
3741
GlobalSection(SolutionProperties) = preSolution
3842
HideSolutionNode = FALSE
3943
EndGlobalSection
44+
GlobalSection(NestedProjects) = preSolution
45+
{112DA850-B52E-49B6-811D-73237C5A7792} = {B77C557B-74CB-457F-AB66-B0BBCF93FA5C}
46+
{013F5235-D334-4770-82E4-350BA00F676C} = {B77C557B-74CB-457F-AB66-B0BBCF93FA5C}
47+
{BB527810-A846-43A8-8AD8-A1C2B191C2AE} = {B77C557B-74CB-457F-AB66-B0BBCF93FA5C}
48+
EndGlobalSection
4049
GlobalSection(ExtensibilityGlobals) = postSolution
4150
SolutionGuid = {D2610FC5-400C-462E-8BEC-028ECC97A1EF}
4251
EndGlobalSection

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "3.1.300",
3+
"version": "5.0.100",
44
"rollForward": "latestFeature"
55
}
66
}

src/WorldDomination.SimpleHosting.SampleHostedServiceApplication/WorldDomination.SimpleHosting.SampleHostedServiceApplication.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk.Worker">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
<UserSecretsId>dotnet-SampleBackgroundTask-AF705853-E47B-47BB-AECC-3BD0F2F2AA7A</UserSecretsId>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
9+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

src/WorldDomination.SimpleHosting.SampleWebApplication.Tests/CustomWebApplicationFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace WorldDomination.SimpleHosting.SampleWebApplication.Tests
1111
{
1212
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
1313
{
14-
protected override IHostBuilder CreateHostBuilder() => SimpleHosting.Program.CreateHostBuilder<Startup>(new MainOptions());
14+
protected override IHostBuilder CreateHostBuilder() => SimpleHosting.Program.CreateHostBuilder(new MainOptions<Startup>());
15+
16+
public MainOptions<Startup> MainOptions{ get; set; }
1517

1618
protected override void ConfigureWebHost(IWebHostBuilder builder)
1719
{

src/WorldDomination.SimpleHosting.SampleWebApplication.Tests/WeatherForecastControllerTests/GetTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Logging;
24
using Shouldly;
35
using Xunit;
46

@@ -13,9 +15,25 @@ public GetTests(CustomWebApplicationFactory factory)
1315
_factory = factory;
1416
}
1517

16-
[Fact]
17-
public async Task GivenAValidRequest_Get_ReturnsAnHttpStatus200OK()
18+
public static TheoryData<MainOptions<Startup>> Data => new TheoryData<MainOptions<Startup>>
1819
{
20+
{
21+
new MainOptions<Startup>()
22+
},
23+
{
24+
new MainOptions<Startup>
25+
{
26+
StartupActivation = new System.Func<WebHostBuilderContext, ILogger, Startup>((context, logger) => new Startup(context.Configuration, logger))
27+
}
28+
}
29+
};
30+
31+
[Theory]
32+
[MemberData(nameof(Data))]
33+
public async Task GivenAValidRequest_Get_ReturnsAnHttpStatus200OK(MainOptions<Startup> mainOptions)
34+
{
35+
_factory.MainOptions = mainOptions;
36+
1937
var client = _factory.CreateClient();
2038

2139
// Act.

src/WorldDomination.SimpleHosting.SampleWebApplication.Tests/WorldDomination.SimpleHosting.SampleWebApplication.Tests.csproj

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

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
@@ -11,11 +11,11 @@
1111
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1212
<PrivateAssets>all</PrivateAssets>
1313
</PackageReference>-->
14-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.6" />
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
16-
<PackageReference Include="Moq" Version="4.14.5" />
14+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.0" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
16+
<PackageReference Include="Moq" Version="4.15.1" />
1717
<PackageReference Include="NBuilder" Version="6.1.0" />
18-
<PackageReference Include="Shouldly" Version="3.0.2" />
18+
<PackageReference Include="Shouldly" Version="4.0.1" />
1919
<PackageReference Include="xunit" Version="2.4.1" />
2020
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
2121
<PrivateAssets>all</PrivateAssets>

src/WorldDomination.SimpleHosting.SampleWebApplication/Controllers/WeatherForecastController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.Extensions.Logging;
65
using WorldDomination.SimpleHosting.SampleWebApplication.Services;
76

87
namespace WorldDomination.SimpleHosting.SampleWebApplication.Controllers
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace WorldDomination.SimpleHosting.SampleWebApplication
8+
{
9+
public class HostedBackgroundService : BackgroundService
10+
{
11+
private readonly IServiceProvider _services;
12+
private readonly ILogger<HostedBackgroundService> _logger;
13+
14+
public HostedBackgroundService(IServiceProvider services, ILogger<HostedBackgroundService> logger)
15+
{
16+
_services = services ?? throw new ArgumentNullException(nameof(services));
17+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
18+
}
19+
20+
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
21+
{
22+
_logger.LogInformation("Consume Scoped Service Hosted Service running.");
23+
24+
while (!cancellationToken.IsCancellationRequested)
25+
{
26+
_logger.LogDebug("Doing stuff that takes a while (like checking a queue) ...");
27+
28+
await Task.Delay(1000 * 5, cancellationToken);
29+
30+
_logger.LogDebug("Doing stuff - finisihed.");
31+
}
32+
}
33+
}
34+
}

src/WorldDomination.SimpleHosting.SampleWebApplication/HostedService1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace WorldDomination.SimpleHosting.SampleWebApplication
44
{
55
public class HostedService1 : CustomHostedService
66
{
7-
public HostedService1(ILogger<HostedService1> logger) : base("HostedService-1", logger)
7+
public HostedService1(ILogger<HostedService1> logger) : base("HostedService-1 (e.g. Database setup / seeding)", logger)
88
{
99
}
1010
}

src/WorldDomination.SimpleHosting.SampleWebApplication/HostedService2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace WorldDomination.SimpleHosting.SampleWebApplication
44
{
55
public class HostedService2 : CustomHostedService
66
{
7-
public HostedService2(ILogger<HostedService1> logger) : base("HostedService-2", logger)
7+
public HostedService2(ILogger<HostedService1> logger) : base("HostedService-2 (e.g. Database migrations)", logger)
88
{
99
}
1010
}
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
21
using System.Threading.Tasks;
3-
using Microsoft.Extensions.DependencyInjection;
4-
using Microsoft.Extensions.Hosting;
2+
using Microsoft.AspNetCore.Hosting;
53
using Microsoft.Extensions.Logging;
64

75
namespace WorldDomination.SimpleHosting.SampleWebApplication
@@ -10,25 +8,16 @@ public class Program
108
{
119
public static Task Main(string[] args)
1210
{
13-
var options = new MainOptions
11+
var options = new MainOptions<Startup>
1412
{
1513
CommandLineArguments = args,
1614
FirstLoggingInformationMessage = "~~ Sample Web Application ~~",
1715
LogAssemblyInformation = true,
1816
LastLoggingInformationMessage = "-- Sample Web Application has ended/terminated --",
19-
20-
CustomPreHostRunAsyncAction = new Action<IHost>(host =>
21-
{
22-
using (var scope = host.Services.CreateScope())
23-
{
24-
var services = scope.ServiceProvider;
25-
var logger = services.GetRequiredService<ILogger<Program>>();
26-
logger.LogInformation($"Inside the {nameof(MainOptions.CustomPreHostRunAsyncAction)} method. Woot!");
27-
}
28-
})
17+
StartupActivation = new System.Func<WebHostBuilderContext, ILogger, Startup>((context, logger) => new Startup(context.Configuration, logger))
2918
};
3019

31-
return SimpleHosting.Program.Main<Startup>(options);
20+
return SimpleHosting.Program.Main(options);
3221
}
3322
}
3423
}

src/WorldDomination.SimpleHosting.SampleWebApplication/Startup.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
68
using WorldDomination.SimpleHosting.SampleWebApplication.Services;
79

810
namespace WorldDomination.SimpleHosting.SampleWebApplication
911
{
1012
public class Startup
1113
{
14+
private readonly ILogger _logger;
15+
private readonly IConfiguration _configuration;
16+
1217
public Startup(IConfiguration configuration)
1318
{
14-
Configuration = configuration;
19+
_configuration = configuration ?? throw new System.ArgumentNullException(nameof(configuration));
1520
}
1621

17-
public IConfiguration Configuration { get; }
22+
public Startup(IConfiguration configuration, ILogger logger)
23+
{
24+
_configuration = configuration ?? throw new System.ArgumentNullException(nameof(configuration));
25+
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
26+
}
1827

1928
// This method gets called by the runtime. Use this method to add services to the container.
2029
public void ConfigureServices(IServiceCollection services)
2130
{
31+
if (_logger != null)
32+
{
33+
_logger.LogInformation("Configuring services");
34+
}
35+
2236
services.AddControllers();
2337

2438
// Some fake database migration service.
2539
services.AddHostedService<HostedService1>();
2640
services.AddHostedService<HostedService2>();
41+
services.AddHostedService<HostedBackgroundService>();
2742

2843
// This is a -real- Weather Service.
2944
services.AddTransient<IWeatherService, WeatherService>();
@@ -32,6 +47,11 @@ public void ConfigureServices(IServiceCollection services)
3247
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3348
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3449
{
50+
if (_logger != null)
51+
{
52+
_logger.LogInformation("Configuring Middleware");
53+
}
54+
3555
if (env.IsDevelopment())
3656
{
3757
app.UseDeveloperExceptionPage();
@@ -45,6 +65,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4565

4666
app.UseEndpoints(endpoints =>
4767
{
68+
// Root/default route.
69+
endpoints.MapGet("/", async context =>
70+
{
71+
await context.Response.WriteAsync("Hello World!");
72+
});
73+
4874
endpoints.MapControllers();
4975
});
5076
}

src/WorldDomination.SimpleHosting.SampleWebApplication/WorldDomination.SimpleHosting.SampleWebApplication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

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

77
<ItemGroup>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Debug",
5-
"System": "Information",
6-
"Microsoft": "Information"
4+
"Default": "Debug",
5+
"System": "Debug",
6+
"Microsoft": "Debug"
77
}
88
}
99
}

0 commit comments

Comments
 (0)