Extra files in the project (NPGSQL) - c#

The question is stupid enough, but it didn’t work with NuGet before. After installing the NuGet package through the console, to connect to PostgreSQL, files appeared that NetFramework should contain. Without copying these files, the project does not start. What to do to get rid of them and not copy them to the directory with the program. In my understanding, these files should be taken from the framework.
See screenshots:
All need files in project C#

My guess is that you have .Net Standard 2.0 libraries/dependencies in your project.
.NET 4.6.1 might add additional runtime dependencies in your output folder:
.NET 4.6.1, 4.6.2, .NET 4.7 and 4.7.1 all have partial .NET Standard
2.0 support in the natively shipped runtimes, but they still are .NET Standard 2.0 compliant by adding additional runtime dependencies into
your output folder to provide the missing functionality. NuGet along
with the runtime targeting handles automatically adding those
dependencies to your projects to provide the needed runtime support
for those extra features. A lot of those assemblies override behavior
from the base framework and .NET uses runtime redirects to route api
calls to the appropriate assemblies rather than than mscorlib.dll or
other system assemblies.
.NET 4.7.2 is the first version of the full .NET Framework that is
fully .NET Standard 2.0 compliant without any additional dependencies.
See: https://weblog.west-wind.com/posts/2019/Feb/19/Using-NET-Standard-with-Full-Framework-NET
In other words, targeting .NET 4.7.2 should get rid of the additional files.

Related

Adding .NET Core Multi-target to a .NET Framework library

I have a .NET Framework library with NuGet package dependencies. This library is consumed exclusively by .NET Framework projects. Some of them by local project references, and most through NuGet. I publish the library to a private NuGet server.
A few projects have cropped up using .NET Core based infrastructure, and I would like to make my standard library available to these projects. I am not switching any of the existing infrastructure to .NET Core. I don't want to introduce .NET Core dependencies into every existing .NET Framework project. I have noticed that when I install certain multi-targeted libraries, sometimes they include "netstandard" or something like that -- and install lots of dependencies. These are unacceptable and get immediately uninstalled from .NET Framework projects.
Is it possible to multi-target my .NET Framework (4.72 if it matters) project to .NET Core as well, while still producing native .NET Framework libraries? I can copy and paste all the code in my library into a netcoreapp3.1 project, install NuGet dependencies, and that code runs fine. However, I don't want to port this library to .NET Core, and then multi-target it back to .NET Framework. As far as I am aware, that is not the same as having a native implementation. I just want to be able to use the library in a .NET Core app without copying and pasting the entire library, and letting the versions get out of sync. The less ceremony the better.
My csproj file doesn't have a <TargetFramework or <TargetFrameworks> property. It has a <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> property.
Is what I'm trying to do possible? I can't tell from the documentation I have read.

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.

Why referencing a .Net standard nuget package in a .Net 4.7.2 project import a lot of .Net core lib?

I'm having a .Net 4.7.2 application, in which I want to reference the package OpcFoundation.NetStandard.Opc.Ua. This project as a list of dependencies for .Net 4.6 that is quite small.
But when I install it, I get like 50+ additional packages to install. Is there a way to reduce this? I feel that a lot of thoses classes are already existing in the full .Net project(System.Threading.Tasks/Timer/...).
Thank you
If you look at many of these types (which are supplied by .NET Standard packages, not .NET Core), you'll find that the specific version that's used against .NET 4.7.2 will be an empty assembly just containing lots of TypeForwardedTo attributes pointing right back at the full-flavour .NET Framework.
So you still end up using the exact types you always would have done. There's just extra indirections which allows .NET Standard to work with both .NET Framework, .NET Core and other .NET Standard implementations.

Dynamic Link Library, Solution can't load assembly

Recently I started learning how to create Dynamic Link Libraries in Visual Studio with C#.
I followed online instructions on how to create DLLs:
https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio
http://www.c-sharpcorner.com/UploadFile/1e050f/creating-and-using-dll-class-library-in-C-Sharp/
https://blogs.msdn.microsoft.com/benjaminperkins/2017/04/13/how-to-make-a-simple-dll-as-an-assembly-reference-just-for-fun/
After completing these steps, I tried to add my .dll file to my projects.
On execution I receive this message:
System.IO.FileNotFoundException: 'Could not load file or assembly
'MySql.Connect, Version=1.0.0.2, Culture=neutral, PublicKeyToken=null'. The
system cannot find the file specified.'
I selected .NET Framework 4.6.1 when I started my project. The target framework in the Properties file under the Application tab is selected as .NET Framework 2.0.
The project that should reference to the file is targeting the .Net Core 2 Framework.
I've been at this problem for nearly a week and searched for online solutions.
Any help will be greatly appreciated.
You need to put the DLL somewhere that the runtime can find it. The easiest thing to do would be to put it in the same directory as the .exe, but you have other options as well (see How the Runtime Locates Assemblies).
It is important to carefully decide what platform you intend to use your DLL with.
I selected.NET Framework 4.6.1 when I started my project.
If you mean when you started your DLL project, then you have limited the types of applications that can consume this DLL to .NET Framework 4.6.1+.
Properties file under the Application tab is selected as .Net Framework 2.0.
If I understand correctly, you changed the DLL target from .NET Framework 4.6.1 to .NET Framework 2.0. This widens the compatibility so that the consuming library can be .NET Framework 2.0+. However, this is at the expense of all of the newer features of the .NET Framework.
Do note that official support (i.e. patches) for .NET Framework 2.0 has been gone for several years, and newer machines aren't likely to have it installed.
The project that should reference to the file is targeting the .Net Core 2 Framework.
Here's the crux of your issue. .NET Core ain't .NET Framework. It is a completely different platform.
That said, .NET Core has some limited support for referencing .NET Framework assemblies, but is it sure not to work with .NET Framework 2.0 (which again, hasn't been supported in years). Also, this "compatibility mode" probably means you lose cross-OS support, which is one of the main benefits of .NET Core.
Option 1
So, the knee jerk answer is to make your DLL target .NET Core if you want to use it with .NET Core applications.
Option 2
However, there is also an option to make a portable DLL that works with .NET Framework 4.5+ and .NET Core - make your DLL target .NET Standard.
See How to port from .net framework to .net standard for instructions on changing your DLL to target .NET Standard.

Nuget package manager installs dependencies for another target

So I have a DLL which is targeting multiple versions (.net 4.5, 4.6.1, netcore 2.0) which is pushed to a Klonkdike
Now I want to use this DLL, my project is .net 4.6.1, so I expect to resolve dependencies on this target only.
However my packages.config gets all .netcore dependencies. How can I prevent that?
This is the DLL:
And this is what is added when fetch this package:
I would expect to have only UAParser to be added and no other change since I already have the dependencies.
There are 3 folders in the lib folders of the package, so I would expect to only need these specific dependencies...
How can I avoid add all these dependencies?
UAParser is a pure .NET Standard based library.
.NET Standard versions lower than 2.0 depend on these libraries. However, the new tooling in VS 2017 (make sure you have at least 15.5.0) trims out these packages during build and makes sure the right assemblies are in place (these may also be System.* dll files but are not coming from these packages).
These dlls put in place by tooling are needed to implement the .NET Standard contract on .NET Framework - they forward to .NET Framework implementations.
In the upcoming .NET 4.7.2, the plan is to no longer need any of these DLLs. Until then, they are necessary.

Categories