I have a solution with some projects:
client app - UWP project
device sdk - UWP project (because of bluetooth APIs)
client app - ASP.Net Core 1.1 targeting .Net Core 2.0
client app plugins
some projects with shared model and interface classes.
I would like to use .net standard projects as much as possible (for the shared projects and preferably for the plugins) because they are more lightweight and can be edited without unloading them in Visual Studio for example.
Referencing .net standard 1.4 libraries FROM an UWP project goes fine, but referencing an UWP project FROM a .net standard 1.4 project results in an error:
Project DeviceSDK is not compatible with netstandard1.4 (.NETStandard,Version=v1.4).
Project DeviceSDK supports: uap10.0.15063 (UAP,Version=v10.0.15063)
Is this is there any workaround for this?
That's simply impossible. A cross platform library won't be able to reference a platform specific one.
I refactored the interfaces and data models of the device SDK out of that project and create a .net standard DeviceSdk.Core project. This project can then be referenced from application logic projects and the DeviceSdk main project.
So the structure becomes roughly:
ClientApp - UWP project, referencing all projects but the ClientApp.Frontend project
DeviceSdk - UWP project (because of bluetooth APIs), referencing DeviceSdk.Core
DeviceSdk.Core - .net standard 1.4
ClientApp.Frontend - ASP.Net Core 1.1 targeting .Net Core 2.0, referencing ClientApp.Core
ClientApp.Plugins - .net standard 1.4, referencing ClientApp.Core and DeviceSdk.Core
ClientApp.Core (some projects with shared model and interface classes) - .net standard 1.4
.net standard class library can't reference UWP libs. That would break the whole concept of .net Standard.
The idea is that a class library that targets specific .net standard version can only reference limited set of APIs defined by the standard.
For example, .net standard v1.4 only allows API-s listed within this file:
https://github.com/dotnet/standard/blob/master/docs/versions/netstandard1.4_ref.md
This list is frozen and is supposed to remain intact.
Related
I'm porting my WPF application from .NET Framework to .NET Core 3. I got this generic UI library that is published on NuGet, that is currently targeting .NET Framework v4.6.1. Now, if I just change the target to .NET Core 3, then it won't work anymore in .NET Framework projects. .NET Standard is designed to solve this kind of problem but I don't think it will help in the case of WPF projects.
What's the right thing to do here? Publish and manage 2 separate packages?
Then this library has 2 other derived libraries. If I create 2 separate packages for the base library, that means I'd have to publish and manage 2 of each derived libraries as well. This solution stinks. Got anything better?
A WPF application that targets .NET Core 3 may actually reference and consume a control library or NuGet package that targets .NET Framework 4.6.1.
You shouldn't have to do anything with the control library itself for this to work, assuming you don't use any APIs that is not present in .NET Core because then you will eventually get runtime errors.
Try to add a reference to the DLL or install the NuGet package as usual. It should work even if the NuGet package only contains a single assembly in lib\net461.
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.
I recently switched all of my class libraries in one of my solutions to be ASP.NET Core 2.0 class libraries when they were net461 libraries previously.
I usually build and then copy and paste the library dlls to a project in another solution and then simply reference them in VS.
This does not seem to be working anymore. Do I have to make these new 2.0 libraries local NuGet packages instead?
Do I need to change the TargetFramework of my .NET Core class libraries to be .NET Standard 2.0?
Do I need to change the TargetFramework of my .NET Core class libraries to be .NET Standard 2.0?
It depends. If all of your class libraries (including the applications that depend on them) target .NET Core, then no you shouldn't need to.
But if you intend the libraries to be shared between .NET Core and .NET Framework applications (or other platforms .NET Standard supports), then you should make them .NET Standard.
I do have an ASP.NET MVC Core app and would like to add a Class library in my project.
I added it in my project via "Add Reference" > "Browse" > select DLL and done.
I added it on code like
using PagarMe; // works good on code
And I was able to compile and run the app. however when the user goes to a page where the lib is referenced then I got the fallowing error.:
FileNotFoundException: Could not load file or assembly 'PagarMe.Pcl, Version=2.0.6372.16631, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
What I already done.
I checked the output bin folder and the Dlls files are there.
Both DLLs are compiled with "Any CPU" configuration.
I tried PCL and non-PCL Version.
App Target framework: .NETCoreApp 1.1
Default Class Library Target Framework: .Net Framework 4.5.
Portable (PCL) Class Library Target Framework: .Net Framework 4.5 and ASP.Net Core 1.0
What can I do in order to use Class Library or PCL library into my Core App?
In .NET Core you no longer use Portable Class Libraries, but you target the correct version of the .NET Standard Library. Some PCL or Shared Classes may use some unsupported
references.
To solve this please try one of this:
1. Rebuild your Class Library to target .NET Standard. .NET Standard 1.6, for instance, is supported by both .NET Core 1.0 and .NET Framework 4.6.1.
.NET Standard can be thought of as the next generation of Portable Class Libraries (PCL). The .NET Standard improves on the experience of creating portable libraries by curating a standard BCL and establishing greater uniformity across .NET runtimes as a result. A library that targets .NET Standard is a PCL or a ".NET Standard-based PCL". Existing PCLs are "profile-based PCLs". (Taken from the documentation)
2. Target your app to .NET Framework.
You could build an ASP.NET Core application to target the full .NET Framework in stead of .NET Core only. This gives you the advantages of ASP.NET Core, without the limits of .NET Core.
ASP.NET Core != .NET Core
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.