Problems referencing a Net Standard GDAL library from Net Framework - c#

I have a .Net Standard 2.0 libray using Gdal.Core 2.3. I can reference it successfully from a Net5 project, but I get a "Gdal.Core dll not found" error if I reference it from a Net Framework4.7.2. If I manually add the Gdal.Core.dll to the Framework project I get a "Unable to find an entry point named 'CSharp_OSGeofGDAL_AllRegister___'" error.
I have tried using the standard GDAL library on the Framework project and the Gdal.core package on the .Net Standard class. Also, I have also changed different versions, created minimal projects and did a lot of testing, but I am not able to make it work. Any advice

I finally realized I had the same issue with all libraries referenced on the Standard project and not only GDAL. This problem was related with the way Nuget packages are handled in the old .Net Framework and the .Net Standard. The solution is to migrate from the old Nuget.confi system, to the new PackageReference system used in .Net Standard.

Related

Error adding ChromiumWebBrowser to project - C# Visual Studio

I am trying to add the Cef.WinForms library to my project and I'm following the steps found here
https://tutorialslink.com/Articles/DEVELOPING-CHROMIUM-BASED-BROWSER-IN-Csharp/1468. I was able to add the library find using Nuget, however I did get a warning that it expected .NET 4.6.1 and I have .NET 5.0. I tried to manually install 4.6.1 but it will not allow it as it says a newer version is already installed. This may not even be the cause of my error but I wanted to mention it just in case.
When I try to drag the component ChromiumWebBrowser over to my main form, I get the error seen below. I've searched for this error to try and resolve it but I am so far not finding anything helpful. Does anyone know what I'm missing here?
As noted in the comments, when targeting .Net 5.0 you'll need to use the packages with the NETCore suffix.
For .Net Core 3.1/.Net 5.0+
https://www.nuget.org/packages/CefSharp.WinForms.NETCore/
https://www.nuget.org/packages/CefSharp.Wpf.NETCore/
https://www.nuget.org/packages/CefSharp.OffScreen.NETCore/
NOTE A minimum of .Net Core 3.1 is required.
For the .Net 4.5.2 to .Net 4.8
https://www.nuget.org/packages/CefSharp.WinForms/
https://www.nuget.org/packages/CefSharp.Wpf/
https://www.nuget.org/packages/CefSharp.OffScreen/
All packages require Microsoft Visual C++ 2019 or greater.
Ijwhost.dll
To support C++/CLI libraries in .NET Core/.Net 5+, ijwhost was created by Microsoft as a shim for finding and loading the runtime. All C++/CLI libraries are linked to this shim, such that ijwhost.dll is found/loaded when the C++/CLI library is loaded. It's important this dll is distributed with your application.

Upgrading class library project to .net standard 2.1 where this has been referenced by .net framework 4.X projects

We are upgrading our solution of multiple projects. Starting with one class library which is been referenced by 13 projects. I need to upgrade my class library project .NET Framework 4.5 to .NET Standard 2.1.
Here the problem is:
This class library is being referenced by projects of .NET Framework 4.5 and its throwing error that it can't access .NET Standard 2.1 lib
Project 'abc.csproj' targets 'netstandard2.1'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.5'.
How to resolve this problem? Upgrading all those 13 projects is not possible at the same time.
EDIT:
Following comments, if I convert lib to .netstandard1.1 then errors start coming in the library itself:
After downgrading lib to .netstandard1.1, following on
NetStandard 1.4 does not allow decorating class with [DataContract]. I saw this post and added this package System.Runtime.Serialization.Primitives:
I even added reference to older dll but, still getting error:
Adding answer as we've iterated through the comments. At first OP wanted to change framework of library to .NET Standard 2.1 while having other apps under .NET Framework 4.5.
Unfortunately, this is not possible. As specified in the official documentation, .NET Standard 2.1 is not following along with .NET Framework. Latest you can get is .NET Standard 2.0 with .NET Framework 4.6.1. Solution for this issue is to downgrade common library project to .NET Standard 1.1, other app projects should be able to link to this.
Next iteration (after downgrading to .NET Standard 1.1) brought issue related to "unknown classes" and VS/compiler asking for adding reference. There was already existing question, answer is to download NuGet package related to the data serializers.
If your code in end-applications are also using classes/attributes from the affected assembly, you must make a cleanup. So that all code references the only single version of source. It is not possible to have one Attribute taken from Nuget and check for "same attribute/same name" but from different source (system assembly -> System.Runtime.Serialization).

Extra files in the project (NPGSQL)

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.

Why does a dotnet Core App with a reference to a classic .Net project works and compiles [duplicate]

I'd really like to start using .NET Core and slowly migrate applications and libraries to it. However, I can't realistically upgrade my entire code base to use .NET Core and then go through the process of testing and deploying a plethora of applications in production.
As an example, if I create a new .NET Core application and try to reference one of my .NET Framework projects I get the following:
The following projects are not supported as references: -
Foobar.NetFramework has target frameworks that are incompatible with
targets in current project Foobar.NetCore.
Foobar.NetCore: .NETCoreApp,Version=v1.0
Foobar.NetFramework: .NETFramework,Version=v4.5
Is it possible to create a new .NET Core application and reference my existing .NET Framework libraries? If so, what's the process for doing that? I've spent hours going through Microsoft's documentation and searching their issues on GitHub, but I can't find anything official on how to achieve this or what their long-term vision is for this process.
Old question, but with the release of .NetStandard 2.0 and .netcore 2.0 and vs2017.3, the game has changed.
You can use the Full .NET Framework (TFM) with .NetCore 2.0, but how?
In Visual Studio 2017.3, you can reference the Full .NET Framework (any version) directly from within a .NetCore2 project.
You can build the .NetStandard2 class library and reference your TFM. Then reference your .NetStandard2 library from your .NetCore2 project.
For example, referencing json.net net45 from .NetStandard2.
Browse to the folder and select version net45 (not netstandard1.3)
See the dependency in the image below, no yellow warning as you see.
Even if a Nuget library is not ready to be ported to .Netstandard 2, you can use any API in the library that is compliant to net461.
Quoting for the .NET Core 2/Standard 2.0 announcement with links:
.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
Reference: .NET Core App target .NET framework 4.5.2 on Linux
A need to use third-party .NET libraries or NuGet packages not available for .NET Core
So only in cases where the libraries or NuGet packages use technologies that aren't available in .NET Standard/.NET Core, you need to use the .NET Framework.
Reference: Choosing between .NET Core and .NET Framework for server apps
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.
Reference: Announcing .NET Core 2.0
Yes, we are currently attempting the same thing. The trick is to make sure that you are supporting the same .NET frameworks. Inside your project.json file, make sure the framework matches the framework of the project you wish to include. For example:
"frameworks": {
"net46": { --This line here <<<<
"dependencies": {
"DomainModel": {
"target": "project"
},
"Models": {
"target": "project"
}
}
}
},
FYI: You might need to change the framework of your .NET Core or your older projects to achieve this. .NET Core can be changed just by editing the project.json file as seen above. You can so the same in .NET projects by right clicking the project and opening properties. Change the framework level there.
Once you have matched the two project frameworks then you should be able to include them. Good Luck!
We delayed migrations as long as could as it seemed daunting as first. But we got an insistent client who wanted to migrate ASAP.
So we migrated their Fintech Web App developed on .NET Framework 4.8 Web Forms to .NET 6 Razor Page. Our team scoured though hundreds of online resources & spoke to Microsoft Tech Support before we started the project. Hope the high-level walkthrough of our journey help you plan your migrations.
Our .NET Framework Website consisted of 1 .NET Web Forms project and 12 Class Libraries.
Here is how we did it.
Refactored the .NET Framework 4.8 Web Forms code
We ensured that the Web Forms code behind did not have a single line of service or business logic code. When we did find some business logic code in the web forms code behind, we refactored it, by moving it to the class libraries.
Created new .NET Standard projects
We created a new .Standard 2.0 Class library project for every .NET Framework 4.8 Class Library. If the original project was called "FintechProjectName.StockMarketClient", we named the .NET standard project "FintechProjectName.StockMarketClient.Standard".
Copied all files from .NET framework to .NET standard
We copied all the class files from .NET framework to .NET standard projects. We then removed all the .NET framework class libraries from the solution and added references to the new class libraries. All projects compiled on the 1st try itself and all our test cases too passed with minor changes.
Create new .NET 6 Web App Project
We created a new .NET 6 Web App Project. We had to entirely redo the front-end as there is no direct path for migrating Web Forms to Razor Pages. This was the only project which took us about 1 month to migrate.
Reference .NET standard class libraries in the new .NET 6 website
We copied all the .NET Standard libraries to this new solution containing the Razor Pages web site. Added the references and got it to work.
Move from .NET Standard to .NET 6 class libraries
Once the new website was up and running, with all test cases passed, we did the last step in the process which was the simplest. Created .NET 6 class library projects for each of the .NET standard libraries and named the projects appropriately. Copied all class files from .NET standard projects to their corresponding .NET 6 projects. Then we removed the .NET Standard libraries and added references to the new class libraries.
Overall project timelines were about a month and a half, most of it spend on Razor Pages implementation using the same html design.
Note:
If you are using any 3rd party library which does not have a .NET standard or .NET 5 version, then you are out of luck. You will need to find a replacement nuget package and recode your application to use this new library.
In my case with .net6 referencing framework 4.8 library ( both winforms), the trick seems to be to add the reference to the framework dll as a shared reference.

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.

Categories