Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
gcsizmadia committed Feb 5, 2022
1 parent 53b1062 commit 75bde5d
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mono_crash.*
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
Expand Down Expand Up @@ -61,6 +62,9 @@ project.lock.json
project.fragment.lock.json
artifacts/

# ASP.NET Scaffolding
ScaffoldingReadMe.txt

# StyleCop
StyleCopReport.xml

Expand Down Expand Up @@ -137,6 +141,11 @@ _TeamCity*
.axoCover/*
!.axoCover/settings.json

# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info

# Visual Studio code coverage results
*.coverage
*.coveragexml
Expand Down Expand Up @@ -348,3 +357,6 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Egonsoft.HU Configuration Extensions
[![GitHub](https://img.shields.io/github/license/gcsizmadia/EgonsoftHU.Extensions.Bcl?label=License)](https://opensource.org/licenses/MIT)

Extensions for Microsoft.Extensions.Configuration.

## EgonsoftHU.Extensions.Configuration.ConfigurationManager
[![Nuget](https://img.shields.io/nuget/v/EgonsoftHU.Extensions.Configuration.ConfigurationManager?label=NuGet)](https://www.nuget.org/packages/EgonsoftHU.Extensions.Configuration.ConfigurationManager)
[![Nuget](https://img.shields.io/nuget/dt/EgonsoftHU.Extensions.Configuration.ConfigurationManager?label=Downloads)](https://www.nuget.org/packages/EgonsoftHU.Extensions.Configuration.ConfigurationManager)

App.config/Web.config configuration provider implementation for Microsoft.Extensions.Configuration.

### Introduction
When migrating from .NET Framework to .NET Core and your `App.config` or `Web.config` file contains
lots of settings it might be desirable to be able to incrementally transfer those settings into the
new `appsettings.json` file so that you can incrementally replace the static uses of [`ConfigurationManager`](https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager) with
an injectable [`IConfiguration`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration) instance.

### Releases
You can download the package from [nuget.org](https://www.nuget.org/).
- [EgonsoftHU.Extensions.Configuration.ConfigurationManager](https://www.nuget.org/packages/EgonsoftHU.Extensions.Configuration.ConfigurationManager)

You can find the release notes [here](https://github.com/gcsizmadia/EgonsoftHU.Extensions.Configuration/releases).

### Usage
Suppose you have an `App.config` file with the following content.
```xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!-- legacy setting -->
<add key="MyAppSetting" value="..." />
<!--
Temporarily added to this file during migration.
The key uses the new format so that
later it can be read the same way from appsettings.json file.
-->
<add key="SomeApi:ApiKey" value="..." />
</appSettings>
<connectionStrings>
<add name="MyConnectionString" connectionString="..." />
</connectionStrings>
<!-- The rest is omitted for clarity. -->
</configuration>
```
First you need to add the configuration provider.
```C#
using Microsoft.Extensions.Configuration;

private static IHostBuilder CreateHostBuilder(string[] args)
{
return
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
webHostBuilder =>
{
webHostBuilder.UseStartup<Startup>();
}
)
.ConfigureAppConfiguration(
(hostBuilderContext, configurationBuilder) =>
{
// Add the configuration provider here.
configurationBuilder.AddConfigurationManager();
}
);
}
```
Then you can read the settings using the injected `IConfiguration` instance, e.g. in the `Startup.cs` file.
```C#
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
// Get the legacy app setting.
string myAppSetting = Configuration["MyAppSetting"];

// Get the new setting
string apiKey1 = Configuration.GetSection("SomeApi")["ApiKey"];

// Alternatively you can get the new setting also this way.
string apiKey2 = Configuration["SomeApi:ApiKey"];

// Get the connection string.
string myConnectionString1 = Configuration.GetConnectionString("MyConnectionString");

// Alternatively you can get the connection string also this way.
string myConnectionString2 = Configuration["ConnectionStrings:MyConnectionString");

// The rest is omitted for clarity.
}
}
```
### Credits
Ben Foster
- Blogpost: [Using .NET Core Configuration with legacy projects](https://benfoster.io/blog/net-core-configuration-legacy-projects/)
- Contact: [GitHub](https://github.com/benfoster), [Twitter](https://twitter.com/benfosterdev)
Loading

0 comments on commit 75bde5d

Please sign in to comment.