question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

looking for appsettings.json

See original GitHub issue

With a recent upgrade (and I can’t tell you whether we are talking Moq. MSTest, .NET Core or Visual Studio)

I am seeing a lot of my tests fail with:

Test method MyTestMethod threw exception: 
System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was 'D:\....\my project\TestResults\Deploy_MarkHeaton 20230114T084906\Out\appsettings.json'.

  Stack Trace: 
FileConfigurationProvider.Load(Boolean reload)
ConfigurationRoot.ctor(IList`1 providers)
ConfigurationBuilder.Build()
ActionFilterAttributeTest.ExecuteOnActionExecutingTest(String testName, IHeaderDictionary headerDictionary, IRequestCookieCollection cookieCollection, Type resultType) line 127
BasicAuthenticationAttributeTest.TestBasicAuthenticationAttributeNoUser() line 27

Obviously it is expecting an appsettings.json file in the OUT directory and that is not there. Google suggests issues with active project context with this sort of error.

Any thoughts?

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
MCFHTAGENTScommented, Jan 18, 2023

ok - the issue here is my application, which is a .NET Core website, expects the current directory to be the directory the app is installed into (as do some of the MS libraries I use) and the test framework is setting it to the ‘out’ directory and so all the relative paths break and it also can’t find appsettings.json.

Adding this

        [ClassInitialize]
        public static void Setup(TestContext ctx)
        {
            //Work around added for MSTest
            var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            Directory.SetCurrentDirectory(dirName);
        }

keeps it working as I would expect

1reaction
Evangelinkcommented, Jan 18, 2023

I just did investigate your reproducer and this is a breaking change behaviour documented (https://github.com/microsoft/testfx/blob/main/docs/Changelog.md#300---2022-12-06), in particular the entry [breaking change] Unify DeploymentDirectory location across target frameworks https://github.com/microsoft/testfx/pull/1414. There was a long standing bug where the DeploymentDirectory was not pointing to the correct directory. But because of the time it took to fix the issue, a lot of people got time to rely on this behaviour.

There is nothing better but updating your code that we could recommend.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Worker Service looking for appsettings.json in System32 ...
The service is using appsettings.json file for the config information. After installing the service using SC CREATE command, the service was ...
Read more >
ASP.NET Core AppSettings.json file
The appsettings.json file is the application configuration file that is used to store configuration settings such as database connection strings, any ...
Read more >
ASP.Net Core 2.1 application cannot find appsettings.json ...
json file exists in the published folder. The error message states that is is looking in the C:\WINDOWS\system32\ folder for the appsettings.
Read more >
Appsettings.json in .NET: How to read and get a value
The appsettings. json is used in .NET and .NET Core applications to read and get a value from the configuration file using the...
Read more >
looking for appsettings.json · Issue #1559 · microsoft/testfx
When I upgrade MSTest.TestAdapter from 2.10.0 to 3.0.2 the tests can no longer find my appSettings.json file. With 2.10.0 the TestContext.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found