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

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

Related

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

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

UWP: Payload contains two or more files with the same destination path

Heyho,
I'm working my way through Xamarin.Forms right now and today I got the following error when I tried to Release compile the UWP project of my app (Debug is working):
Payload contains two or more files with the same destination path 'System.Runtime.CompilerServices.dll'. Source files:
C:\Users\[...]\.nuget\packages\runtime.win10-x64-aot.microsoft.netcore.universalwindowsplatform\6.2.0-preview1-26926-04\runtimes\win10-x64-aot\lib\uap10.0.15138\System.Runtime.CompilerServices.Unsafe.dll
C:\Users\[...]\.nuget\packages\system.runtime.compilerservices.unsafe\4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
The projects shared library compiles as netstandard2.0 and again includes a netstandard2.0 library itself, though I don't believe it should be a problem.
The UWP project references "Universal Windows" and the (self-compiled) SQLite.Net library. It also referenced the "Microsoft.NETCore.UniversalWindows" NuGet package, but I uninstalled it (attempting to solve the issue above).
I also tried Cleaning and recompiling the solution, removing and restoring the actual NuGet directories, adding and removing references, up- and downgrading of the UWP packages, changing the build order of my solutions projects...but nothing worked.
Does anyone have any idea what I could do here?
My main problem is that the error seems to come from .NET packages that I can't exchange/remove without crashing the whole project...
Thanks to this question I found a solution to my problem.
Though it wasn't the build target under (Project properties->Build->Build Target) for me I noticed the checkbox "Compile with .NET Native toolchain". I removed the tick and the project compiles fine for Release.

DLL version clashes in different NuGet packages

In my solution some projects reference the "MahApps" NuGet package, which includes 'System.Windows.Interactivity.dll' 4.5.0.0. Some projects also reference the "Prism" NuGet package, which includes 'System.Windows.Interactivity.dll' 4.0.0.0.
The app.config has a binding redirect of "0.0.0.0-4.5.0.0" to "4.5.0.0" by the way.
A handful of projects reference both NuGet packages, and looking in their "References" lists some of them have S.W.I v4.5.0.0 while others have v4.0.0.0. (I'm guessing this randomness is down to the order in which the packages were installed to the projects).
Sometimes the solution will build and run fine, but if only make a code change in one of the projects referencing S.W.I 4.0.0.0 then I get a runtime error along the lines of "v4.5.0.0 could not be found". v4.0.0.0 is being copied to the build output folder but my binding redirect is telling it to expect 4.5.0.0.
Any thoughts on a solution? I could try uninstalling and reinstalling the packages in the projects causing the issue, to see if I can get them to reference the 4.5.0.0 in the MahApps package, but my concern is that this may not be guaranteed to work during a package restore, screwing it up for another developer (or the build server).
I ended up upgrading to Prism 6, which includes no DLLs other than its own. Thankfully it was a straightforward job.
I also had to remove the MahApps package then add it again, to get Visual Studio to add that package's System.Windows.Interactivity.dll (4.5.0.0).

Upgrade of normal project to Xamarin.Forms project

I had a Xamarin project that was working fine, but it was build some time ago. Now my requirement is to upgrade this project to a Xamarin.Forms project. So I installed the Form nuget package. And then I try build the project. What happen is all api levels setting gone from all the projects. So I increased the api level => min api lavel 19 and compile api level with latest.
After all this I'm getting this error:
Error 141 Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Mono.Android.Support.v4, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065'. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'Mono.Android.Support.v4.dll'
at Xamarin.Android.Tuner.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters)
at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(ICollection`1 assemblies, AssemblyDefinition assembly, Boolean topLevel)
at Xamarin.Android.Tasks.ResolveAssemblies.Execute()
I've tried to install the Xamarin.android.supprt.v4.dll from nuget but it made no difference.
I'm using Visual Studio 2013 and I've also updated my the Xamarin for Windows.
I can only guess, since I can't see your project file.
This looks like a PCL issue. Xamarin.Forms projects can be either "shared" or "PCL" flavor. The "shared" project has access to the same APIs as your iOS or Android projects. The "PCL" flavor only has access to a subset of the APIs and to make things more complicated - there sub-flavors of PCL.
If you're migrating an existing project to XF I'd say you may be better off with the "shared" flavor. If you're starting new or feel it wouldn't be too much to refactor - use PCL.
It looks like you have a PCL XF project and copied code that tries to use System.IO.File - one of several missing APIs - and you'll have to refactor this API use into a dependency service or use some IoC to access it.
In a nutshell you'll create an interface that offers the file functionality you need (save, load, delete...), implement that interface in your respective iOS/Android/Win project or even better - shared project - and use IoC to instantiate the interface in XF project. For examples see how MediaService from XLabs is used

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.

Resources