There was a console .NET Core 3.1 application for SOAP communication based on IHost / IHostBuilder.
New requirements came and I had to convert the application to the GUI application. I chose WinForms.
I made following changes in the CSPROJ file for that:
<Project .........>
...........
<PropertyGroup>
<ApplicationIcon />
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<StartupObject />
</PropertyGroup>
...........
</Project>
and in the Program.cs
file:
using System.Windows.Forms;
.....................
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
...................
MainForm mainForm = new MainForm();
loggerFactory.AddGUILogger(mainForm);
...................
webBuilder.ConfigureServices(services =>
services.TryAddSingleton(mainForm)
);
...................
IHost host = ............
...................
host.RunAsync(); // host.Run();
Application.Run(mainForm);
}
It was Ok. The application was running with te main form, but while debugging it had two windows: the main form and the console window. When I closed the main form, the application stopped with an exception.
After some time I decided to change that and compared a newly created .NET Core 3.1 WinForms application with my application.
The main change was in project SDK. So I changed in the CSPROJ file
<Project Sdk="Microsoft.NET.Sdk.Web">
to
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
The other change was the [STAThread]
attribute before the Main
method.
Since then there is no additional console windows while debugging, but there is another problem: configuration is not being loaded from the appsettings.json
file.
I have a configuration object and it initializes itself as follows:
public class Configuration
{
private IConfigurationRoot _config = null;
private IConfigurationRoot Config
{
get
{
if (_config == null)
{
string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
IConfigurationBuilder builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings{env}.json", true, true)
.AddEnvironmentVariables();
_config = builder.Build();
}
}
return _config;
}
}
Now when I request Config[paramName]
, I get always null
.
How to make te configuration to work as it previously did?