-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from MrDave1999/feat/issue-136
feat: Add support for Microsoft.Extensions.Configuration
- Loading branch information
Showing
27 changed files
with
524 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Dotenv.Extensions.Microsoft.Configuration | ||
|
||
[![dotenv.core](https://img.shields.io/badge/.NET%20Standard-2.0-red)](https://github.com/MrDave1999/dotenv.core) | ||
[![Nuget-Badges](https://buildstats.info/nuget/Dotenv.Extensions.Microsoft.Configuration)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.Configuration/) | ||
|
||
ENV configuration provider implementation for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration). | ||
|
||
This library adds extension methods for the [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration) package, which can be accessed through the [IConfigurationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfigurationbuilder) interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the [DotEnv.Core](https://github.com/MrDave1999/dotenv.core) project. | ||
|
||
This project was created to integrate the [DotEnv.Core](https://www.nuget.org/packages/DotEnv.Core) package into the .NET configuration system. | ||
|
||
Refer to the [API documentation](https://mrdave1999.github.io/dotenv.core/api/Microsoft.Extensions.Configuration.html). | ||
|
||
## Installation | ||
|
||
If you're want to install the package from Visual Studio, you must open the project/solution in Visual Studio, and open the console using the **Tools** > **NuGet Package Manager** > **Package Manager Console** command and run the install command: | ||
``` | ||
Install-Package Dotenv.Extensions.Microsoft.Configuration | ||
``` | ||
If you are making use of the dotnet CLI, then run the following in your terminal: | ||
``` | ||
dotnet add package Dotenv.Extensions.Microsoft.Configuration | ||
``` | ||
|
||
## Usage | ||
|
||
The following example shows how to read the application configuration from ENV file. | ||
```cs | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
// Build a configuration object from ENV file. | ||
IConfiguration config = new ConfigurationBuilder() | ||
.AddEnvFile("appsettings.env", optional: true) | ||
.Build(); | ||
|
||
// Get a configuration section. | ||
IConfigurationSection section = config.GetSection("Settings"); | ||
|
||
// Read configuration values. | ||
Console.WriteLine($"Server: {section["Server"]}"); | ||
Console.WriteLine($"Database: {section["Database"]}"); | ||
} | ||
} | ||
``` | ||
To run this example, include an `appsettings.env` file with the following content in your project: | ||
```.env | ||
Settings__Server=example.com | ||
Settings__Database=Northwind | ||
``` | ||
It doesn't matter if your .env file is in the root directory of your project, the configuration provider will start searching from the current directory and go up the parent directories until it finds it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
- name: Microsoft.Extensions.DI | ||
href: microsoft_extensions_DI.md | ||
href: microsoft_extensions_DI.md | ||
- name: Microsoft.Extensions.Configuration | ||
href: microsoft_extensions_config.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
DB_HOST=localhost | ||
DB_PORT=3306 | ||
DB_USERNAME=admin123 | ||
DB_PASSWORD=admin123##$ | ||
|
||
MYSQL__HOST=mysql | ||
MYSQL__PORT=3307 | ||
MYSQL__USERNAME=admin244 | ||
MYSQL__PASSWORD=admin244##$ |
15 changes: 15 additions & 0 deletions
15
...ins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\DotEnv.Extensions.Microsoft.Configuration.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DotEnv.Core.Props" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
IConfigurationRoot config = new ConfigurationBuilder() | ||
.AddEnvFile(path: ".env", optional: true) | ||
.AddEnvFile("config.env") | ||
.Build(); | ||
|
||
Console.WriteLine("DB_HOST=" + config["DB_HOST"]); | ||
Console.WriteLine("DB_PORT=" + config["DB_PORT"]); | ||
Console.WriteLine("DB_USERNAME=" + config["DB_USERNAME"]); | ||
Console.WriteLine("DB_PASSWORD=" + config["DB_PASSWORD"]); | ||
Console.WriteLine(); | ||
|
||
Console.WriteLine("BASE_URL=" + config["BASE_URL"]); | ||
Console.WriteLine(); | ||
|
||
IConfigurationSection section = config.GetSection("MYSQL"); | ||
Console.WriteLine("MYSQL__HOST=" + section["HOST"]); | ||
Console.WriteLine("MYSQL__PORT=" + section["PORT"]); | ||
Console.WriteLine("MYSQL__USERNAME=" + section["USERNAME"]); | ||
Console.WriteLine("MYSQL__PASSWORD=" + section["PASSWORD"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BASE_URL=http://localhost:3000/ |
38 changes: 38 additions & 0 deletions
38
plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Configuration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Microsoft.Extensions.Configuration</RootNamespace> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<PackageId>Dotenv.Extensions.Microsoft.Configuration</PackageId> | ||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> | ||
<Authors>David Román Amariles</Authors> | ||
<Copyright>David Román Amariles</Copyright> | ||
<PackageProjectUrl>https://mrdave1999.github.io/dotenv.core</PackageProjectUrl> | ||
<PackageIcon>dotenv-icon-nuget.png</PackageIcon> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<RepositoryUrl>https://github.com/MrDave1999/dotenv.core</RepositoryUrl> | ||
<PackageTags>dotenv, env, variables, environment, configuration, envfile</PackageTags> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance> | ||
<Description>Integrates DotEnv.Core package with Microsoft.Extensions.Configuration</Description> | ||
<Version>1.0.0</Version> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<NoWarn>1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\DotEnv.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\..\dotenv-icon-nuget.png" Pack="True" PackagePath="" /> | ||
<None Include="..\..\..\docs\extensions\microsoft_extensions_config.md" Pack="True" PackagePath="README.md" /> | ||
<None Include="..\..\..\LICENSE" Pack="True" PackagePath="" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.