Skip to content

Commit 79e440f

Browse files
committed
Merge branch 'improvement/ASP.NET-Core-6-hosts' into develop
While the OnTopic library will continue to use .NET Standard 2.1 for backward compatibility with .NET Core and even .NET Framework, we expect _most_ customers will be using ASP.NET Core 6. As such, I've upgraded the unit tests, integration tests, and host projects to run on .NET 6 or ASP.NET Core 6. As part of this, I also migrated the main `OnTopic.AspNetCore.Mvc.Host` project over to the new ASP.NET Core 6 minimal hosting model, which merges `Startup` and `Program` into a single top-level program. This is more consistent with the out-of-the-box ASP.NET Core 6 projects that new implementations will be working off of, and is also much easier to read. I didn't migrate the `OnTopic.AspNetCore.Mvc.IntegrationTests.Host` project as there are apparent incompatibilities between the new minimal hosting model and the integration tests. I suspect these are easy to resolve—and may simply necessitate exposing internals to the integration tests project—but I don't have time to look into it right now, so will revisit that later. In the meanwhile, however, as the only purpose of that project is to act as a barebones host for the integration tests, and not a boilerplate example for implementers, it's not an immediate priority.
2 parents ebdd23d + c2e229e commit 79e440f

7 files changed

Lines changed: 82 additions & 162 deletions

File tree

OnTopic.AspNetCore.Mvc.Host/OnTopic.AspNetCore.Mvc.Host.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>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<UserSecretsId>62eb85bf-f802-4afd-8bec-3d344e1cfc79</UserSecretsId>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

OnTopic.AspNetCore.Mvc.Host/Program.cs

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,80 @@
33
| Client Ignia, LLC
44
| Project Sample OnTopic Site
55
\=============================================================================================================================*/
6-
using System.Diagnostics.CodeAnalysis;
7-
8-
namespace OnTopic.AspNetCore.Mvc.Host {
9-
10-
/*============================================================================================================================
11-
| CLASS: PROGRAM
12-
\---------------------------------------------------------------------------------------------------------------------------*/
13-
/// <summary>
14-
/// The <see cref="Program"/> class—and it's <see cref="Program.Main(String[])"/> method—represent the entry point into the
15-
/// ASP.NET Core web application.
16-
/// </summary>
17-
[ExcludeFromCodeCoverage]
18-
public static class Program {
19-
20-
/*==========================================================================================================================
21-
| METHOD: MAIN
22-
\-------------------------------------------------------------------------------------------------------------------------*/
23-
/// <summary>
24-
/// Responsible for bootstrapping the web application.
25-
/// </summary>
26-
public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
27-
28-
/*==========================================================================================================================
29-
| METHOD: CREATE WEB HOST BUILDER
30-
\-------------------------------------------------------------------------------------------------------------------------*/
31-
/// <summary>
32-
/// Configures a new <see cref="IWebHostBuilder"/> with the default options.
33-
/// </summary>
34-
public static IHostBuilder CreateHostBuilder(string[] args) =>
35-
Microsoft.Extensions.Hosting.Host
36-
.CreateDefaultBuilder(args)
37-
.ConfigureWebHostDefaults(webBuilder => {
38-
webBuilder.UseStartup<Startup>();
39-
});
40-
41-
} //Class
42-
} //Namespace
6+
using Microsoft.AspNetCore.Mvc.Controllers;
7+
using Microsoft.AspNetCore.Mvc.ViewComponents;
8+
using OnTopic.AspNetCore.Mvc;
9+
using OnTopic.AspNetCore.Mvc.Host;
10+
11+
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
12+
13+
/*==============================================================================================================================
14+
| CONFIGURE SERVICES
15+
\-----------------------------------------------------------------------------------------------------------------------------*/
16+
var builder = WebApplication.CreateBuilder(args);
17+
18+
/*------------------------------------------------------------------------------------------------------------------------------
19+
| Configure: Cookie Policy
20+
\-----------------------------------------------------------------------------------------------------------------------------*/
21+
builder.Services.Configure<CookiePolicyOptions>(options => {
22+
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
23+
options.CheckConsentNeeded = context => true;
24+
options.MinimumSameSitePolicy = SameSiteMode.None;
25+
});
26+
27+
/*------------------------------------------------------------------------------------------------------------------------------
28+
| Configure: MVC
29+
\-----------------------------------------------------------------------------------------------------------------------------*/
30+
builder.Services.AddControllersWithViews()
31+
32+
//Add OnTopic support
33+
.AddTopicSupport();
34+
35+
/*------------------------------------------------------------------------------------------------------------------------------
36+
| Register: Activators
37+
\-----------------------------------------------------------------------------------------------------------------------------*/
38+
var activator = new SampleActivator(builder.Configuration.GetConnectionString("OnTopic"));
39+
40+
builder.Services.AddSingleton<IControllerActivator>(activator);
41+
builder.Services.AddSingleton<IViewComponentActivator>(activator);
42+
43+
/*==============================================================================================================================
44+
| CONFIGURE APPLICATION
45+
\-----------------------------------------------------------------------------------------------------------------------------*/
46+
var app = builder.Build();
47+
48+
/*------------------------------------------------------------------------------------------------------------------------------
49+
| Configure: Error Pages
50+
\-----------------------------------------------------------------------------------------------------------------------------*/
51+
if (app.Environment.IsDevelopment()) {
52+
app.UseDeveloperExceptionPage();
53+
}
54+
else {
55+
app.UseExceptionHandler("/Home/Error");
56+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
57+
app.UseHsts();
58+
}
59+
60+
/*------------------------------------------------------------------------------------------------------------------------------
61+
| Configure: Server defaults
62+
\-----------------------------------------------------------------------------------------------------------------------------*/
63+
app.UseHttpsRedirection();
64+
app.UseStaticFiles();
65+
app.UseCookiePolicy();
66+
app.UseRouting();
67+
app.UseCors("default");
68+
69+
/*------------------------------------------------------------------------------------------------------------------------------
70+
| Configure: MVC
71+
\-----------------------------------------------------------------------------------------------------------------------------*/
72+
app.MapTopicRoute("Web");
73+
app.MapTopicSitemap();
74+
app.MapTopicRedirect();
75+
app.MapControllers();
76+
77+
/*------------------------------------------------------------------------------------------------------------------------------
78+
| Run application
79+
\-----------------------------------------------------------------------------------------------------------------------------*/
80+
app.Run();
81+
82+
#pragma warning restore CA1812 // Avoid uninstantiated internal classes

OnTopic.AspNetCore.Mvc.Host/Startup.cs

Lines changed: 0 additions & 120 deletions
This file was deleted.

OnTopic.AspNetCore.Mvc.IntegrationTests.Host/OnTopic.AspNetCore.Mvc.IntegrationTests.Host.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>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

OnTopic.AspNetCore.Mvc.IntegrationTests/OnTopic.AspNetCore.Mvc.IntegrationTests.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">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

OnTopic.AspNetCore.Mvc.Tests/OnTopic.AspNetCore.Mvc.Tests.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">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

OnTopic.Tests/OnTopic.Tests.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">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<NoWarn>CS1591</NoWarn>
77
</PropertyGroup>

0 commit comments

Comments
 (0)