Skip to content

Commit 119787f

Browse files
authored
Update README
Co-authored-by: Coen <coen@tcr-it.nl>
1 parent 12223d0 commit 119787f

1 file changed

Lines changed: 51 additions & 74 deletions

File tree

README.md

Lines changed: 51 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,41 @@
55

66

77
# Configuration.Extensions.EnvironmentFile
8+
**Add support for Unix-style `.env` files in your .NET applications.**
89

9-
Unix style Environment files to configure .Net core applications
10+
This package lets you use `.env` files as a configuration source.
1011

11-
```
12+
```env
1213
ConnectionStrings__Logs=User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;
1314
14-
#Security section -- this line is omitted by the configuration provider
15+
# Comments are ignored
1516
Security__Jwt__Key=q2bflxWAHB4fAHEU
1617
Security__Jwt__ExpirationTime=00:05:00
1718
Security__Jwt__Audience=https://always-use-https.com
1819
```
1920

2021
# Motivation
21-
22-
Having a development environment that resembles as much as possible to production is the best.
23-
In Unix servers, you can configure your background services with an environment file that has the format specified above, but what about in local?
24-
So many options but none matches that, so you can have them now, having a `.env` file (or more) copied with your files on build and loaded in the configuration.
25-
22+
This package bridges the gap between development and production environments for
23+
.NET applications deployed as **Unix system services** on controlled servers.
24+
25+
When deploying .NET applications to Unix/Linux servers as systemd services,
26+
daemon processes, or other system-level services, the standard approach is to
27+
use environment files (`.env`) for configuration management. These files are
28+
referenced directly by service definitions and provide a clean, relatively secure way to
29+
manage application settings on controlled infrastructure.
30+
31+
However, .NET's built-in configuration providers do not support this Unix
32+
environment file format, creating a disconnect between your development
33+
environment and production deployment. This forces developers to use alternative
34+
configuration approaches during development that don't match the actual
35+
production setup.
36+
37+
# Security Considerations
38+
- **Never commit `.env` files** - Add them to your `.gitignore` immediately
39+
- **Restrict file permissions** - In production, place `.env` files in secure directories (e.g., `/etc/systemd/system/`) with read access limited to the service user (root)
40+
- **Or level up with secrets management** - For production workloads, consider upgrading to dedicated solutions like HashiCorp Vault (see [VaultSharp](https://github.com/rajanadar/VaultSharp) for .NET integration)
41+
42+
2643
# How to use it
2744

2845
Install the package via Nuget
@@ -31,7 +48,7 @@ Install the package via Nuget
3148
Install-Package Configuration.Extensions.EnvironmentFile
3249
```
3350

34-
or .Net core command line
51+
or .NET command line
3552

3653

3754
```
@@ -56,27 +73,23 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
5673
});
5774
```
5875

59-
# Default behavior
76+
or with the minimal hosting model (.NET 6+):
77+
78+
```
79+
var builder = WebApplication.CreateBuilder(args);
80+
builder.Configuration.AddEnvironmentFile();
81+
```
82+
83+
84+
# Default behavior (in development)
6085
- The variables are loaded from a file called `.env` that is placed in the same directory as your applications.
6186
- Trimming is performed (usually spaces at the end are mistakes).
6287
- No quotes in values are trimmed (there is no need to add quotes, the library will handle `=` just fine).
6388

6489
```
65-
public static IHostBuilder CreateHostBuilder(string[] args)
66-
{
67-
Host
68-
.CreateDefaultBuilder(args)
69-
.ConfigureAppConfiguration((hostingContext, config) =>
70-
{
71-
config
72-
.AddEnvironmentFile(removeWrappingQuotes: true, trim: false)
73-
.AddEnvironmentVariables();
74-
})
75-
.ConfigureWebHostDefaults(webBuilder =>
76-
{
77-
webBuilder.UseStartup<Startup>();
78-
});
79-
}
90+
config
91+
.AddEnvironmentFile(removeWrappingQuotes: true, trim: false)
92+
.AddEnvironmentVariables();
8093
```
8194

8295

@@ -85,22 +98,10 @@ public static IHostBuilder CreateHostBuilder(string[] args)
8598
You can have several files also loaded, remember the last file will override the first one (if same variables are present)
8699

87100
```
88-
public static IHostBuilder CreateHostBuilder(string[] args)
89-
{
90-
Host
91-
.CreateDefaultBuilder(args)
92-
.ConfigureAppConfiguration((hostingContext, config) =>
93-
{
94-
config
95-
.AddEnvironmentFile() // Configuring from '.env' file
96-
.AddEnvironmentFile("database-config.env") // Overriding with 'database-config.env'
97-
.AddEnvironmentVariables(); // Overriding with environment variables
98-
})
99-
.ConfigureWebHostDefaults(webBuilder =>
100-
{
101-
webBuilder.UseStartup<Startup>();
102-
});
103-
}
101+
config
102+
.AddEnvironmentFile() // Configuring from '.env' file
103+
.AddEnvironmentFile("database-config.env") // Overriding with 'database-config.env'
104+
.AddEnvironmentVariables(); // Overriding with environment variables
104105
```
105106

106107

@@ -109,23 +110,11 @@ public static IHostBuilder CreateHostBuilder(string[] args)
109110
You can specify variable prefixes to be omitted
110111

111112
```
112-
public static IHostBuilder CreateHostBuilder(string[] args)
113-
{
114-
Host
115-
.CreateDefaultBuilder(args)
116-
.ConfigureAppConfiguration((hostingContext, config) =>
117-
{
118-
config
119-
.AddEnvironmentFile() // Configuring from '.env' file
120-
.AddEnvironmentFile("with-prefix.env") // Variables like MyPrefix_MyVariable are loaded as MyPrefix_MyVariable
121-
.AddEnvironmentFile("with-prefix.env", prefix: "MyPrefix_") // Variables like MyPrefix_MyVariable are loaded as MyVariable
122-
.AddEnvironmentVariables(); // Overriding with environment variables
123-
})
124-
.ConfigureWebHostDefaults(webBuilder =>
125-
{
126-
webBuilder.UseStartup<Startup>();
127-
});
128-
}
113+
config
114+
.AddEnvironmentFile() // Configuring from '.env' file
115+
.AddEnvironmentFile("with-prefix.env") // Variables like MyPrefix_MyVariable are loaded as MyPrefix_MyVariable
116+
.AddEnvironmentFile("with-prefix.env", prefix: "MyPrefix_") // Variables like MyPrefix_MyVariable are loaded as MyVariable
117+
.AddEnvironmentVariables(); // Overriding with environment variables
129118
```
130119

131120

@@ -134,22 +123,10 @@ public static IHostBuilder CreateHostBuilder(string[] args)
134123
Configuration can automatically update on file changes
135124

136125
```
137-
public static IHostBuilder CreateHostBuilder(string[] args)
138-
{
139-
Host
140-
.CreateDefaultBuilder(args)
141-
.ConfigureAppConfiguration((hostingContext, config) =>
142-
{
143-
config
144-
.AddEnvironmentFile() // Configuring from '.env' file
145-
.AddEnvironmentFile("reloadable.env", reloadOnChange: true) // This file will be watched for changes
146-
.AddEnvironmentVariables(); // Overriding with environment variables
147-
})
148-
.ConfigureWebHostDefaults(webBuilder =>
149-
{
150-
webBuilder.UseStartup<Startup>();
151-
});
152-
}
126+
config
127+
.AddEnvironmentFile() // Configuring from '.env' file
128+
.AddEnvironmentFile("reloadable.env", reloadOnChange: true) // This file will be watched for changes
129+
.AddEnvironmentVariables(); // Overriding with environment variables
153130
```
154131

155132
Logo Provided by [Vecteezy](https://vecteezy.com)

0 commit comments

Comments
 (0)