Class library that can be shared between .NET Framework and .NET Core - asp.net

I have a big class library written in .NET 4.0 but when I reference it in a Store App that uses .NET Core, it shows an error. And further, Microsoft just released .NET Core for the next version of .NET.
I want to know what type of project will be able to be shared by both .NET 4.6 and .NET Core? Is a class library able to do that?
It seems that the release of .NET Core specialized for Store Apps causes confusion for me.

If you have a PCL (Portable Class Library) that works for Metro apps, it will definitely run on the fully fledged .NET (aka, your normal ASP.NET, WF, WPF applications).
That means that you don't have to do anything, besides porting your existing library to be compatible with the PCL you choose.

There is a new target called dotnet which you can use to target the platforms DNX, UWP and .Net 4.6. Effectively, this libraries are build against .Net Core packaged libraries instead of the libraries of the full .Net Framework.
Read more here: https://oren.codes/2015/07/29/targeting-net-core/ and related posts.

Related

Referencing .net(4.7.1) projects in .net core 2.2

I have a .net Core 2.2 project. This is created using a Webapplication (Model View Controller) template. I can add my .Net Framework 4.7.1 projects into this core project, it compiles, run - and is deployed on my test servers.
1) Then I read about 2.2 End of Life, and I tried to migrate this to 3.1, and I cannot reference .Net Framework 4.7.1 in 3.1 framework. I don't know what is my next step here.
2)I read that I can convert my dll's to .Net Standard and reference - but, how can I do this?
3)These 4.7.1 dll's are shared by .Net Framework projects and core projects, so if I change this to .Net Standard - will my .Net Framework applications work?
4) Also - should I migrate my 2.2 Core projects to 3.0 because of the EOL? Is that mandatory? How will EOL affect audits if I don't migrate?
First, 2.2 is EOL, because 2.1 is the LTS release. You can downgrade to 2.1 if you don't want to jump to 3.x yet, and you'll still have a year or two of support there, I think.
However, 3.x takes the first step towards the new vision of one .NET (.NET 5 for all workflows), so the sooner you can get there, the better. 3.1, specifically, is the LTS release for 3.x, so stick there if you don't want to be forced to upgrade again for a while.
.NET Core 3.x implements .NET Standard 2.1, which is why you can no longer target .NET Framework with that (no version of .NET Framework implements .NET Standard 2.1 and never will). However, .NET Standard 2.0 is supported by both .NET Core (2.x and 3.x) and .NET Framework 4.6.1+. As a result, if you need to share a library between all these targets, you should target .NET Standard 2.0.
As far as converting your existing libraries go, you simply change the target framework to .NET Standard 2.0. That's literally it. Once you do that, some functionality in the library may fail (anything that requires .NET Framework, i.e. Windows-specific APIs). At that point, you either need to rewrite those parts of the library to use .NET Standard-compatible APIs, or use compiler directives to sub-in alternate implementations for .NET Standard 2.0/.NET Core, at which point, you'd have to multi-target the library (i.e. .NET Framework and .NET Standard 2.0 or even specifically .NET Core). When compiling, DLLs will be generated for each specific target, allowing you to seamless reference the same library from projects targeting any of the library targets.
If you're doing anything with ASP.NET Core components in your libraries, you should factor that code out into separate libraries and target .NET Core 3.1 directly there. There's no point in targeting .NET Standard 2.1, as that code will only ever be applicable to .NET Core, anyways. You should also work in the opposite direction. In other words, if there's anything that's only applicable to .NET Framework projects (Web Forms, etc.), then factor that out into separate libraries that will only target .NET Framework. That will allow you to migrate the remaining parts of the library more easily to .NET Standard 2.0.

.Net Standard class library with WPF application and .NET Core Web app

I have,
Existing class library written with .Net Framework 4.5.2
Exiting WPF application written with .Net Framework 4.5.2
WPF application using class library.
Now, I would like to create asp.net Core web app with dotnetcore 2.1 and want to reference the class library.
Do I need to,
Convert class library with .Net Standard?
Which version of .Net Standard so that I should be able to refer to my WPF application (with .Net Framework 4.5.2) which we can't convert to .Net Core UI app?
Yes, you have to convert you libriary to .NET Standard in order to reference it from both Framework and .Net Core apps
You would need to use .NET Standard version 1.2 or lower in order to support .NET Framework 4.5.2
See the documentation for details: https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support

.NET Core project add reference to .NET Framework project. Why it's possible?

I have followings projects:
.NET Core 2.0 Web Application
.NET Standard 2.0 Class library &
.NET Framework 4.5 Class Library.
I add reference of .net framework class library to asp.net core web api project. and it seems it works very well.
I am wondering why it's possible to add reference of .NET Framework class library project to ASP.NET Core Web API or MVC?
It's not supposed to allow adding only Standard or Core libraries references to Core projects?
Is this core Web project with .NET Framework class libraries references still cross platform?
UPDATE
According to Phiter comment:
"If you import a .net framework library to your project it'll no longer be cross platform, but you can do it freely if you want to. They allow it because you might want to use .net core and still be on windows."
So if this is a reason, if I want to bind my project to .NET Framework and remain on windows why I use Core Web Project from the first place?
I thought we use core projects for cross platform ability and if not, the .Net framework is not a better option?
UPDATE
mason comment:
"Nothing funny: ASP.NET Core project doesn't have to run on .NET Core. It can also be run on .NET Framework.
Just because it's called 'Core' doesn't mean they're related. They could have called it ASP.NET FancyPants and had it run on .NET Core and .NET Framework and you wouldn't be as confused. Microsoft just sucks at naming things."
UPDATE (November 12, 2018)
A first look at changes coming in ASP.NET Core 3.0 - Fully leveraging .NET Core
As announced on the .NET Blog earlier this month, .NET Framework will get fewer of the newer platform and language features that come to .NET Core moving forward, due to the in-place update nature of .NET Framework and the desire to limit changes there that might break existing applications. To ensure ASP.NET Core can fully leverage the improvements coming to .NET Core moving forward, ASP.NET Core will only run on .NET Core starting from 3.0. Moving forward, you can simply think of ASP.NET Core as being part of .NET Core.
Customers utilizing ASP.NET Core on .NET Framework today can continue to do so in a fully supported fashion using the 2.1 LTS release. Support and servicing for 2.1 will continue until at least August 21, 2021 (3 years after its declaration as an LTS release) in accordance with the .NET Core support policy.
This was just added as part of .NET Standard/Core 2.0. As long as the .NET Framework dll only references things in the .NET Standard, it will use type forwarding to the .NET Core implementations.
I do not know what made Microsoft allow referencing .net framework class library into .net core project but as a programmer, I am happy with this allowance.
You see allowing .net core application to reference .net framework libraries is useful in case you want to start with windows and are planning to go cross platform in the future.
We are in a stage where many useful open source libraries do not fully support .net core till the date of this post, masstransit is an example, so when I am developing a new software I will be using .net core project that depends on such libraries and I will update them later when they support .net core.

Why mix .Net Standard and .Net Core projects in one solution?

I just started working through the Apress "Building Web Applications with Visual Studio 2017". I am at the beginning setting up the projects.
So I set up a solution called SpyStore.
Then a .Net Core console app called SpyStore.DAL.
Then a .Net Standard Class Library project called SpyStore.Models.
My question is why would the author choose .Net Standard class library over .Net Core Class library. Is there a difference?
What is the difference between .Net Standard and .Net Core.
And could there be a reason for him mixing Standard in all of the sudden?
You can visualize .netstandard as a set of APIs. It is implemented by multiple runtimes, like netframework (net461, net47 monikers for example) or netcore (netcoreapp) A lib is not an executable, so it is independent from the concrete implementation.
Because you may want to use your library in a .net framework project (or any other implementation of .net standard), targeting netstandard make sure it is fully portable to any of these implementation, where a netcoreapp lib would only be targetable from a netcoreapp project.
An app can only be netcoreapp or netcore, as it is dependent of the implementation of the framework
You can find more infos here, and which platform implements which version of netstandard:
https://learn.microsoft.com/dotnet/standard/net-standard

Best class library project for ASP.NET Core v1.1?

Well, I know the ASP.NET Core v1.1 is fresh, but I am little playing a with it and I am a bit confused about the right class library project choosing in the solution. So here is a scenario:
I start in the Visual Studio 2017 with ASP.NET Core web application project template and choose sub-branch ASP.NET Core (= not with .NET Framework).
Ok, than I would like to add a class library project, but there are two possibilities:
.NET Core Library
.NET Standard Library
Well, ASP.NET Core v1.0 did not support common class libraries, but it seems v1.1 have no problem with that. So result is, I can reference both types.
So my question - is it ok to reference .NET Standard Library to ASP.NET Core project? Or should I reference .NET Core Library only?
You can use .NET Standard Library with .Net Core 1.0. The Standard library is trying to set a common API for the cross platform .net APIs.
You could use it in your project to get experience with it, but choose what makes sense for your class requirements and timeline.
This site has a good article on .NET Standard Library

Resources