.NET Core console application, how to configure appSettings per environment?

This is how we do it in our .netcore console app. The key here is to include the right dependencies on your project namely (may be not all, check based on your needs) and copy to output the appSetting.json as part of your buildoptions { “buildOptions”: { “emitEntryPoint”: true, “copyToOutput”: { “include”: [ “appsettings*.json”, “App*.config” … Read more

Only one configSections element allowed per config file and if present must be the first child of the root configuration element

The error message itself actually details the correct fix: configSections must be the first child* of the root element: *emphasis added So just move the configSections to the top: <configuration> <configSections> <section name=”Reva.Properties.Settings” type=”System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ requirePermission=”false” /> <section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” /> </configSections> <startup useLegacyV2RuntimeActivationPolicy=”true”> <supportedRuntime version=”v4.0″/> </startup> </configuration>

.NET console application exit event

You can use the ProcessExit event of the AppDomain: class Program { static void Main(string[] args) { AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); // do some work } static void CurrentDomain_ProcessExit(object sender, EventArgs e) { Console.WriteLine(“exit”); } } Update Here is a full example program with an empty “message pump” running on a separate thread, that allows … Read more

Startup.cs in a self-hosted .NET Core Console Application

So i came across with this solution, inspired by the accepted answer: Program.cs public class Program { public static void Main(string[] args) { IServiceCollection services = new ServiceCollection(); // Startup.cs finally 🙂 Startup startup = new Startup(); startup.ConfigureServices(services); IServiceProvider serviceProvider = services.BuildServiceProvider(); //configure console logging serviceProvider .GetService<ILoggerFactory>() .AddConsole(LogLevel.Debug); var logger = serviceProvider.GetService<ILoggerFactory>() .CreateLogger<Program>(); logger.LogDebug(“Logger is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)