C# build generates a lot build-in dll files - c#

I am writing a simple .NET 4.6.1 console application (not .NET core), as below
References are
The problem is, once compiled, it generates a hefty lot of dlls that are supposed to be build-in dlls. What is the problem?

You left a big clue in the screenshot.
They're recommending that libraries target .NET standard.
There's a good discussion at:
https://github.com/dotnet/standard/issues/146
Libraries should generally target .NET Standard as this ensures that
they can be consumed by any app. There will be circumstances where you
need to access .NET Core specific APIs, either because the API is new
and not implemented anywhere else, or the concept is .NET Core only.
That's why I believe we should make it easy to retarget between .NET
Standard and .NET Core so that developers never have to fear being
"locked in". Start with NET Standard and retarget if necessary &
revert back once a new version of the standard is available that has
all the APIs you need.
So gRPC now works with .NET Standard.
If you don't want to opt-in, you could build the particular library targeting the .NET Framework.

Related

How come other languages can call .net6 dll's, but a .net framework application cannot?

We have a very large .net 4.8 application, and would like to start creating newer features in .net 6 as separate libraries in an attempt to slowly upgrade everything. However it seems that .net Framework cannot call .net 6 dll's!
However I believe other languages, such as VB, can call a DLL of any version. So why does .net framework care so much?
Am i missing something?
That's not possible. A .NET Framework (4.X) application cannot call into a .NET 6.0+ assembly. But the other way round works most of the time. So if you change your main app to compile with .NET 6.0, you can reference .NET 4.X assemblies or Nugets. You might get a bunch of warnings that the old libraries are not fully compatible, but you can still try to run them.
Note that you always run only one instance of the runtime, so within one process, there can only be one version of the .NET runtime, but the binary code of .NET Framework and .NET libraries is the same and the system libraries are mostly backwards compatible.

On Windows only: does net5.0-windows (or net6.0-windows) allow me to recompile .Net framework in .Net 5 ( or .Net 6)?

I have .Net framework apps, that are intended to run only on Windows, now and forever.
I am thinking about upgrading them to .Net 5 or .Net 6, and target them to net5.0-windows (or net6.0-windows). From MS website it says that
net5.0-windows will be used to expose Windows-specific functionality,
including Windows Forms, WPF and WinRT APIs.
Does that mean that my .Net framework apps can be recompiled in .Net 6, as long as I specify the net-windows target and the csproject upgrade and compilation is successful, the program will just work? I just intend them to run on Windows, now and always.
My apps also consume a lot of .Net framework 2.0/3.5/4.6 dlls ( mostly they are involved with OpenGL and other graphic things), which I can only assume to have no general port to .Net 5 or .Net 6, and which the source code is no longer available even to me ( they are from close-source third party providers).
Does that mean that my .Net framework apps can be recompiled in .Net 6,
Yes
the program will just work?
Most likely it will, but one should do a full regression test to verify.
My apps also consume a lot of .Net framework 2.0/3.5/4.6 dlls
If you can't migrate to .Net 4.8, then chances are slim that one can be able to migrate into .Net core.
( mostly they are involved with OpenGL and other graphic things), which I can only assume to have no general port to .Net 5 or .Net 6
Each one has to be taken on as a case by case basis; you may have just actually answered your own question. Say Goodnight Gracey...
What may better serve you is to create a docker container which will serve up the older versions of these app(s) which has the target version of the frameworks which currently work.
Unfortunately there is no definitive answer to your question, as it will depend (no pun intended) entirely on what your dependencies are.
.NET 5 was intended to be the unification of classic .NET Framework and .NET Core and in many cases it will just work.
Speaking from experience, in many cases it is easier to create a clean project in the new framework, copy all the existing files from the old project then re-add NuGet and project references. I have done this successfully for Console Apps, Test Projects and even a WinForms app 15-20 project dependencies to .NET 4.8 projects and it has worked out of the box - albeit with a compiler warning about the .NET 4.8 dependencies.
However I have also come across at least one missing / changed API between 4.8 and 5.0 - BeginInvoke. A blog post here describes it in more detail. Sometimes there are also issues with NuGet versions.
The closest thing to a definitive list online is the MS Doc "Changes that affect compatibility" and in particular the Breaking changes for migration from .NET Framework to .NET Core section. I'm not sure the is a better "answer from a reputable source" out there.
As for your .NET 2.0 / 3.5 / 4.6 dlls I don't think there is anyway of knowing without trying. There are simply too many unknowns, especially as they are 3rd party dlls. If I had to guess it would be that at least one won't work, whether you can find an updated version or how much work an alternative library will be to incorporate will be impossible to tell
As for net5.0-windows, it's basically a fudge. While the goal of everything .NET 5.0 onwards was unification and cross-platform everything, they couldn't get all the work done in time. I read a blob post about it, which I can't find right now, but basically WinForms in particular is very closely tied to some underlying Windows only APIs. Maybe .NET 7.0?

What’s the difference between NETStandard.Library package and Microsoft.NETCore.App packages?

I wanted to know the difference between the two packages and if it had anything to do with .NETCore and NETStandard and compatibility. I found this:
The NETStandard target framework is an abstract target framework that represents API surface of many frameworks and platforms. As such NETStandard assemblies can run on any platform that supports the NETStandard targeted by that assembly, for example: .NET Desktop, Windows Phone, Universal Windows Platform applications, .NET Core applications, etc. NETCoreApplication is a concrete target framework that represents a single platform with both API surface and implementation. .NET Core applications are runnable on their own. .NETStandard libraries must be published-for or consumed-by a specific concrete target framework to be used in that type of application.
on https://masteringalm.github.io/framework/2018/07/24/NetStandardvsNetCoreApp-Project-Types.html
But I’m not really sure how to use this to describe the differences between the two packages. I saw a post on this link: What's difference between .NetCoreApp and .NetStandard.Library?
Where it said:
NetCoreApp is a platform and .NetStandard.Library is a library supposed to be cross platform (portable class library) for various .NET platforms runtimes.
I think this connects to differences between the two specific packages mentioned in the question header, but could anyone describe the differences between the packages in a not so convoluted way?
.net standard
Each implementation of the managed framework has its own set of Base Class Libraries. The Base Class Library (BCL) contains classes such as exception handling, strings, XML, I/O, networking, and collections.
.NET Standard is a specification for implementing the BCL. Since a .NET implementation is required to follow this standard, application developers will not have to worry about different versions of the BCL for each managed framework implementation.
Framework Class Libraries (FCL) such as WPF, WCF, and ASP.NET are not part of the BCL, and therefore are not included in .NET Standard.
The relationship between .NET Standard and a .NET implementation is the same as between the HTML specification and a browser. The second is an implementation of the first.
Hence, the .NET Framework, Xamarin, and .NET Core each implement .NET Standard for the BCL in their managed framework. Since the computer industry will continue to introduce new hardware and operating systems, there will be new managed frameworks for .NET. This standard allows application developers to know that there will be a consistent set of APIs that they can rely on.
Each .NET version has an associated version of the .NET Standard.
.NET Core is a free, cross-platform, open source implementation of the managed framework. It supports four types of applications: console, ASP.NET Core, cloud, and Universal Windows Platform (UWP). Windows Forms and Windows Presentation Foundation(WPF) are not part of .NET Core.
Technically, .NET Core only supports console applications. ASP.NET Core and UWP are application models built on top of .NET Core.
Unlike the .NET Framework, .NET Core is not considered a Windows component. Therefore, updates come as NuGet packages, not through Windows Update. Since the .NET Core runtime is installed App-Local, and applications are updated through the package manager, applications can be associated with a particular .NET Core version and be updated individually.
For more information visit https://www.infoq.com/news/2017/10/dotnet-core-standard-difference/
Overly simplistic, but think of .NET Standard as a library that runs on any platform that can run .NET. This means it can run on Linux, Mac, Windows, etc. .NET Core is a minimalistic version of the "framework", so you can keep your your apps as small as possible and only add what you need. Core means only the core functionality that is absolutely required for an app to run.
Why is standard and core different? It is largely because the functionality of .NET on other platforms started open source with someone other than Microsoft. As such, the features have lagged behind .NET. The name of the .NET "framework" on other platforms was Mono. As some point .NET Core may well replace standard, but certain things have to get to the same level.
I imagine some purist will disagree with my oversimplification, but it is an easy way to get your head around the libraries.
As for what specifically is different between the two (or "what is missing from standard to be equivalent to core") - I am not sure. I am sure it can be looked up. In many cases, I have found Core works fine, such as dockerizing the application (Yeah, I made the word "dockerizing", but meaning running in a Docker container). The problems are when you run some package that cannot be handled in "Mono".
When it comes to packages you add into the standard or Core (Nuget anybody?), where there is a difference deals with one having features that cannot run in standard.

How to use a .Net Standard 2.1 DLL in .Net Framework 4.8?

I have a project that is targeted to .Net Framework 4.8. Now, we need to use a 3rd party dll. The problem is that the dll is targeted to .Net Standard 2.1.
Is there a way to use this dll in the .Net Framework 4.8 project?
I added the dll, get this error:
Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
I searched, seems it should work directly. Maybe I need to add a reference? Not sure what needs to do now.
Thanks
You cannot consume a .Net Standard 2.1 assembly in any .Net Framework Version because the .NET Framework (even the last ever version, 4.8) does not implement .Net Standard 2.1.
The .Net Standard .NET implementation support matrix at https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1 does not contain an entry for 2.1 under "Framework". Although very unlikely, the support matrix might change in time; this was the supported list at the time of writing.
More details on the decision by MS: https://devblogs.microsoft.com/dotnet/announcing-net-standard-2-1/
Extract:
Given many of the API additions in .NET Standard 2.1 require runtime
changes in order to be meaningful, .NET Framework 4.8 will remain on
.NET Standard 2.0 rather than implement .NET Standard 2.1. .NET Core
3.0 as well as upcoming versions of Xamarin, Mono, and Unity will be updated to implement .NET Standard 2.1.
Library authors who need to support .NET Framework customers should
stay on .NET Standard 2.0. In fact, most libraries should be able to
stay on .NET Standard 2.0, as the API additions are largely for
advanced scenarios. However, this doesn’t mean that library authors
cannot take advantage of these APIs even if they have to support .NET
Framework. In those cases, they can use multi-targeting to compile for
both .NET Standard 2.0 as well as .NET Standard 2.1. This allows
writing code that can expose more features or provide a more efficient
implementation on runtimes that support .NET Standard 2.1 while not
giving up on the bigger reach that .NET Standard 2.0 offers.
Solution:
The easiest way would be to convince the 3rd party to backport/multi-target to support netstandard2.0 as well. Alternatively, you can switch to targeting .Net 6 (LTS), .Net Core 3.1 (LTS), or Mono instead of Framework 4.8 in your project.
UPDATE:
Another possible workaround:
You might also attempt some trickery with .NET Framework compatibility mode. I had no idea you could do this. You could, for example, make a "wrapper" project that targets standard and references both your .Net Framework 4.8 code and the 3rd party library, unless the 4.8 part uses WPF - perhaps there are other pitfalls in unsupported scenarios as quoted below. It sounds convoluted to me, but if you have no other option, it might be worth a shot. From the same original site:
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.
(*) - Also .Net Core (elsewhere in the document/page)

Using .NET Standard library with Windows Compatibility Pack in .NET Framework

I'm working on migrating a large code base of libraries in a direction to eventually support .NET Core. Currently, everything is based on .NET Framework. I have a set of library projects which are consumed by several web applications.
The plan is to convert the library projects over to .NET Standard 2.0 so that they can be consumed by both .NET Framework (version 4.7.1) based websites and by new .NET Core (version 2.0) websites. I've done some test solutions which proved that this can be done.
To convert the first library over to .NET Standard I had to leverage the Windows Compatibility Pack for some of the features that are not part of .NET Standard. Some features such as SqlClient and some System.Drawing tools had to be imported into the library. All of this worked but an issue turned up when trying to bring my library into code which was still targeting .NET Framework.
Even though the namespaces were the same, the consuming code could not see the objects (such as Image or SqlConnection) unless I added the same Windows Compatibility Pack libraries into the consuming project. If anything, I would have expected this to cause issues as I now have two identical classes (same namespace and object name) in different assemblies. Fortunately, it is working. At least the unit tests are still passing.
Is this the way the Windows Compatibility Pack libraries are supposed to work? I had hoped that they would provide the functionality in the .NET Standard or .NET Core code but allow the .NET Framework to still use its own implementation.
The compatibility package references a few of the assemblies that were brought back to increase the compatibility of .NET Core with .NET Framework.
The way the package works is that there is a meta-package (the one that you reference) and it has references to individual packages that actually contain the implementation. Those individual packages have different assets depending on the target framework.
Take for example System.IO.Ports. That package contains the following assets (and a few more things that are not directly relevant to this):
net461
netstandard2.0
The netstandard2.0 asset contains the code that implements the System.IO.Ports functionality. You will use this if you are building a .NET Core application.
The net461 asset type-forwards the types exposed by the System.IO.Ports namespace to the assemblies you will find in the .NET Framework installation. You will use these if you are building a .NET Framework application (like a console application of web site).
This means that when you are consuming your library on .NET Core, you are using the implementation that was ported and made to work on .NET Core.
When you are using your library on .NET Framework you will use the implementation that is part of .NET Framework.

Categories

Resources