Xamarin.Android: System.Runtime.Caching can not resolve reference - visual-studio

I have a solution with:
A project that handles business logic through crud functions. At some point the project uses System.Runtime.Caching
Other webforms, MVC and RESTservice projects. (irrelevant to the topic)
A newly created Xamarins.Forms project targeting .netstandard2.0
My problem is that when I reference the business logic project in the Xamarin.forms project then the Xamarin.Android project errors that it can not resolve the reference System.Runtime.Caching referenced by the business logic project.
It suggests that I add a NuGet package or assembly reference for System.Runtime.Caching, or remove the reference to the business logic project.
I have tried installing the package for both the Xamarin.Forms project and the related Xamarin.Android project. But it does nothing.
I am in a bit of halt here since I am unable to figure what the problem is and better yet how to sovle it.

System.Runtime.Caching doesnt seem to be part of .Net standard hence you can not add it .Check the nuget log when you try to add it.
You might be calling web project/API project code directly from xamarin forms, dont do that. You need to isolate the two.

Related

EF Core Analyzer RawSqlStringInjectionDiagnosticAnalyzer error

I am trying to port everything from Entity Framework 6.3 to Entity Framework Core 2.1.2 and this is my first experience with EF Core.
I have 2 projects one is my core or infrastructure project with all the entity models, DB Context and repositories and the other one is an asp.Net MVC which is my startup project and using all the services from the core project. Both projects are using .NetStandard Library 2.0.3 and targeted .Net Framework 4.6.2. And I have .Net Core SDK 2.2 on my machine.
The problem:
1- Converted everything to Entity Framework Core (All the entity mappings, contexts and repositories) and all the stuff seems fine, both projects build successfully.
2- starting first migration targeting existing database and entity models. Now while the migration process building both projects I am getting a strange error on asp.net MVC project as following:
An instance of analyzer
Microsoft.EntityFrameworkCore.RawSqlStringInjectionDiagnosticAnalyzer
cannot be created from
C:\Users\user\.nuget\packages\microsoft.entityframeworkcore.analyzers\2.1.2\analyzers\dotnet\cs\Microsoft.EntityFrameworkCore.Analyzers.dll
: Could not load file or assembly 'Microsoft.CodeAnalysis,
Version=2.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or
one of its dependencies. The system cannot find the file specified.
3- Now everytime you build the Asp.net project, you get the same error.
I really have no idea about this RawSqlStringInjectionDiagnosticAnalyzer and why this being even used by the projects, I have spent 2 days so far on this problem search all over the Internet, some people experienced somewhat similar problems (like this one) but could not found any solution, some of the stuff that I did was:
Updated my VS to the latest version (15.9.7), no luck
Tried to reference different versions of EF Core including Analyzer for both projects, no luck
I know Microsoft.CodeAnalysis dll should be a VS internal mechanism, nevertheless tried to reference it manually, no luck
Tried to include Microsoft.CodeAnalysis nuget package, no luck
Also there were other packages that I have tried, couldn't remember the names now, no luck
Please help me I am tired of adding and referencing different packages and binaries, I can't even grasp the problem.
I have realized this should be a warning and not an error so I found out that the "Treat warning as error" options in the project settings is enabled so that's reasons I am getting this as an error, however, still do not know the reason behind this warning.

Is it possible to share code between Xamarin.Forms projects?

My team has built a Xamarin.Forms application using VS2017. We are using a Shared project because one of our core functionalities requires ESRI mapping, and ArcGIS Runtime for Xamarin does not support anything but Shared projects at this time.
The next app that my boss wants should be fairly quick to develop if I can use the existing code we've already written for the first app. However, I don't see any way that I can reference that code from another project. I have tried adding a new UWP project to the existing one, but then I have to add all the same NuGet packages, 90% of which are unnecessary to this new project. On top of that, it won't actually compile correctly since the App.xaml is different.
I've searched all over trying to find a solution to this, and haven't come up with anything. PCL is not an option, nor is .NET Standard. Is there any way to accomplish this?

Adding System.Data.SQLite through NuGet doesn't add a reference to the project

I'm trying to make a very simple Xamarin.Forms test application to explore some features of the System.Data.SQLite package (namely encryption). However, I can't seem to get Visual Studio 2012 to add a reference to the package to the project.
Here are the steps I am taking:
Created a new Xamarin.Forms Portable blank app.
Right-click on TestApp (core project) and select Manage NuGet Packages
Search for "sqlite"
Install the "System.Data.SQLite (x86/x64)" package. (This package is version 1.0.97.0 and it is described as "the official SQLite database engine for both x86 and x64 along with the ADO.NET provider.")
According to NuGet, the package was successfully installed.
However, I cannot see the reference added under my project's "References" folder, and I also cannot write "using System.Data.SQLite" without errors ("cannot resolve symbol 'Data'"). Does anyone have any idea why this is happening?
On a side note, I can follow the exact same process described above except instead of creating a Xamarin.Forms Portable blank app, I choose a regular C# console app. This allows me to install the NuGet package fine and it shows up in references as expected.
Posting an update to share what I've learned.
So as Jason sort of alluded to in his answer, the reason I was not able to reference the package in my Xamarin.Forms project is because Xamarin.Forms is, by definition, a PCL project. It's meant to work on all platforms. The package I was referring to in my question is not a PCL, which means that anything in the package that is platform dependent is inaccessable (which is basically the entire package). The SQLite.Net-PCL package is basically the same thing as System.Data.SQLite, but in PCL form, which means it is compatible for use in a Xamarin.Forms project.
Following this logic, it makes sense that I was able to access System.Data.SQLite from a console project, since a console project is not a PCL project and it targets a specific platform.
For PCL projects, use SQLite.Net-PCL.

Can't reference PCL library within Xamarin Studio

I'm trying to build an Android and iOS app with Xamarin using a shared PCL library. I'm following the tutorial on the Xamarin website [1].
I have created my PCL solution, which currently doesn't do anything, and I'm now trying to reference it from another Android solution.
The documentation indicates I should use References and then select Projects. However, when I do this, I can't either see or find a way to add my PCL library project as a reference.
How can I add my PCL library solution as a reference in another solution?
[1] http://developer.xamarin.com/guides/cross-platform/application_fundamentals/pcl/introduction_to_portable_class_libraries/#Using_PCL
A solution is just a container for one or more projects. One project can be included in multiple solutions. By including multiple projects in one solution you can reference them from one another, which will allow them to automatically build/update their dependencies when needed. You can add a new project to an existing solution by doing "Add New Project" in the solution explorer - you don't have to create a complete new solution for each project.

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

Resources