Referencing a .NET Standard library in an ASP.Net application - c#

I've got a strange issue referencing a net standard application (FW Full 4.5.2)
I got this error targets 'netstandard2.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.5.2'., wasn't .NET standard supposed to allow references from Core and Framework projects as well?

As stated here .NET implementation support. You need atleast .NET Framework 4.61 for referencing .NET Standard 2.0 assemblies.

Related

Can a NET Standard library reference another NET Standard library?

I've inherited a bunch of net framework 4.71 solutions, and two net core 2.1 solutions.
I need to write a library for blob storage that can be used by both of them. I found that the net framework won't call a net core library, and a net core framework won't call the net framework library. So I was in the midst of trying a couple of net standard 2.1 libraries. (one for external models and one for the blob storage functions)
I haven't even gotten to the point of making sure that the net core and net framework projects can reference net standard libraries.
Question 1) can net framework and net core reference net standard libraries?
Question 2) is there a way to even share code between net core and net framework or should I just work on creating duplicate libraries?
Edit: I did just finally figure out what I was doing wrong in referencing the net standard model project from the blob storage net standard project. I had to add the project reference directly. Haven't had to do that in a long time. Visual Studio would add it for me with net framework.
Edit2: looks like I'm just an idiot. I finally figured out that I needed to also manually add the project reference for the library to the net core project. Now it compiles. Thanks everyone.
can net standard libraries reference other net standard libraries?
yes; a netstandard2.0 library can reference another netstandard2.0 library; a netstandard2.1 library can reference netstandard2.0 and netstandard2.1 libraries
can net framework and net core reference net standard libraries
.NET Framework 4.7.1 can allegedly reference netstandard2.0 libraries, although it has some assembly-binding-redirect problems in a few areas ("unsafe", "buffers", etc) - and some areas are simply glitchy; it cannot reference netstandard2.1 libraries
.NET Core can reference netstandard libraries; these days you shouldn't really be looking at .NET Core below .NET Core 3.1, which means it can reference both netstandard2.0 and netstandard2.1 libraries
is there a way to even share code between net core and net framework or should I just work on creating duplicate libraries
multi-targeting is also an option, if netstandard doesn't work well for you; you can use <TargetFrameworks>netcoreapp3.1;net471</TargetFrameworks>, for example, and use #if as necessary to switch between target-specific implementations.
In additional to the excellent answer by Marc I would like to add the .net standard support matrix that shows what .net version support what .net standard version.
The way I understand it, the intended migration strategy is something like this
Migrate the core libraries to .net standard 2.0 (or lower), starting with the libraries with the fewest dependencies.
Migrate the applications, UI projects, and any other project that depends on things outside .net standard.
Migrate the core libraries to .Net core or .Net 5. No new versions of .net standard will be released. So to use any new features the libraries need to be updated to .net core/5/6 etc.
This way should limit the number of projects that need to be updated at once. Allowing migration to be done piecemeal rather than all at once.

Can .NET Standard 2.0 Class Library depend on .NET Framework 4.7.2?

In one of my .NET Framework projects, I introduced a new .NET Standard 2.0 project. This project has dependencies on some existing .NET Framework projects. I was wondering if this is right to do.?
The reason I added a .NETStandard project is that we have plans to move the whole repository to .NET Core / Standard. Hence I thought, the new project we add can target .NET Standard. With .NET Standard project I get the new SDK style project file, package references, etc by default.
The consuming application is still .NET Framework.
Do I have to retarget the new project to .NET Framework 4.7.2 so that the project will have the above SDK style project file and package references but targets .NET Framework 4.7.2 now. It will then be as easy as changing the target framework when we move to .NET Core?
Depends on your purpose of introducing the Standard 2.0 project. Normally, you make a Standard when you need it to be accessible both from .NET Framework and Core. In your case, your Standard project won't be operable from Core under macOS or Linux.
Yes, this can be done.
https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-0
.NET Framework compatibility mode
Starting with .NET Standard 2.0, the .NET Framework compatibility mode was introduced. This compatibility mode allows .NET Standard projects to reference .NET Framework libraries as if they were compiled for .NET Standard. Referencing .NET Framework libraries doesn't work for all projects, such as libraries that use Windows Presentation Foundation (WPF) APIs.
This is necessary as an intermediate step for some projects, but in order to get to your final goal you will of course have to go all in on .net core which will require updating those projects not to use framework or they will crash at runtime.

Prefer .NET Standard 2.0 library reference over .NET 4.5.1 from a .NET 4.7.2 project

I have an old-style C# WPF project file. It is a .NET Framework 4.7.2 project. I want to reference a NuGet library that has builds for .NET Standard 2.0 and .NET Framework 4.5.1. An example of this would be morelinq.
As I understand it, .NET Framework 4.7.2 fully implements the .NET Standard 2.0 API. Therefore I would like my project to reference the .NET Standard 2.0 build in the NuGet package instead of the .NET Framework 4.5.1 build. The benefit of this would be that the .NET Standard 2.0 version of morelinq does not have additional dependencies, e.g. on System.ValueTuple.
However, when I add the NuGet package, it defaults to referencing the .NET Framework 4.5.1 build and hence, includes its additional dependencies.
This is only one of a few such cases. So at the moment it seems, to me at least, that I'm references many unnecessary additional dependencies that could be avoided.
(My apologies if this is a duplicate. I did try searching first.)

FileNotFoundException when after adding a Class Library assembly

I do have an ASP.NET MVC Core app and would like to add a Class library in my project.
I added it in my project via "Add Reference" > "Browse" > select DLL and done.
I added it on code like
using PagarMe; // works good on code
And I was able to compile and run the app. however when the user goes to a page where the lib is referenced then I got the fallowing error.:
FileNotFoundException: Could not load file or assembly 'PagarMe.Pcl, Version=2.0.6372.16631, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
What I already done.
I checked the output bin folder and the Dlls files are there.
Both DLLs are compiled with "Any CPU" configuration.
I tried PCL and non-PCL Version.
App Target framework: .NETCoreApp 1.1
Default Class Library Target Framework: .Net Framework 4.5.
Portable (PCL) Class Library Target Framework: .Net Framework 4.5 and ASP.Net Core 1.0
What can I do in order to use Class Library or PCL library into my Core App?
In .NET Core you no longer use Portable Class Libraries, but you target the correct version of the .NET Standard Library. Some PCL or Shared Classes may use some unsupported
references.
To solve this please try one of this:
1. Rebuild your Class Library to target .NET Standard. .NET Standard 1.6, for instance, is supported by both .NET Core 1.0 and .NET Framework 4.6.1.
.NET Standard can be thought of as the next generation of Portable Class Libraries (PCL). The .NET Standard improves on the experience of creating portable libraries by curating a standard BCL and establishing greater uniformity across .NET runtimes as a result. A library that targets .NET Standard is a PCL or a ".NET Standard-based PCL". Existing PCLs are "profile-based PCLs". (Taken from the documentation)
2. Target your app to .NET Framework.
You could build an ASP.NET Core application to target the full .NET Framework in stead of .NET Core only. This gives you the advantages of ASP.NET Core, without the limits of .NET Core.
ASP.NET Core != .NET Core

The .NET Framework and .NET Standard Conflict (Compatibility Issue)

My DLL is created using the .NET Standard 1.6 and ASP.net is using .NET Framework 4+
This is the Error that I am getting
Severity Code Description Project File Line Suppression State
Error Project targets '.NETStandard,Version=v1.6'. It cannot be
referenced by a project that targets
'.NETFramework,Version=v4.5.2'. WebApplication
Is there a work around for this ? Using the old ASP.NET framework instead of the new ASP.NET Core
I am not a fan of the Razor syntax, however I would like to use the old aspx syntax to create web forms.
Using : VS - 2017
Only .Net Framework 4.6.1 with tooling 2.0 supports .Net Standard 1.6. You will not be able to use DLL targeting .Net Standard 1.6 in application that's targeting .Net Full framework 4+ except 4.6.1 with tooling 2.0. Please refer this link for more details on platform support and compatibility.
It's like referencing a 4.5 assembly from a 4.0 project:
To see what .net standard version is implemented by what version of the .net framework, see: https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

Categories

Resources