Could not load file or assembly Visual Studio 2019 (Community) - visual-studio

This is going to be one of those questions for which there are hundreds of answers, so please bare with me as I have tried most of them!
I have been breaking up a very large project into smaller components with the view to slowly migrate it all to .NET Core.
Some Projects in my solution are now .NETstandard class libraries, and there are also some .Net Framework (4.7.8) projects.
I was getting on quite well today when suddenly I started getting this:
Could not load file or assembly 'ServiceStack.Text, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
The version installed in my projects is (was - read on...) 5.8.0.0
Now, I am not new to this error, I've found it before and it usually relates to either a project reference file, web.config or packages.config referencing a .DLL with a different version than that of the file in the /bin directory.
So I have been through all (34) project folders and deleted servicestack.text.dll in every project.
I have uninstalled it via NuGet in every project and reinstalled it.
I have checked every project's packages.config file and where they are .NETStandard projects, the project files and made sure that there are not references to ServiceStack.Text v 5.0.0.0
I have rebuilt, cleaned and restarted the solution, visual studio and my computer many times!
Then I noticed something...
In my .Net Framework projects, under References, if I click on the ServiceStack.Text file and view the Reference Properties, it shows Version: 5.0.0.0, but the Path is directing it at 5.8.0.0.
In the .NETStandard projects, it shows the correct version.
So then I thought maybe 5.9 can't work in the Framework projects, so I uninstalled it from every project and installed v 5.0.0.0 in every project. This then wouldn't build because Visual Studio threw errors in the .NETStandard projects saying "downgrade detected, please restore the updated version"
SO then I installed 5.9 (which is now the latest version) into all the .NETStandard projects, leaving 5.0.0.0 in the .NET Framework ones.
Now it will build, but the FileNotFound exception throws again when I try to run it.
So I have tried;
Everything using ServiceStack.Text 5.9
Everything using ServiceStack.Text 5.0
.NET Framework projects using 5.0 and .NETStandard (2.0) projects using 5.9
Uninstalling and reinstalling multiple times
Deleting all obj directories, cleaning and rebuilding
Checking all project and packages files
Wasted most of the day on this, so turned to SO... I do hope someone can help!

Right.... Answering my own Question again...
After a lot of messing about I found out what was happening, and it was nothing to do with Visual Studio builds, or .package file conflicts
The reason the error suddenly appeared was a call to Dump() in something I was testing, which had another call to Dump() in a preceding function referencing a different .NET Framework. Whilst according to Microsoft this (mixing Frameworks) works, ServiceStack has different versions and it caused a version conflict.
In case anyone else runs into this, here's what I have found…
Pseudo code example… a function in a .NET Standard Project (2.0)
Function Blah() as Something
… do something
Log(TestResult.Dump())
Return Something
End Function
And then a .NET Framework (4.7.8) Project which consumes this and again tries the Dump the result:
Sub DoSomething()
Dim Something = Blah()
Write(Something.Dump())
End Sub
The two frameworks require different versions of ServiceStack.Text and therefore throw a confusing, though I guess correct, FileNotFoundException in the running project.
I’m sure to run into this one again….!

Related

Visual Studio Could not load assembly

I have a C# project using MySql.Data and EntityFramework, and have used the Data Entity Model Wizard to create code based on an existing database. I created the project on a machine running MySql Server 8.0.25 (where I cannot control the installed version), so all my references for MySql packages are also version 8.0.25.
When I moved the project onto my own machine, running MySql Server version 8.0.31, the project successfully builds but has an error message, which Visual Studio says comes from a .edmx file:
Error 4: Could not load file or assembly 'MySql.Data, Version=8.0.31.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) ... ScheduleModel.edmx 7
The file in question only contains the text "MySql.Data" in one place, and that's an un-versioned reference to "MySql.Data.MySqlClient", on line 7.
Since the project builds and appears to run correctly, I don't know if this error affects anything. Regardless, I would like to understand it and why it happens and ideally make it go away.
Here's what I've learned form searching for an answer and trying to investigate/fix the error:
There are no references to version 8.0.31 anywhere in my project, as using Visual Studio to search the entire project for "8.0.31" found no results.
Answers to others' questions tell me the version number in the error message specifies the version of a file that Visual Studio DID find. (Whereas I would have thought the given version number is the one that it is LOOKING for.)
Several places said that the found version is probably in the GAC. Originally, the error message said MySql.Data.EntityFramework (not just MySql.Data), and I did find MySql.Data.EntityFramework in the GAC. (No idea how it got there.) After removing it, the error message changed to just MySql.Data. However, MySql.Data is not in the GAC, and no MySql.Data.* are still there either.
There's a tool called fuslogvw.exe that supposedly can help with this issue, but I could not get it to work. When I run it, there is nothing in the log. When I click on options, all options are grayed out. The one person's recommended registry edit did not work either. (I tried closing and re-opening Visual Studio after said edit, then re-building.)
Process Monitor tells me that, during the build process, no processes accessed any files that include "MySql.Data" in the name except those under packages/MySql.Data.8.0.25 and the project's output directory; all files are version 8.0.25. This appears to conflict with #1, because how can Visual Studio have found version 8.0.31 if it never accessed any such file?
I tried using bindingRedirect in App.config to redirect from 8.0.31 to 8.0.25, which had no apparent effect.
Cleaning the solution and re-building ends up putting MySql.Data version 8.0.25 back into the output directory, so Visual Studio clearly is finding the correct version.
Deleting the packages folder results in everything being replaced (with the right version, again) but the error message persists. (The rebuild was strangely fast; I'd have thought everything would have to be re-downloaded, but it was too fast to have done that.)
How would I diagnose this issue? What might be causing Visual Studio to look for or to find a version that I cannot find any reference to, or any .dll file for?
I'm using Visual Studio Community 2019, Version 16.11.2.

Assemblies can't get resolved when getting them from NuGet

I have some annyoing problem which I can't figure out. I installed Visual Studio 2017 Community on a new laptop and loaded an project I made on my old one from TFS. Suddenly some of the references can't get resolved.
So I tried to create a complete fresh ASP.NET MVC Project and download some of the assemblies that don't work in the old project by NUGET. After downloading the references don't work. I get already the warning in VS. One of those assemblies that doesn't work is for example
The referenced component 'System.Linq.Expressions' could not be found.
NUGET downloaded the assembly correctly and added the reference to the project. If I open the project file I find the entry for System.Linq.Expressions
and in HintPath: ..\packages\System.Linq.Expressions.4.3.0\lib\net463.
That file definitely exists on the file system. I also tried to change to path to an absolute one manually with same result.
I am using the newest .NET framework 4.7.2
Any suggestions what might be the problem?

Exception while loading assemblies: Could not load assembly 'Microsoft.Data.Sqlite'

I'm a longtime ASP.NET developer but new to Xamarin. I have a pretty simple NETStandard Xamarin project for Android built on Core 2.0 inside Visual Studio 2017. I've just begun building my app. In the common project I am using Microsoft.Data.Sqlite to work with a local database. That's the only library I'm pulling in besides Xamarin.Forms and NETStandard.
EDIT: I should note that this application used to compile just fine until I upgraded from Android API 26 to 27. I haven't changed any code. The problem isn't my code but either the project configuration or NuGet.
Here is the compile-time error that is not associated with any particular file:
Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Microsoft.Data.Sqlite, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'Microsoft.Data.Sqlite.dll'
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters)
at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver resolver, ICollection`1 assemblies, AssemblyDefinition assembly, Boolean topLevel)
at Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver resolver)
I do not know why the Android app needs Sqlite, as the database stuff is being handled by the common project. This seems like a red herring but I can't make this error go away.
Here is NuGet for MyApp (common project):
Here is NuGet for MyApp.Android:
My first instinct is this long list isn't right, but all I did was Install-Package Xamarin.Forms and all these other things came with it. Prior to installing Xamarin.Forms, I removed all NuGet packages. I've been fighting NuGet for a while so the "default" list isn't the same anymore. I've had Crosslight in here at one point, among other things, possibly Sqlite. When I create a brand-new Xamarin Forms app, there are only six packages: Xamrin.Forms (v3.1.0), Xamarin.Android.Support.v4 (v27.0.2), ...v7.AppCompat (v27.0.2), ...v7.CardView (v27.0.2), and ...v7.MediaRouter (v27.0.2), though they are not the latest versions.
I've exhausted Google. The closest I could find to resolving this is this GitHub issue for Mono, but the specific library is System.Drawing.Common, not Microsoft.Data.Sqlite. As that thread stands, it's a bug with Mono. If so, what is the workaround?
Should I be concerned that MyApp.Android is mixing Support.v4 and Support.v7?
If it's a clue, MyApp has gone through a lot of massaging and playing around with NuGet to get various things to work. I've installed many libraries and removed them later. I've edited the .sln and .csproj files, I've deleted the Packages, obj, and bin folders multiple times. (Sqlite is not referenced in the .csproj file.)
I've tried copying my code from MyApp to a fresh Xamarin Forms MyApp2 but that has different issues (not finding the Sqlite database and crashing when displaying a toast notification, despite having identical code). MyApp didn't have either of these issues last week when everything was running and before I tried upgrading the API from 26 to 27.
And yes, I've Cleaned and Rebuilt my project innumerable times, as well as cleared the NuGet cache.
I am trying to use the (latest) Android API 27. I believe I created MyApp under API 26 but I'm not certain.
All I want to do is create a Xamarin Forms app for Android API 27 that uses Microsoft.Data.Sqlite! Why is this so hard? I've spent over 90% of my time on this project fighting code I didn't write!
Xamarin Forms/Visual Studio can easily get confused. Sometimes certain errors may go away just be restarting VS. I generally will close VS, nuke all obj + bin directores, and nuke the .vs directory. That will clean a lot of the cached cruff. Always close + reopen VS after change the major XF versions. (2.0<->2.5, 2.5<->3.0, 3.0<->3.1, etc.)
Beyond that, due to the large dependency tree of nuget packages, both explicitly referenced, and implicitly, it's possible to get into situation where various nuget packages won't be compatible.
Mono.Data.Sqlite is a strange case because there is some redundancy between the mono assemblies and the .net assemblies, and you have to specifically add that assembly in. See: https://forums.xamarin.com/discussion/99947/how-to-reference-mono-data-sqlite You may have to explicitly go and find that assembly and add it to your project. bleh.
Generally I don't use that for sqlite, and prefer https://github.com/praeclarum/sqlite-net and https://github.com/bordoley/SQLitePCL.pretty. I realize that other nuget packages may depend on Mono.Data.Sqlite

Missing Method exception "System.Web.Routing.RouteValueDictionary System.Web.WebPages.TypeHelper.ObjectToDictionaryUncached(System.Object)

I've created a new project in Visual Studio 2013.2 with WebAPI and MVC enabled. Right out of the box, compiling the generated project and running gives me the exception. Looking around here, I've come across the following two questions:
Mvc 5.1 MissingMethodException System.Web.WebPages.TypeHelper.ObjectToDictionaryUncached
System.Web.WebPages.TypeHelper.ObjectToDictionaryUncached
Both these answers involve assembly redirects; however, in my case the assembly redirects are present and I'm still getting an issue. My problem is that the GAC is loading version 3.00.11001.0 which is listed under assembly version 3.0.0.0 and my application wants 3.20.20520.0 which is also listed as assembly version 3.0.0.0. (Note that in diagnosing the project I installed the 5.2 pre-release nuget package, but I had the same problem with the original versions, I just don't have those readily available)
Since the GAC is preferred over the bin directory, it seems that my development machine will always pull the wrong file.
Is it safe to remove these files from the GAC? It appears that these are required for Visual Studio to produce Razor intellisense
Removing the Assemblies from the GAC did behave exactly as expected. The Razor intellisense for the project stopped working; however, the application began functioning properly.
You have to add the reference of System.Web to get it working.

Visual Studio compiles assemblies I want to be content

I have duplicate assemblies with different versions. I cannot recompile projects that use old versions of the assembly, so I manually copy the assemblies into a sub directory of my bin folder, then update the web.config with assemblybinding's.
This works fine, but now I am trying to add the old assemblies to the visual studio project so that they will be deployed to the build directory. But, visual studio keeps looking at these assemblies and telling me I have a version conflict! Therefore, I cant compile the project...
You cant have the same assembly referenced twice in one program, even if they are different version, it's just the way visual studio works. You need to reconstruct and recompile the code that references the older assemblies using the new versions of the assembly. This can be done via a reflector, and is relatively easy if you know what you are doing.

Resources