Unhandled Exception: System.BadImageFormatException - c#

I'm getting an unusual error when trying to build my newly created ASP.NET Core 6 project. Although it was working fine for the last few days, this error started appearing suddenly:
Unhandled Exception: System.BadImageFormatException: Could not load
file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The
module was expected to contain an assembly manifest.
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets
I tried changing the platform target to other options like x64 or x86, but this problem doesn't seem to come from the project itself. That is because, I created another project to see if it's still appearing. Turns out that this error being thrown even when I build my newly created project.
What could possibly go wrong? Should I reinstall Visual Studio 2022 (Community edition)?
EDIT
Rebooting the system fixed it. This could probably be an issue in my hard disk too as Hans Passant has mentioned in the comments section below.

Related

VSTest.Console.exe Could not load file or assembly Microsoft.TestPlatform.CoreUtilities

I'm trying to set up a C# project with the latest version of Visual Studio, 2022; this is with .Net 6 on Windows 10. It's a simple console program, and I've set up the project and a corresponding unit test project basically following the steps described in https://learn.microsoft.com/en-us/visualstudio/test/walkthrough-creating-and-running-unit-tests-for-managed-code?view=vs-2022 so I've got something isomorphic to that tutorial project.
And the unit tests work fine when run from within Visual Studio.
Now I want to also run them from the command line.
vstest.console bin\Debug\net6.0\foo.dll
gives
Testhost process exited with error: Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Microsoft.VisualStudio.TestPlatform.TestHost.Program.Main(String[] args)
. Please check the diagnostic logs for more information.
Looks like the toolchain is failing to find one of its own libraries? Is there some option I need to be specifying?
Make sure that foo.dll is the project which contain the tests and reference MSTest.TestFramework if you follow convention naming, this should be fooTests.dll
vstest.console bin\Debug\net6.0\fooTests.dll
in the example you mention the following line will throw error
vstest.console ..\BankTests\bin\Debug\net6.0\Bank.dll
While this will run normally
vstest.console ..\BankTests\bin\Debug\net6.0\BankTests.dll
After upgrading from .Net 5 to .Net 6, I had to face the same issue.
Add the following two lines to your .csproj file to fix the issue.
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>
Example:

'Could not load type 'System.Diagnostics.CodeAnalysis.SuppressMessageAttribute'

When I create a WPF project using VS2019 Community 16.9.4 I keep getting the Could not load type System.Diagnostics etc error message on Build while WPF projects I had previously created using earlier versions of VS don't give me this message.
I am at a loss to see what is causing this project to invoke this message. Does anyone have any ideas?
The full error message is:
Unknown build error, 'Could not load type
'System.Diagnostics.CodeAnalysis.SuppressMessageAttribute' from
assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'
Okay. I got to the bottom of this problem by process of elimination by comparing a project that works with one that doesn't and noticed that this issues was happening because a current day Telerik control was installed when I only have a license up to 2017. How that got translated into the above message is beyond me and completely threw me off the track.
I guess it's one to bare in mind where seemingly completely random things can happen.

VS2019 RegPkg.exe throws Unhandled Exception

I've just installed Visual Studio 2019 with SDK and when i try to run RegPkg.exe, I see the following error. I don't have any other VS instances installed on the machine but I'm confused why the error is reporting about version 15.0.0.0 (which is VS2017).
Can someone please help me to resolve this error.
Unhandled Exception: System.IO.FileNotFoundException: Could not load
file or assembly 'Microsoft.VisualStudio.Shell.Framework,
Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or
one of its dependencies. The system cannot find the file specified.
at Microsoft.VisualStudio.Tools.RegPkg.Main(String[] arguments)
I just noticed I got the exact same error message, using VS 2017's version. So it has nothing to do with the version you are using. I don't even have 2019 installed.
Microsoft simply broke their SDK, which has apparently been broken for 2 years, and no one noticed. For reference, I'm using VS 2017 15.9.7, also with the VSSDK installed.
I have a NuGet reference to that DLL in a VSIX extension I build, so I had a copy in my .nuget package cache ("%USERPROFILE%.nuget\packages\microsoft.visualstudio.shell.framework\15.9.28307\lib\net45\Microsoft.VisualStudio.Shell.Framework.dll"). I copied that version into the folder where regpkg.exe is located, and resolved the error (%VSSDK150INSTALL%\VisualStudioIntegration\Tools\Bin).
Probably not the best solution, but Microsoft needs to fix their product. I suspect it broke as part of their reorganization for the 2017 product, as they also added all the assemblies that extensions need for dependencies to NuGet. I guess they forgot to keep that assembly in the actual VS install folder.
EDIT: I've been looking into this more, and apparently the file actually exists in the lib sub folder right there with the exe. However none of the versions I've found actually work with CreatePkgDef.exe which was my backup plan, as it now throws this error instead:
CreatePkgDef : error : ArgumentException: No Visual Studio
registration attribute found in this assembly. The assembly should
contain an instance of the attribute
'Microsoft.VisualStudio.Shell.RegistrationAttribute' defined in
assembly 'Microsoft.VisualStudio.Shell.Framework' version '15.0.0.0'
Seems to work with regpkg.exe, though I haven't tried every combination of options.

WPFToolkit.Aero2 failed to load

I get this error when debugging:
Managed Debugging Assistant 'BindingFailure' : 'The assembly with display name 'WPFToolkit.Aero2' failed to load in the 'Load' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'WPFToolkit.Aero2, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.'
I tried uninstall the Extended WPF Toolkit nuget (3.3.0) and install again. didn't solve.
What am I missing ?
I am using VS Enterprise 2017 version 15.5.7, .NET 4.7.1.
The exception is when I debug Managed debugging assistant on the upper
line of my Xaml.
My references are:
just had the same issue, while using the WPFToolkit.Extended (standalone, old version)
fixed it by removing the reference I had, and installing the nuget you refer to.
my current references look like this:
Note, that I don't reference "WPFToolkit", while you - do.
I would say, try removing the "WPFToolkit" reference, see if it helps.

Xamarin visual studio filenotfoundException Windows.winmd

I'm creating a Portable Xamarin.Forms project, and I just added some pages I had on my shared project fixing errors.
After the edits, the project doesn't compile with the error
Unexpected error - Please file a bug report at http://bugzilla.xamarin.com. Reason: System.IO.FileNotFoundException: Could not load assembly "Windows.winmd, Version=, Culture=neutral, PublicKeyToken='. Pheraps it doesn't exist in the Mono for Android profile?
Project: myproject.Droid
Plus others 4670 errors like this:
file .winmd "Windows.winmd" contains type 'Windows.Storage.Streams.IDataWriter'. Windows namespace usage is reserved.
Project: myproject.WinPhone
Any error has a different type and they all refers to the WinPhone project.
I tryed checking in the Droid references for the Mono.Android but this is the scenario:
(the selected one was thicked, i deselected it for testing, but still same error)
What can I do?
Thanks all

Categories

Resources