1

I built a small C# web API using ASP.NET, mostly following this YouTube video. The project runs perfectly fine on Visual Studio Code. However, when I try to open the same folder with Visual Studio and run program.cs, I get the following error message:

Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I'm using ASP.NET Core Runtime 6.0.5, and I believe that's where the problem is (please correct me if I'm wrong). However, I don't know how to fix the issue. I tried reading this article, but I don't have App.config file for web APIs. I also tried to read .NET's known issue report, didn't really tell me what exactly should I do to fix the problem.

Any help and insights are appreciated. I'll also share any relevant code and package version information. Thanks!

Update

The problem is now fixed, although I'm not sure how exactly. Here are the steps I took:

1 - From VS code, create a new project using dotnet new webapi --framework net6.0 command line.

2 - Delete WeatherForecast.cs and WeatherForecastController.cs, if they got automatically created.

3 - Copy every C# file I wrote from the last project to this one, including program.cs.

4 - Close VS code. Open the project folder with Visual Studio and select File -> New -> Project From Existing Code -> (What type of project would you like to create?) C#.

One thing I noticed about the working project is that the project icon is different (before it was a green "C#" and wasn't working, now it looks like a blue globe representing web project, although both were C# project and had a .csproj file). Hope that helps.

sfeng
  • 93
  • 1
  • 7
  • Is this complete error message ? In my experience there you are missing some package from you dependencies – J.Memisevic Jun 21 '22 at 06:14
  • The complete message at break mode when debugging is "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." – sfeng Jun 21 '22 at 12:14
  • Since I generated this template from VS code and I haven't found any .config files, where can I add packages to my dependencies? – sfeng Jun 21 '22 at 12:15
  • did you run your project using "dotnet run" command from the same folder if it runs properly then it is nothing do with missing dependencies or with VSCode or Visual studio. it will be depending on how do you run it in visual studio. – CodingMytra Jun 25 '22 at 08:23

1 Answers1

0

Simpler solution: Just open the [project].sln file through double-click (to open it with Visual Studio ) directly.

The .sln file maintains the project state - Resource

Ghani
  • 1
  • 1