From d31f12abce2ac5e3ea392f95456ebe35d356c5e4 Mon Sep 17 00:00:00 2001 From: Robin Sue Date: Wed, 30 Mar 2022 01:30:52 +0200 Subject: [PATCH] Upgrade to ASP.NET Core 6 --- .../SpikeCore.Irc.Irc4NetButSmarter.csproj | 2 +- .../SpikeCore.Irc.IrcDotNet.csproj | 2 +- .../ContainerBuilderExtensions.cs | 4 +- .../SpikeCore.MessageBus.Foundatio.csproj | 6 +-- src/SpikeCore/SpikeCore.Web/Program.cs | 45 ++++++++++--------- .../SpikeCore.Web/SpikeCore.Web.csproj | 24 +++++----- src/SpikeCore/SpikeCore.Web/Startup.cs | 29 +++++++----- src/SpikeCore/SpikeCore/SpikeCore.csproj | 18 ++++---- 8 files changed, 68 insertions(+), 62 deletions(-) diff --git a/src/SpikeCore/SpikeCore.Irc.Irc4NetButSmarter/SpikeCore.Irc.Irc4NetButSmarter.csproj b/src/SpikeCore/SpikeCore.Irc.Irc4NetButSmarter/SpikeCore.Irc.Irc4NetButSmarter.csproj index e8538a3..8ef88cd 100644 --- a/src/SpikeCore/SpikeCore.Irc.Irc4NetButSmarter/SpikeCore.Irc.Irc4NetButSmarter.csproj +++ b/src/SpikeCore/SpikeCore.Irc.Irc4NetButSmarter/SpikeCore.Irc.Irc4NetButSmarter.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 diff --git a/src/SpikeCore/SpikeCore.Irc.IrcDotNet/SpikeCore.Irc.IrcDotNet.csproj b/src/SpikeCore/SpikeCore.Irc.IrcDotNet/SpikeCore.Irc.IrcDotNet.csproj index 2a6948a..3a94402 100644 --- a/src/SpikeCore/SpikeCore.Irc.IrcDotNet/SpikeCore.Irc.IrcDotNet.csproj +++ b/src/SpikeCore/SpikeCore.Irc.IrcDotNet/SpikeCore.Irc.IrcDotNet.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 diff --git a/src/SpikeCore/SpikeCore.MessageBus.Foundatio/AutofacIntegration/ContainerBuilderExtensions.cs b/src/SpikeCore/SpikeCore.MessageBus.Foundatio/AutofacIntegration/ContainerBuilderExtensions.cs index b291fe7..94fce10 100644 --- a/src/SpikeCore/SpikeCore.MessageBus.Foundatio/AutofacIntegration/ContainerBuilderExtensions.cs +++ b/src/SpikeCore/SpikeCore.MessageBus.Foundatio/AutofacIntegration/ContainerBuilderExtensions.cs @@ -23,7 +23,7 @@ public static void RegisterFoundatio(this ContainerBuilder self) var subscribeAsyncMethodInfo = typeof(IMessageSubscriber) .GetMethods() - .Single(methodInfo => methodInfo.Name == "SubscribeAsync"); + .Single(methodInfo => methodInfo.Name == nameof(IMessageSubscriber.SubscribeAsync)); self.RegisterBuildCallback(container => { @@ -55,7 +55,7 @@ public static void RegisterFoundatio(this ContainerBuilder self) var messageType = iMessageHandlerType.GetGenericArguments().Single(); // Get the handle method for the message type - var handleMessageAsyncMethodInfo = iMessageHandlerType.GetMethod("HandleMessageAsync", new[] { messageType, typeof(CancellationToken) }); + var handleMessageAsyncMethodInfo = iMessageHandlerType.GetMethod(nameof(IMessageHandler.HandleMessageAsync), new[] { messageType, typeof(CancellationToken) }); // Create a delegate which satisfies SubscribeAsync, and calls the handle method var messageHandlerParameter = Expression.Parameter(iMessageHandlerType); diff --git a/src/SpikeCore/SpikeCore.MessageBus.Foundatio/SpikeCore.MessageBus.Foundatio.csproj b/src/SpikeCore/SpikeCore.MessageBus.Foundatio/SpikeCore.MessageBus.Foundatio.csproj index faa3758..4088438 100644 --- a/src/SpikeCore/SpikeCore.MessageBus.Foundatio/SpikeCore.MessageBus.Foundatio.csproj +++ b/src/SpikeCore/SpikeCore.MessageBus.Foundatio/SpikeCore.MessageBus.Foundatio.csproj @@ -1,12 +1,12 @@  - net5.0 + net6.0 - - + + diff --git a/src/SpikeCore/SpikeCore.Web/Program.cs b/src/SpikeCore/SpikeCore.Web/Program.cs index 779c1ca..966c725 100644 --- a/src/SpikeCore/SpikeCore.Web/Program.cs +++ b/src/SpikeCore/SpikeCore.Web/Program.cs @@ -1,44 +1,32 @@ using System; -using System.IO; using System.Threading; using System.Threading.Tasks; - -using Microsoft.AspNetCore; +using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Serilog; +using Serilog.Events; using SpikeCore.Domain; namespace SpikeCore.Web { public class Program - { - private static IConfiguration Configuration { get; } = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile(path: "appsettings.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables() - .Build(); - + { public static async Task Main(string[] args) { var cancellationTokenSource = new CancellationTokenSource(); var tokenHolder = new WebHostCancellationTokenHolder(cancellationTokenSource); - + Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(Configuration) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .Enrich.FromLogContext() .Enrich.WithProperty("App Name", "SpikeCore") - .CreateLogger(); + .WriteTo.Console() + .CreateBootstrapLogger(); try { - var webHost = WebHost - .CreateDefaultBuilder(args) - .ConfigureServices(servicesCollection => { servicesCollection.AddSingleton(tokenHolder); }) - .UseStartup() - .UseSerilog() - .Build(); - Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; @@ -49,7 +37,7 @@ public static async Task Main(string[] args) Log.Information("Running, press CTRL-C to stop the bot."); - await webHost.RunAsync(cancellationTokenSource.Token); + await CreateHostBuilder(args).ConfigureServices(servicesCollection => { servicesCollection.AddSingleton(tokenHolder); }).Build().RunAsync(cancellationTokenSource.Token); Log.Information("The bot has successfully stopped."); } finally @@ -57,5 +45,18 @@ public static async Task Main(string[] args) Log.CloseAndFlush(); } } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .UseServiceProviderFactory(new AutofacServiceProviderFactory()) + .UseSerilog((context, services, configuration) => configuration + .ReadFrom.Configuration(context.Configuration) + .ReadFrom.Services(services) + .Enrich.FromLogContext() + .Enrich.WithProperty("App Name", "SpikeCore")) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } } \ No newline at end of file diff --git a/src/SpikeCore/SpikeCore.Web/SpikeCore.Web.csproj b/src/SpikeCore/SpikeCore.Web/SpikeCore.Web.csproj index 75222f1..3781d9b 100644 --- a/src/SpikeCore/SpikeCore.Web/SpikeCore.Web.csproj +++ b/src/SpikeCore/SpikeCore.Web/SpikeCore.Web.csproj @@ -1,24 +1,24 @@  - net5.0 + net6.0 aspnet-SpikeCore.Web-E201EA6B-FD51-4107-8B35-2258C27E66F3 2.8 latest - - - - - - - - - - - + + + + + + + + + + + diff --git a/src/SpikeCore/SpikeCore.Web/Startup.cs b/src/SpikeCore/SpikeCore.Web/Startup.cs index e901530..71e1d7c 100644 --- a/src/SpikeCore/SpikeCore.Web/Startup.cs +++ b/src/SpikeCore/SpikeCore.Web/Startup.cs @@ -40,7 +40,7 @@ public Startup(IConfiguration configuration) Configuration.GetSection("Web").Bind(WebConfig); } - public IServiceProvider ConfigureServices(IServiceCollection services) + public void ConfigureServices(IServiceCollection services) { if (WebConfig.Enabled) { @@ -82,8 +82,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services) if (WebConfig.Enabled) { services - .AddMvc() - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + .AddControllersWithViews(); services.ConfigureApplicationCookie(options => { @@ -94,10 +93,17 @@ public IServiceProvider ConfigureServices(IServiceCollection services) } services.AddHttpClient(); + } - var containerBuilder = new ContainerBuilder(); - - containerBuilder.Populate(services); + // ConfigureContainer is where you can register things directly + // with Autofac. This runs after ConfigureServices so the things + // here will override registrations made in ConfigureServices. + // Don't build the container; that gets done for you by the factory. + public void ConfigureContainer(ContainerBuilder containerBuilder) + { + // Register your own things directly with Autofac here. Don't + // call builder.Populate(), that happens in AutofacServiceProviderFactory + // for you. var ircConnectionConfig = new IrcConnectionConfig(); Configuration.GetSection("IrcConnection").Bind(ircConnectionConfig); @@ -145,7 +151,11 @@ public IServiceProvider ConfigureServices(IServiceCollection services) .PropertiesAutowired() .SingleInstance(); - var container = containerBuilder.Build(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + var container = app.ApplicationServices.GetAutofacRoot(); // Grab an instance of IBot so that it gets activated. // We don't need to keep hold of it, it's a singleton. @@ -156,11 +166,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services) container.Resolve>(); container.Resolve(); - return new AutofacServiceProvider(container); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { if (WebConfig.Enabled) { if (env.IsDevelopment()) diff --git a/src/SpikeCore/SpikeCore/SpikeCore.csproj b/src/SpikeCore/SpikeCore/SpikeCore.csproj index 1af54bc..cf7ed81 100644 --- a/src/SpikeCore/SpikeCore/SpikeCore.csproj +++ b/src/SpikeCore/SpikeCore/SpikeCore.csproj @@ -1,19 +1,19 @@  - net5.0 + net6.0 latest - - - - - - - - + + + + + + + +