Running different frameworks in the same solution? - c#

I have a class library using .Net Framework 4.5, in the code I'm using HttpWebRequest of System.Net.
If I use in a console application using .Net Framework 4.5, it's ok.
If I use in a console application using .Net Core 2.2, it's ok but it's using System.net from .Net Core 2.2 and not from .Net Framework 4.5.
There's a bug/correction in .Net Core 2.2 HttpWebRequest and I want to use the .Net Framework 4.5 version but my application is in .Net Core 2.2.
Is this possible?

Nope. It's best to think of the target framework for a class library as simply an interface. It's dictating a certain API footprint, but the actual framework code is not bundled into the class library.
The actual framework dependencies are satisfied by the end application that utilizes the class library. As long as there's an acceptable level of API compatibility, then you can add the reference, but when it comes time to compile, the target framework will be that of the application itself, not the class library.
If you absolutely need the .NET Framework version, then your only choice is to make your console application target .NET Framework as well, instead of .NET Core. You can still use the new project format; you just won't benefit from cross-platform or things like being able to deploy self-contained.

Related

ISystemClock not available in netstandard2.0

I'm still getting to grips with .Net Standard vs .Net Core after many years of development ASP.NET Framework. I have set up a new Web Api "app" which targets netcoreapp3.1 framework along with a middle-tier/Business Logic ClassLib and Data Access ClassLib, both of which target .Net Standard for maximum future compatibility. However, I can't seem to use ISystemClock from the Microsoft.AspNetCore.Authentication namespace in the .Net Standard classlibs!
From reading this SO question, I believe this might be because netstandard2.0 might not implement Microsoft.AspNetCore.App framework. Is this correct?
If it doesn't, should I:
Provide my own IMySystemClock interface in my class libraries which
the "app" itself can implement a trivial concrete class for?; or
Change my class middle and data access tiers to netcoreapp3.1
framework (seems over kill and restrictive to do this)?
Something else? Maybe I am missing the point of .Net Standard`?
A service (like ISystemClock) to provide the current (real or mock) time seems quite a fundamental service so I'm unsure why it's not appearing in .net standard framework?
Thanks
BloodBaz
If you want to access that functionality from a library, change the target framework of your library to .NET Core 3.1. Libraries can be built in .NET Standard or .NET Core. You can't use .NET Core functionality within a .NET Standard library, but you can do the reverse. Use .NET Standard functionality within a .NET Core library.
Also as a side note, unless it's required for compatibility reasons, I recommend you switch to .NET Standard 2.1. Visual Studio defaults new projects to .NET Standard 2.0, but a ton of new functionality was added in .NET Standard 2.1.
I spent years in the .NET Framework, so I feel the confusion. It took me a while to get used to it. I have a huge project I recently migrated over from .NET Framework to .NET Standard / .NET Core. What I ended up doing was dividing my code up into three sections. A .NET Standard 2.1 library with all the non .NET Core specific common code, a .NET Core 3.1 library with all my .NET Core specific common code (which referenced my .NET Standard library), and my applications as .NET Core 3.1 projects (which referenced my .NET Core 3.1 library).
Make sure you take a read at the answer in this question as well: What is the difference between .NET Core and .NET Standard Class Library project types?

Using .NET Core 2.0 implementation of Concurrent classes from .NET Framework application

I would like to use the .NET Core 2.0 implementations of the Concurrent and Generic collections referenced here from a .NET framework application. Creating a .NET Standard library, wrapping these classes, and referencing the project in a .NET Framework App does not appear to work. Instead, it runs the .NET Framework versions of the classes (which makes sense.)
Is there any way to do this? Our application relies on many Framework features that aren't in Core, so unfortunately we can't just convert it into a Core application.

.net core app target .net framework 4.5.2 on Linux

I wanted to understand the dot net core support a bit more.
My basic understanding was that if I wanted to run a .net app on Linux then it needs to be built .net core and targeted netcoreapp1.0 framework to guarantee this.
1) I assume the above assumption is correct?
2) When I was reading various articles online, such as this one about referencing exiting .net framework project within a .net core application (https://www.hanselman.com/blog/HowToReferenceAnExistingNETFrameworkProjectInAnASPNETCore10WebApp.aspx)
If I did this, presumably the app would only run on Windows and not Linux?
3) In the following article:
https://blogs.msdn.microsoft.com/cesardelatorre/2016/06/28/running-net-core-apps-on-multiple-frameworks-and-what-the-target-framework-monikers-tfms-are-about/
In context to running with .net run 4.5.2 option (
dotnet run -f NET452), it's mentioned:
If this app were running on the .NET Core Platform, let’s say on a Linux box or a Mac, this code won’t be executed, but the app would
still be running on Linux or MacOS.
What's the distinction between running and not executing? If my initial understanding was correct, then by running with .net 4.5.2 option on Linux I wouldn't expect the app not to run at all.
Appreciate a few questions there but really wanted to understand .net core a bit more.
There is a difference between .NET Core and ASP.NET Core and the articles you mentioned are about running ASP.NET Core "apps" on .NET Framework. Let my try to clarify this using a few declarative statements:
.NET Core is the cross-platform runtime.
ASP.NET Core is a set of libraries that until version 1.1.* can run
on both .NET Framework and .NET Core.
This means you can create a .NET Framework application (=> e.g.
net452) and use ASP.NET Corein this application.
The CLI tooling works for both projects targeting netcoreapp* and
net* - but net* currently only works on windows.
This means that for netcoreapp1.*, you cannot reference arbitrary libraries that have been built for .NET Framework. If you change the target framework to say net452, you are no longer building a .net core application, but a .net framework application.
For ASP.NET Core 2.0 this is going to change. Again a few statements:
ASP.NET Core 2.0 is still a set of libraries but they can only be used on .NET Core 2.0 and not on .NET Framework
Do note that this is still under discussion at the time of writing: https://github.com/aspnet/Home/issues/2022
.NET Core 2.0 is able to freely reference libraries that have been built for .NET Framework up to version 4.6.1
However, some libraries may fail at run time if they try to use API methods that aren't available on .NET Core

Can UWP run in .NET?

When I create new UWP project it automatically references .NET Core. Is it possible to run it on .NET? Why it needs .NET Core? I can't find any valuable information about this...
It needs .NET Core because it is built on .NET Core, much like WPF is built on the .NET framework.
So no, you cannot change it to run on the standard .NET framework any more than you can make WPF run on .NET Core.
The .NET Core Framework is a new version of .NET for modern device and cloud workloads. It’s a general-purpose and modular implementation of the Microsoft .NET Framework that can be ported and used in many different environments for a variety of workloads.
and No, you cannot run UWP project on .Net
althousg if you install previous version of VS then you can run it on .NET

Which class library type do I use with a Core 1.0 RC2 (.NET Framework) Web App

In ASP.NET Core RC2 Microsoft split the Core web application into two separate templates:
ASP.NET Web Application (.NET Framework)
and
ASP.NET Web Application (.NET Core)
I am planning on developing an app that is net 451 only. If I go the (.NET Framework) path. What type of class libraries should I be using? I assumed Class Library (.NET Core), but when I tried adding reference from a project with the (.NET Framework Template) it says it can't add reference to .NET Standard.
Maybe I shouldn't be using the .NET Framework template. The main reason why I feel like I should be is because I do not want to have to deal with developing for the other operating systems and not having access to certain packages like email and transactionscope, etc..
If you set your class library to target net451 also it will work.
Change project.json in class library project as follows...
"frameworks": {
"net451": { }
}
As I understand it, .NET Standard can reference .NET Core libraries, but .NET Core cannot reference .NET Standard libraries.
So, it comes down to the end-use of your application. If it's only going to run (or be hosted) on Windows, then use the larger, more established .NET Standard for the application (although you can choose to put common code in .NET Core projects in the same solution for future cross-platform re-use). However, if there's a requirement to run/host it on another platform (Linux, Mac), then you'll have to use the younger .NET Core.

Categories