.net core & .net framework in the same team - c#

Our development team has many .net framework 4.6 projects (VS 2015).
We want to start a new .net core project to eventually deploy on linux.
We have installed VS 2017 and the .net core 2.0 preview.
But how can we reuse the existing library projects in this new one ?
We research but it is not clear for us :
- we need to change the target of the old projects from ".Net Framework 4.6" to ".NetStandard 1.x" ? (and solve the incompatibility)
- or we can use them like that ? (but how?)
Thanks

Microsoft publishes official guidelines for the porting process: https://learn.microsoft.com/en-us/dotnet/articles/core/porting/
To summarize:
Deal with your dependencies (by migrating them), recursively
Retarget your projects. Applications move to .NET core, libraries move to .NET Standard, where possible.
Use some helpful tooling to verify your ports
Test
So, to share things between .NET Framework and .NET Core, your libraries should target .NET Standard, as much as possible. Otherwise, you could possibly share the code and have to do multiple builds - build once targetting .NET Framework and again targetting .NET Core.

You can use/reference your old projects only if you target Full Framework in your new projects (which is not the case if you are going to run them on Linux).
If you started with preview you should convert you old projects to .Net Core projects and either target .NET Core 2.0 Preview or NetStandard 2.0 Preview. If you are not going to reference/use your old projects outside your application it might be better to target .NET Core 2.0 Preview because it might provide more API than NetStandard 2.0 Preview.

Related

Visual Studio 2019 - .NET Standard Project compatibility warning

I have inherited a .NET application. This application has a Visual Studio 2019 solution with many projects. When I open the solution, I can see the multiple warnings that say:
ProjectReference {path} was resolved using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This project may not be fully compatible with your project.
In Visual Studio 2019, I can see that the Target framework listed in the project properties is ".NET Standard 2.0". Which, means I have the framework installed on my machine. When I open one of the .csproj files in Notepad that throws this warning, I see that the TargetFramework element is set to netstandard2.0. At this point, everything looks correct. However, I don't understand why I'm receiving the error above or how to fix it.
How do I get rid of the warning shown above?
That means your .NET Standard 2.0 project has reference dependencies to other projects that have .NET Framework 4.6.1 as their target.
All of .NET Standard until v2.0 projects cannot reliably depend on other projects that use non .NET Standard projects such as .NET Framework and .NET Core projects. The hierarchy of target dependencies is incorrect, because .NET Standard projects has higher abstractions (more general) than .NET Framework and .NET Core.
The non .NET Standard project can have reference dependencies to .NET Framework and .NET Core projects.
Based on this fact above (and see the docs below) You can force .NET Standard project to depend on.NET Framework/NET Core projects, but there will be no guarantee that it will be compatible, therefore it won't be guaranteed to work especially at runtime.
The .NET Standard 2.1 are only compatible with .NET Core 3.0 and later.
See also this official doc of .NET Standard: https://learn.microsoft.com/en-us/dotnet/standard/net-standard
See the official blog post of .NET Standard 2.1 announcement: https://devblogs.microsoft.com/dotnet/announcing-net-standard-2-1/

visual studio generate .net dll files

im building an app using c# (not my first. other projects i dont have this problem). this one i included nuget package json
the problem that VS generates too many .dll files in my bin/debug folder (with exe). i don't want these dll files but it seems like the exe doesn't run without them (i coppied the exe to another path and run it)
when i search on the internet all the details about merging using some merge tool but my problem is related VisualStudio in the setting i believe?
This happens when you include a library which targets .NET Standard 1.5 or higher, from a project which targets .NET Framework 4.7.1 or lower.
The .NET Platform Standard table hints at this: it claims that .NET Framework 4.6.1 supports .NET Standard 1.5 or higher, but there's a footnote:
The versions listed here represent the rules that NuGet uses to determine whether a given .NET Standard library is applicable. While NuGet considers .NET Framework 4.6.1 as supporting .NET Standard 1.5 through 2.0, there are several issues with consuming .NET Standard libraries that were built for those versions from .NET Framework 4.6.1 projects. For .NET Framework projects that need to use such libraries, we recommend that you upgrade the project to target .NET Framework 4.7.2 or higher.
One of these issues is that you'll get a lot of extra DLLs, apparently because .NET Framework 4.6.1 only has partial support for .NET Standard 1.5.
The solution is for your project to target .NET Framework 4.7.2, or .NET Core.

With the final release of netstandard 2.0 and netcore 2.0, why we care to build netstandard2 library while we can use the full framework 4.6.1

.Netstandard2 is released final with .Net Core 2.0 and vs2017.3 with nuget4.3 and API surface cover 32k (except 41 class) and full cover net461
Quoting from Announcing .NET Core 2.0
You can now reference .NET Framework libraries from .NET Standard libraries using Visual Studio 2017 15.3. This feature helps you migrate .NET Framework code to .NET Standard or .NET Core over time (start with binaries and then move to source). It is also useful in the case that the source code is no longer accessible or is lost for a .NET Framework library, enabling it to be still be used in new scenarios.
We expect that this feature will be used most commonly from .NET Standard libraries. It also works for .NET Core apps and libraries. They can depend on .NET Framework libraries, too.
The supported scenario is referencing a .NET Framework library that happens to only use types within the .NET Standard API set. Also, it is only supported for libraries that target .NET Framework 4.6.1 or earlier (even .NET Framework 1.0 is fine).
So, in .netcore2 environment we can continue build/use Full .Net Framework as we did many years without the need to switch to .netstandard2 libraries.
With multi target project ,(net64;netstandard2), we get .net standard 2.0 out of box free (zero time effort) with the same API coverage.
Can you share your experience regarding: Can we continue build Full Framework 4.6.1 class library and use it in .netcore2? what is the limitation?
When you build a .NET Framework library, you can only use 4.6.1 with .NET Standard. If you want to use a newer API that is in e.g. netstandard2.0 or .NET 4.7, you'd need to retarget your library to 4.7, which then cannot be used by netstandard2.0 / netcoreapp2.0 projects. Effectively, you are locked into .NET Framework version 4.6.1 and can (/should) never update it to a newer version. (Of course, there is a workaround involving disabling the implicit fallback definition and manually setting AssetTargetFallback in the consuming project, but it requires manually editing the .csproj file and instructing all package consumers to do so).
Furthermore, the compatibility layer does not protect against using APIs that are not implemented on netstandard so a consuming project may get exceptions about missing types, methods, assemblies etc. Only when you target .NET Standard, your library is guaranteed to work on .NET Standard conformant platforms.
A practical limitation currently is that referencing .NET Framework library projects(!) (not NuGet packages) from .NET Core is not possible / only works with specific setups and workarounds (you will get an incompatible target framework warning).

Can I use any old arbitrary WIndows library in a .net core web app?

I just can't understand why I can't use an old library in a .net Core app targeting Windows and the full .net framework (I don't care about multi-platform).
Just trying to understand the limits here. I don't want to hit a wall after investing too much into it.
Steps followed:
Create a new .Net core web Application
Added PetaPoco from NuGet (just an example)
Can't use the library
From a comment from you on a deleted answer to this question
It's not about this particular reference. I just want to understand why I can't use an arbitrary Windows DLL. (I don't care about multi-platform) – Eduardo Molteni
It appears you are not too concerned why this specific project is not working (the deleted answer you commented on covered that quite well and if it was not deleted I would have up-voted it) but why in general you can't use a .NET Framework DLL in a .NET Core project.
Here is a diagram showing the layout of the ".NET ecosystem"
Things built for .NET Framework can't use DLLs built specifically for .NET Core, and things built for .NET Core can't use DLLs built specifically for .NET Framework because they are two "siblings" in the hierarchy.
Both .NET Framework and .NET Core can use .NET Standard libraries in their projects because .NET Standard is the "parent" of both the framework and core. Many newer NuGet packages now release versions that target .NET Standard so the library can be used with both versions. See the below chart to see what version of the .NET Standard library is compatible with various platforms. netstandard libraries are forward compatible so something that is compatible with 1.5 (like .NET 4.6.2) is also compatible with versions 1.0-1.4

Are .NET Core projects referencing PCLs cross-platform?

I have a .NET Core (ASP.NET Core) project created using Visual Studio 2017 RC (MSBuild).
I also have an old F# project library (traditional .NET Framework Library) that compiles to a dll file. I want to use it in said ASP.NET Core project.
To do that, I added a project reference in the ASP.NET Core project, opened the namespace, and made a few function calls in my controllers; it works perfectly when running the ASP.NET Core server using ISS from VS.
My question is, will this arrangement work if I want to deploy to linux (Ubuntu)? Or does using external PCLs/dlls from .NET Core instantly make it windows-only?
Note that I didn't have to add .NET Framework as a target framework in my csproj (it's MSBuild Core), it just worked.
Since your F# project targets the .NET Framework it will not run cross platform. If you modify your F# project to target the .NET Standard library it will. Here is a video by Immo Landwerth explaining how the .NET Standard Library works.
You should be able to just to reference the .NET standard library and not have to change anything. However the current version, which is 1.6, is missing a lot of API's from .NET Framework. This is fixed in 2.0. Infact since your using .NET Core 1.1 I think you are already using 2.0, but I'm not certain. Here is another video by Immo explaining how it should work.
That being said, I'm not sure what the F# support is like with .NET Standard.

Categories