Change Xamarin.Forms .Net Framework target - c#

I have a Xamarin Forms project and the majority of times that I want to install a nuget package I have an error saying that:
Install-Package : Could not install package 'Microcharts 0.7.1-pre'. You are trying to install this package into a project that targets
'.NETPortable,Version=v4.5,Profile=Profile259', but the package does not contain any assembly references or content files that are compatible with that framework.
I Assume I can work this out by changing the project .net framework target. But when I go and change it, I get an error saying that it cannot change the target because that implies upgrading nuget to 3.0 and It canĀ“t do that.
So my question is: Which is the best way(and simplest) to change the target framework so I have less problems like above with nuget packages.

This library was built to target .NETStandard v1.4. You are trying to use it in a PCL project, that is not possible. PCL is on the way out as the previous not-so-good way to build cross-platform libraries, .NETStandard is the new way.
There is no older version available of this library, rebuilding it from source is a possible option. But realistically it is time to move on to avoid hitting this wall over and over again, you need to update your tooling so you can consume these kind of libraries. The VS2017 Community edition is freeware.

It is time to go from your
.NET PCL Library to an appropriate .NET Standard Library.
Here are some links concerning the migration to .NET Standard.
Upgrade PCL to .NET Standard Class Library
.NET Standard 2.0 Support in Xamarin.Forms
Converting PCL (Portable Class Libraries) to .NET Standard Class Libraries
.NET Standard - PCL Compatibility
.NET Standard Implementation Support
My Suggestion concerning the version of the .NET Standard to use.
If you don't care about backwards compatibility (You want to use the latest OS and SDKs) go all the way to .NET Standard 2.
If you want to have a good backward compatibility you should go on .NET Standard 1.1 - 1.5.
On most projects i use 1.4 - 1.5.

Related

C# - Can net6.0 project use a package reference of .net framework, for example, .net472?

I have a net6.0 SDK-style project and I want to use a library which seems to be only released to .net framework. After I added the reference, I got a warning as below. And for sure I can't use any classes from the lib in my code.
Package 'xxx' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project.
I'm new to C#. Does it mean that each lib only works for specific .net versions? Library developers declare and compile their code for the supported .net versions and publish to nuget. When we refer to a nuget lib, nuget will try to find the dll which matches our .net version and throw the above warning if not found.
That is to say, to be compat with the library, I have to downgrade my project to net472?
Yes, that is correct. Each library is typically compiled for specific versions of .NET.
If you want to use this library, you will need to downgrade your project to target one of the supported .NET Framework versions.
Does it mean that each lib only works for specific .net versions?
It is more nuanced than that; usually, libraries are perfectly happy with lower versions - meaning: if I target net5.0 from a library, and your application is net7.0: that's fine (with the caveat that even minor revisions can - but rarely - break something, if it was depending on functionality that has changed, perhaps because it was a bug - or some other implementation detail).
However, there is a hard break between .NET Framework and .NET Core (now just .NET); a .NET Core / .NET application cannot reliably consume a library that only targets .NET Framework, as .NET Framework is a fundamentally different framework than .NET Core / .NET.
In this scenario, libraries can do a few things to help you:
they can target some variant of .NET Standard, which is the subset of APIs that should work correctly on both .NET Framework and .NET Core / .NET (and other platforms that implement .NET Standard, such as Unity, Mono, Xamarin, etc as)
they can multi-target, which is to say: in the nupkg they can include multiple assemblies, perhaps a net472 target, a net6.0 target, and maybe one or two others
(the second option is more common than the first; .NET Standard is fading into the background now)
If the library you're using only supports .NET Framework, then indeed that won't work from .NET Core / .NET; you have three options:
limit yourself to .NET Framework (I do not recommend this option)
see if the library can be updated to use .NET Core / .NET (by contacting the maintainers, and possibly even proposing the changes yourself)
use a different library

Specifying Nuget Package Platform Target

I want to create a Nuget Package for a C# library that targets .NET Framework 4.0 and higher.
If I put my library in a directory named Net40, will this cause issues for projects that target say 4.1 and higher?
Should i just publish my library in a lib folder without specifying the supported .NET Framework version?
My library supports .NET Framework 4.0 and higher.
The reason why I am asking is that sometimes projects get messed up quickly when they target a different platform whilst the installed libraries via NuGet target another platform version. At least I want to avoid issues when developers target 4.0 and higher.
I hope your advice will be very helpful.

Problem adding the Square.Connect nuget package

I have been trying to add the Square.Connect nuget package so that I can integrate with the square up payment system.
When I am trying to add the package I am getting an error:
"Could not install package 'Square.Connect 2.14.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author."
I have tried on a new application using .net 3.5 and .net 2.0 but I get the same error but with the changed target version.
Has anyone been able to get around this or can you advise me what I maybe doing wrong?
According to nuget.org https://www.nuget.org/packages/Square.Connect/ the Square.Connect package is for .NETStandard 2.0. So you should target that. Hint: Expand dependencies on the nuget page, to see what it requires.
This package is incompatible with your project.
You should target .NET Standard 2.0 or .NET Full Framework 4.6.1 or higher.
Both can work with .NET Standard 2.0 assemblies.
.NET Core 2.0 too, but that's not usefull for you, I guess!
As Palle Due's answer says, the package information page for the most recent version of the package lists .NET Standard 2.0 as a dependency. Therefore, your project must target something compatible with .NET Standard 2.0. This table lists several compatible runtimes, such as .NET Framework 2.6.1 or higher, .NET Core 2.0 or higher, as Schwarzie2478's answer listed (but he didn't give source information, which I think is useful). The table also lists minimum versions for some other runtimes like Xamarin's Android/iOS, or Unity game engine.
However, if you look at some older versions of the package, 2.10.1.297 and earlier depend on .NET Framework 4.5. Therefore, if for some reason you can't target a newer version of the .NET Framework, you could use these older versions of the package. Although I don't know any reason why anyone would be stuck on such an old version of the Framework. .NET Framework lifecycle is complex and very difficult to find a table with actual dates, but my guess is that no supported server operating system is limited to .NET Framework 4.5, so any server installing security updates will probably be on .NET Framework 4.6.x or more likely 4.7.x, so I really don't see any good reason to stay on net45.

.NETStandard NuGet packages

I am trying to download a package that will allow me to encrypt and decrypt passwords. However, I can not find a package that is compatible with my project as I am working in .NETStandard.
Is there a way to make a package that is not fully compatible still work?
When I try to change my project to a .NETFramework I do not get the option of any .NETFrameworks...
Despite another project in the same solution being targeted to .NETFramework v4.6.1
Solutions:
Are there any .NETStandard password encryption packages?
Is there a way to search for .NETStandard compatiable packages?
Can I make my project target a .NETFramework even though it is not an option, but is downloaded?
The warning itself is just a warning but the library might still work fully well, especially when you are actually targeting .NET Framework 4.6.1 with your project.
Basically when you create a .NET Standard library, it targets a specific version of .NET Standard and you can imagine .NET Standard as a set of interfaces a platform that supports that version of .NET Standard must support. In this case, if you are targeting .NET Framework 4.6.1 with the app, the .NET Standard library will run on .NET Framework 4.6.1 anyway, so there shouldn't be any problem.
Problems could occur if you built another client targeting a different implementation of .NET like .NET Core or Mono, which wouldn't support some of the APIs that the library is using internally. In such case the application would crash once such API was accessed because the platform does not offer any implementation of it.
In case you don't require cross-platform support, you could just use classic .NET Framework library, instead of .NET Standard and the warning would disappear.
I'm personally using RSACryptoServiceProvider
from System.Security.Cryptography. This is available in .NET standard. Doesn't need any nuget packages either.
using System.Security.Cryptography;
Edit:
I should add to this that encrypting/decrypting password is never a good idea. Password should be stored as a hash width a strong one way hasing algorithm and should never be encrypted for decryption for later use.

Nuget Package compatible with C# and VB.NET

We have a legacy VB.NET application. The frontend is being redesigned in MVC 5, and I was assigned to abstract the business logic to an API.
I created a library with some code to make calls to the API (so the developer building the MVC 5 app can easily install/update the package and use it).
But I would like to help the developer responsible to redesign the VB.NET project to consume the API with the same NuGet package.
Is it possible to create one NuGet package compatible with both, or do I really need to make two packages?
I read something about .NET Standard, but I couldn't find anything about VB.NET, it looks they work for C# only (I'm not sure).
If it's possible, should the VB.NET project use the same .net framework version as the package in order to work?
A compiled .Net DLL is language agnostic: you can use a DLL written in any .Net language from any .Net language.
Note that in general you can only reference .Net DLL (.Net assembly) that is compiled for that or lower version of .Net compared to one you set target .Net version of code you compile. Ideally both DLL (on in NuGet and VB.Net) target the same .Net version, otherwise read MSDN: Version Compatibility in the .NET Framework.
The language does not matter for .Net Nuget packages because they all compile to the framework(s) they target. This means C# assemblies compiled from C# code can be decompiled into C# or VB .Net regardless of the original language they were created in.
A little more detail:
Nuget packages are limited by the framework version they target, so your legacy app should preferably target the latest version of the .Net framework (4.7.1 as of this post) if you want to maximally utilise third party libraries and target the latest .net standard (2.0 as of writing).
.Net Standard is a compatibility standard, which means that if a language runtime version supports that version of the standard, it is guaranteed to have certain APIs available.
https://learn.microsoft.com/en-us/dotnet/standard/net-standard
Based on the table in that documentation, if your legacy application is targeting .net framework 4.5, you can only build libraries that target .net standard 1.1. Any higher version for your nuget package will prevent you from using it in your legacy app.

Categories