Reputation: 84540
I've got a .NET Framework console app I'm trying to update to .NET 5, but it's failing on certain points that require data from app.config
.
The relevant documentation on how config works now quite helpfully has all of its examples as console apps... right up until you actually look at the examples, which are completely unhelpful. They're all based around a Main
block that looks like this:
static async Task Main(string[] args)
{
using IHost host = CreateHostBuilder(args).Build();
// Application code should start here.
await host.RunAsync();
}
A console app isn't a WinForms app. The application code doesn't "start" in Main; Main
is the main body of your program, which means that putting the line that makes configuration actually work at the end of the method means it will never be hit until the program is ready to terminate!
What am I missing? These documentation examples appear entirely pointless. I'm not trying to host any services; I just want my existing app.config
to be read at startup and made available to the config system, exactly the way it used to work in .NET Framework. What's the proper way to get the old behavior back?
Upvotes: 6
Views: 9581
Reputation: 4896
I suggest you could use System.Configuration.ConfigurationManager
.
This ConfigurationManager.ConnectionStrings is part of .NET Core Desktop runtime since .NET Core 3.0 (in previous version before 3.0, System.Configuration.ConfigurationManager was available as external nuget package).
By using System.Configuration.ConfigurationManager you can leverage existing app.config to be used for your Console project.
On your csproj of the console project, add this:
<PropertyGroup>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
Then you could have reference to System.Configuration.ConfigurationManager,
add the using System.Configuration;
and try something like this to get first connection string from ConfigurationManager.ConnectionStrings collection:
var constring = ConfigurationManager.ConnectionStrings[0];
Upvotes: 0
Reputation: 1066
Setting up the configuration should be pretty strait forward, there are plenty more options the configuration builder offers but here is bare bones for this example.
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
If you are wanting to use a DI pattern in your app then use IOptions
approach, In your constructor just ask for IOptions<OptionConfig>
, otherwise you could inject just IConfiguration
(Don't recommend)
var serviceProvider = new ServiceCollection()
.AddSingleton<IConfiguration>(config)
.Configure<OptionConfig>(config.GetSection("ExampleSection"));
.BuildServiceProvider();
Of course you will want to enter your first dependency using the service provider to essentially build your "application dependency tree"
Edit For those wanting a .net core project to work like configuration of old (.net framework).
If you want to use ConfigurationManager
you can its available to developers using the Nuget Package System.Configuration.ConfigurationManager
. Make sure to include the config file as app.config
, the configuration should look something like this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="appconfigtest" value="abc" />
</appSettings>
</configuration>
Upvotes: 6