How to solve Runtime Error in Visual Studio - c#

I am following a course on PluralSight on C# Fundamentals all is going well until I try to debug the application inside visual studio or even run without debug I get the exception below. Even with simple Hello World I get this exception also. So far I have tried reinstalling Visual Studio the problem occurs again, also to mention I create the folder structure through the terminal I am not sure this is relevant but better to mention it.
I am using Visual Studio Version Version 16.4.2.
The runtime error:
An handled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot fund the file specified.
The code:
using System;
namespace Gradebook
{
class Program
{
static void Main(string args[])
{
Console.WriteLine($"Hello, {args[0]}!");
}
}
}

In case anyone else has this issue, I am also going through the same course. The issue is that when you create the file through the command line, you don't have a solution file created. So then, I thought maybe I could select "Open a local folder" in the visual studio menu, navigate to Program.cs and execute it from there. If you try to do that, you will get this error
Instead, you need to click "Open a project or solution". Notice that it lets you open a project file in addition to the solution. So even though you don't have a solution (.sln) file, you can click on the .csproj file. Then after doing so, you will be able to directly run the code in visual studio. Also, it will automatically create a solution file.

Posting this as answer so I can add screenshots.
Increase Framework Version to 4.5. To do so, you need to enter project properties. You get there by e.g. right clicking your Project => Properties or by double clicking "Properties" in your solution explorer:
The Application tab should be the first tab. There you find a dropdown with which you can specify your Target Framework version:
While you are in your project's properties, you might as well provide default command line arguments to prevent said "IndexOutOfRange" error in debug:

Related

C# .NET "Could not load file or assembly System.Runtime"

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.
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

Unable to start program. [VALUE].dll is not a valid Win32 application error in Visual Studio 2017

I have developed an C#, ASP.NET web application in a Windows 7 machine using Visual Studio 2012. Now i had imported the entire project into VS 2017 running on windows 10 machine, and when i try to enter the debugging mode to analyze my code it shows the following error:
I guess the project configurations are conflicting hence it throws this error.
Any suggestions??
The same error happens when Visual Studio solution has selected the wrong Startup Project. The bold project is the designated startup project.
Go to the Solution Explorer > Right click on the correct project and select "Set as StartUp Project" in the context menu.
I also got this error. I ultimately got to know that I was not selecting .sln file.
In VS, you should select .sln file and it automatically loads the complete project structrue is what I learnt.
Selecting .sln file worked for me
These errors are mostly because you are not selecting the .sln or solution file. In your solution explorer tree, double click the solution file and then build and run.
This runs contrary to a users intuition that simply opening a file and running it would work. Consider it a poor user interface. Jet Brains Rider, for instance, does not have this issue.
Change Targeting Platforms with the Configuration Manager and Build the project then try to debug it.I hope this will help you.
Don't export the project folder. upload the .sln file.
It will work.
DLLs cannot be ran/debugged directly. You have to specify host application in the
Configuration Properties>Debugging>Command and then let it load the DLL by itself.
You will most likely need to copy the DLL to the directory searchable by the host application e.g. its root or ./plugins folder.
In the Configuration Properties>Build Events>Post-Build Event>Command Line simply enter something like:
copy "$(TargetPath)" "$(HOST_APP)\plugins"
The Startup Item needs to be a .exe file. It's looking at BusinessLayer.dll because BusinessLayer is currently the Startup Project.
First, build the solution. Then, set the Solution Explorer to folder view and find the .exe in one of you project's /bin folders. Right click on it and set it to the Startup Item.
Finally, click the play button in Visual Studio top bar.
EDIT: Basically the same as Thomas' answer, but I'm pointing out that the "correct project" is the one with the .exe file. I would have commented on his answer, but I have less than 50 rep right now.

Web Performance Test - could not load type 'Microsoft.VisualStudio.WebTesting.SharepointInformation'

I installed VS.2017 Enterprise and start a web performance project. When I try to add a Web performance test to the project I receive the error:
"The Web test could not be loaded: Could not load the type
'Microsoft.VisualStudio.TestTools.WebTesting.SharepointInformation'
from assembly 'Microsoft.VisualStudio.QualityTools.WebTestFramework'
Version=10.0.0.0, .."
TL;DR:
Check installed prerequisites in the latter part of Create a web performance and load test project
In my case, replacing original file
C:\windows\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.WebTestFramework\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.QualityTools.WebTestFramework.dll
with
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.QualityTools.WebTestFramework.dll solved the problem.
I tackled the same issue. Every time I created Web Performance and Load Test Project in Visual Studio 2017 Enterprise (with arbitrary .NET framework version) I got the same error when I tried to open *.webtest file. VS's user interface seemed incomplete and clicking on toolbars sometimes shown null exceptions.
At first, I thought I have incomplete installation, according to Create a web performance and load test project. I have even tried to completely uninstall and reinstall mentioned packages. Unfortunately, this did not help. I have also experimented with adding and removing reference to Microsoft.VisualStudio.QualityTools.WebTestFramework as there are several options in my reference browser, but without success.
There is not much information about WebTesting.SharepointInformation namespace. After a while, I discovered the existence of Microsoft.VisualStudio.QualityTools.WebTest.SharePoint.dll and after closer inspection, I have noticed that it references QualityTools.WebTestFramework assembly in C:\Windows\assembly\... that has the same assembly version and public key token, yet smaller in size compared to the one that is contained in VS 2017 installation. I have replaced it with VS2017's one and voila, *.webtest can be successfully opened.
The above answer is correct but the problem is the directory isn't accessible in my case due to some reasons so I just open my cmd and run this command. Just run, it will copy the file, but first turn of the vs.
copy "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ReferenceAssemblies\v4.0\Microsoft.VisualStudio.QualityTools.WebTestFramework.dll" "C:\windows\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.WebTestFramework\10.0.0.0__b03f5f7f11d50a3a"

How to find the reason for a failed Build without any error or warning

I have a WebApplication which contains reference to WCF services.
While building using Visual Studio 2010, Build fails without any error or warning. However building the .csproj using MsBuild is successful.
Can't figure out what should I try in Visual Studio, to resolve / diagnose the issue. Can you please help out?
I find out that the build has been failing,
From text displayed in status Bar.
From output window:
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
The output tab includes configuration details.
------ Build started: Project: <projectName here> Configuration: Debug Any CPU
I noticed that if "Build + Intellisense" is selected in the Error List, it causes the error messages to be swallowed.
Change this option to "Build Only", and all error messages will be displayed:
I don't know if this is a bug in Visual Studio or what, but it certainly revealed hidden error messages that were the key to pinpointing the failure for me.
Some, like Richard J Foster, have suggested increasing the "MSBuild project build output verbosity" setting to "Diagnostic" (the highest possible option), but this didn't solve the problem for me, as Visual Studio appeared to be suppressing the error message(s) themselves.
As an alternative, you may try to use the raw output messages from the "Output" tab, which haven't been filtered by Visual Studio. Either do an in-place search for the strings "error" and/or "failed", or copy all of the output to your favorite text editor and do a search there.
To ensure that the Output window appears each time you do a build, you can go to Tools → Options → Projects and Solutions → General, and ensure that the option "Show Output Window when build starts" is checked.
As an additional troubleshooting step, it is also possible to build the project from the PowerShell command line by running dotnet build. This will show you the complete build output, including any errors that Visual Studio may be hiding.
I just ran into a similar situation. In my case, a custom action (from the MSBuildVersioning package available on Nuget.org - http://www.nuget.org/packages/MSBuildVersioning/) which appeared in the csproj file's BeforeBuild target was failing without triggering any error message in the normal place.
I was able to determine this by setting the "MSBuild project build output verbosity" (in the latest Visual Studio's Tools tab [Path: Tools > Options > Build and Run]) to "Diagnostic" as shown below. This then showed that the custom action (in my case HgVersionFile) was what had failed.
Here are some things that you can try:
If your solution contains more than one project, try building each project one at a time. (You may even want to try opening each project independently of the solution.)
If applicable, ensure that all of your projects (including dependencies and tests) target the same version of the .NET Framework. (Thanks to user764754 for this suggestion!)
Tip: Check Tools → Extension and Updates to ensure that your packages are up-to-date.
Ensure that all dependency projects are built to target the same platform as your main project.
Try restarting Visual Studio.
As suggested by Bill Yang, try running Visual Studio as Administrator, if you aren't already. (If you are already running Visual Studio as Administrator, perhaps try the opposite?)
Try restarting your computer.
Try "Rebuild All".
Run "Clean Solution", then remove your *vspscc* and *vssscc* files, restart Visual Studio, and then "Rebuild All".
As suggested by Andy, close Visual Studio, delete the .suo file, and restart Visual Studio.
As suggested by Arun Prasad E S, close Visual Studio, delete the .vs folder in your solution directory, and then re-open Visual Studio. (This folder is auto-generated by Visual Studio and contains cache, configuration settings, and more. More details can be found in these questions: Visual Studio - Deleting .vs folder and https://stackoverflow.com/q/48897191.)
As suggested by MrMalith, close Visual Studio, delete the obj folder in your solution directory, clear your temporary folder, and then re-open Visual Studio.
Delete the hidden .vs folder & restart Visual Studio. That worked for me.
I want to expand on Sasse's answer. I had to target the correct version of .NET to resolve the problem.
One project was giving me an error:
"The type or namespace name 'SomeNamespace' does not exist in the namespace 'BeforeSomeNamespace' (are you missing an assembly reference?)".
There was no error in the Error List window but the assembly had a yellow warning sign under "References".
I then saw that the referencing project targeted 4.5.1 and the referenced project 4.6.1. Changing 4.6.1 to 4.5.1 allowed the overall build to succeed.
Nothing was working for me so I deleted the .suo file, restarted VS, cleaned the projected, and then the build would work.
I tried many things like restarting Visual Studio, cleaning and rebuilding the solution, restarting the PC, etc., but none of them worked for me. I was finally able to solve the problem by doing the following:
First of all, make sure all the projects in your solution (including tests) are targeting the same .NET version. Then:
Save pending changes in the project and close Visual Studio
Find the exact location from file explorer and find "obj" file and open it,
Then, delete all the included files (some files won't remove, it doesn't matter, just skip them).
Use run command (by pressing Windows Key + R) and type "%temp%" and press enter to find temporary files.
Finally, delete them all.
On other possibility is that Visual Studio needs to run as Administrator, this might be related to deploying to local IIS server or other deployment need.
Just for the sake of completion and maybe helping someone encountering the same error again in the future, I was using Mahapps metro interface and changed the XAML of one window, but forgot to change the partial class in the code-behind. In that case, the build failed without an error or warning, and I was able to find it out by increasing the verbosity of the output from the settings:
In my case (VS 2019 v16.11.20), disabling Text Editor->C#->Advanced->Enable 'pull' diagnostics in the options solved the issue.
Double check for _underscore.aspx pages in your project.
I had a page and code-behind:
`myPage.aspx` and `myPage.aspx.vb`
when building the project, I'd get errors on the .aspx.vb page stating that properties defined on the .aspx page didn't exist, even though the page itself would build fine and there were NO OTHER ERRORS showing in the output (even with diagnostic level build output).
I then came across a page in the project that was named the same thing but with an underscore: _myPage.aspx - not sure where it came from, I deleted it, and the solution built fine.

Cannot add SQLBASEUTIL.dll (Gupta) as reference to C# - VStudio 2012

I am doing a C# project in Visual Studio 2012 (x86). I was able to add Gupta.SqlBase.Data to the project but when I run the project if gives me an error related to SqlBaseUtil.dll.
Unable to load SQLBASEUTIL.dll driver library.
Please check your CLIENTRUNTIMEDIR entry inside INI file or check your environment variable path or system path.
Therefore, I tried to add as reference to the project the SqlBaseUtil.dll. I could not manage to add this dll. This dll is also added to GAC.
Please help.
Check if the mentioned ini file exists. If it does, the language files associated with the SqlBaseUtil.dll are missing and cannot be loaded.
I had the same problem, and even after finding the dll and putting it in the same folder as the exe I was trying to run, it still gave the same error.
The problem wasn't that it couldn't find the dll, but rather that it couldn't load it, because it was missing vcredist2005_x86.exe (Microsoft.VC80.MFC).
If anyone else has the same problem, try checking the Event Viewer for more info (Windows Logs > Application).

Categories

Resources