Visual Studio Version:
16.1.6
Summary:
When I run a newly created empty WebApplication and start Debugging the WorkingDirectory is the projects root folder. When I create an empty console application it is the Output Directory. I expect both to be the same (and am quite sure this was the case some time in the past?).
Steps to Reproduce:
Create an empty web application project with .Net Core 2.1 or 2.2.
Paste this code:
c#
Console.WriteLine("CurrentDirectory in Main: {0}", System.IO.Directory.GetCurrentDirectory());
Run the project without IIS (IIS will show the same behavior but logging it is a bit more complicated).
Expected Behavior:
The output is CurrentDirectory in Main: {ProjectPath}\bin\Debug\netcoreapp2.2
Actual Behavior:
The output is CurrentDirectory in Main: {ProjectPath}
User Impact:
I can't use a library that expects the WorkingDirectory to be in the output directory without adding some code changing the working directory to where it should have been in the first place.
Other notes
This issue seems to have been reappearing and fixed for console applications and now seems to appear for web applications as well.
See #2239 and #589
I think Microsoft have to make a clear statement, is working directory for console and web apps supposed to be the same, or are they promoting what @davkean wrote in issue #2239.
For console apps it has to be output directory.
This websdk #238 might be relevant.
Issue #3619 is about changing console apps to match the behavior of web apps
It is actual for netcore 3.1 (AST.NET CORE 3.1 MVC/SPA )
Triage: This needs investigation as to what the right behavior is, please reach out to @BillHiebert on ASP.NET tooling to figure out if there's work for either team to do here.
Please factor in https://github.com/dotnet/project-system/issues/3619 when you look into this.
What I've been told in the past: ASP.NET uses the project directory as the working directory to avoid copying a significant number of content files to the output directory during build which would degrade the dev "inner loop" experience. Content files are, of course however, copied to the publish directory.
For me the problem is that one of the libs (nuget package RandomPersonLib) is looking for its content files using relative path. In our solution we have 2 ways of execution, one is console and second is WebApp. From console everything is working fine but when we run WebApp exception is thrown because RandomPersonLib can't find required data files.
So I manually added folder with data to WebApi project folder, however it is not really nice solution. This required data folder is included in WebApi/bin/Debug (as it is set to Build Action: Content, Copy if newer) but while default directory is WebApi it can't be found by RandomPersonLib package.
Most helpful comment
For me the problem is that one of the libs (nuget package RandomPersonLib) is looking for its content files using relative path. In our solution we have 2 ways of execution, one is console and second is WebApp. From console everything is working fine but when we run WebApp exception is thrown because RandomPersonLib can't find required data files.
So I manually added folder with data to WebApi project folder, however it is not really nice solution. This required data folder is included in WebApi/bin/Debug (as it is set to Build Action: Content, Copy if newer) but while default directory is WebApi it can't be found by RandomPersonLib package.