Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env file does not load in ASP.NET / .NET Framework 4.7.2 #67

Closed
MrDave1999 opened this issue Apr 12, 2022 · 1 comment · Fixed by #68
Closed

.env file does not load in ASP.NET / .NET Framework 4.7.2 #67

MrDave1999 opened this issue Apr 12, 2022 · 1 comment · Fixed by #68
Labels
bug Something isn't working
Milestone

Comments

@MrDave1999
Copy link
Owner

I am testing this library in ASP.NET with .NET Framework 4.7.2 but the result is unexpected. The Load method always generates the same error:

.env: error: No such file or directory.

The problem is that this library uses the Directory.GetCurrentDirectory() method and this method is returning a path that does not correspond to the project path:

C:\Program Files\IIS Express

This problem has been mentioned here: dotnet/aspnetcore#4206.
As far as I can see, the Directory.GetCurrentDirectory() method works perfectly when you use it with .NET Framework 4.7.2, however, with ASP.NET it returns a path that does not correspond to the current directory of the application.

Temporary solution

The temporary solution I found was to use the AppContext.BaseDirectory property:

new EnvLoader()
     .SetBasePath(System.AppContext.BaseDirectory)
     .Load();

Note: This solution only works with dotenv.core >= 2.0.0.

The value representing the AppContext.BaseDirectory property is null in xamarin-android. This bug has not been fixed yet: dotnet/android#3489.

@MrDave1999
Copy link
Owner Author

Note that the AppContext.BaseDirectory property returns the root directory of the project in ASP.NET Framework, on the other hand, in ASP.NET Core it returns the current directory of the application.

@MrDave1999 MrDave1999 added the bug Something isn't working label Apr 16, 2022
@MrDave1999 MrDave1999 added this to the 2.0.1 milestone Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant