Port .NET Framework libraries to .NET Core
Learn how to port .NET Framework library code to .NET Core, where it runs cross-platform and expands the reach of the apps that use it.
This article assumes that you:
- Are using Visual Studio 2017 or later. (.NET Core isn't supported on earlier versions of Visual Studio.)
- Understand the recommended porting process.
- Have resolved any issues with third-party dependencies.
You should also become familiar with the content of the following articles:
This article describes the formal specification of .NET APIs that are intended to be available on all .NET implementations.
Developing Libraries with Cross Platform Tools
This article explains how to write libraries using the .NET Core CLI.
Additions to the csproj format for .NET Core
This article outlines the changes that were added to the project file as part of the move to csproj and MSBuild.
Porting to .NET Core - Analyzing your Third-Party Party Dependencies
This article discusses the portability of third-party dependencies and what to do when a NuGet package dependency doesn't run on .NET Core.
Retarget to .NET Framework 4.7.2
If your code isn't targeting .NET Framework 4.7.2, we recommended that you retarget to .NET Framework 4.7.2. This ensures the availability of the latest API alternatives for cases where the .NET Standard doesn't support existing APIs.
For each of the projects you wish to port, do the following in Visual Studio:
- Right-click on the project and select Properties.
- In the Target Framework dropdown, select .NET Framework 4.7.2.
- Recompile the project.
Because your projects now target .NET Framework 4.7.2, use that version of the .NET Framework as your base for porting code.
The next step is to run the API Portability Analyzer (ApiPort) to generate a portability report for analysis.
Make sure you understand the API Portability Analyzer (ApiPort) and how to generate portability reports for targeting .NET Core. How you do this likely varies based on your needs and personal tastes. The following sections detail a few different approaches. You may find yourself mixing steps of these approaches depending on how your code is structured.
Deal primarily with the compiler
This approach works well for small projects or projects that don't use many .NET Framework APIs. The approach is simple:
- Optionally, run ApiPort on your project. If you run ApiPort, gain knowledge from the report on issues you'll need to address.
- Copy all of your code over into a new .NET Core project.
- While referring to the portability report (if generated), solve compiler errors until the project fully compiles.
Although it is unstructured, this code-focused approach often resolves issues quickly. A project that contains only data models might be an ideal candidate for this approach.
Stay on the .NET Framework until portability issues are resolved
This approach might be the best if you prefer to have code that compiles during the entire process. The approach is as follows:
- Run ApiPort on a project.
- Address issues by using different APIs that are portable.
- Take note of any areas where you're prevented from using a direct alternative.
- Repeat the prior steps for all projects you're porting until you're confident each is ready to be copied over into a new .NET Core project.
- Copy the code into a new .NET Core project.
- Work out any issues where you noted that a direct alternative doesn't exist.
This careful approach is more structured than simply working out compiler errors, but it's still relatively code-focused and has the benefit of always having code that compiles. The way you resolve certain issues that couldn't be addressed by just using another API varies greatly. You may find that you need to develop a more comprehensive plan for certain projects, which is covered in the next approach.
Develop a comprehensive plan of attack
This approach might be best for larger and more complex projects, where restructuring code or completely rewriting certain areas of code might be necessary to support .NET Core. The approach is as follows:
- Run ApiPort on a project.
- Understand where each non-portable type is used and how that affects overall portability.
- Understand the nature of those types. Are they small in number but used frequently? Are they large in number but used infrequently? Is their use concentrated, or is it spread throughout your code?
- Is it easy to isolate code that isn't portable so that you can deal with it more effectively?
- Do you need to refactor your code?
- For those types that aren't portable, are there alternative APIs that accomplish the same task? For example, if you're using the WebClient class, you might be able to use the HttpClient class instead.
- Are there different portable APIs available to accomplish a task, even if it's not a drop-in replacement? For example, if you're using XmlSchema to parse XML but don't require XML schema discovery, you could use System.Xml.Linq APIs and implement parsing yourself as opposed to relying on an API.
- If you have assemblies that are difficult to port, is it worth leaving them on .NET Framework for now? Here are some things to consider:
- You may have some functionality in your library that's incompatible with .NET Core because it relies too heavily on .NET Framework or Windows-specific functionality. Is it worth leaving that functionality behind for now and releasing a temporary .NET Core version of your library with less features until resources are available to port the features?
- Would a refactor help?
- Is it reasonable to write your own implementation of an unavailable .NET Framework API? You could consider copying, modifying, and using code from the .NET Framework reference source. The reference source code is licensed under the MIT License, so you have significant freedom to use the source as a basis for your own code. Just be sure to properly attribute Microsoft in your code.
- Repeat this process as needed for different projects.
The analysis phase could take some time depending on the size of your codebase. Spending time in this phase to thoroughly understand the scope of changes needed and to develop a plan usually saves you time in the long run, particularly if you have a complex codebase.
Your plan could involve making significant changes to your codebase while still targeting .NET Framework 4.7.2, making this a more structured version of the previous approach. How you go about executing your plan is dependent on your codebase.
It's likely that you'll mix the above approaches on a per-project basis. You should do what makes the most sense to you and for your codebase.
Port your tests
The best way to make sure everything works when you've ported your code is to test your code as you port it to .NET Core. To do this, you'll need to use a testing framework that builds and runs tests for .NET Core. Currently, you have three options:
Ultimately, the porting effort depends heavily on how your .NET Framework code is structured. A good way to port your code is to begin with the base of your library, which is the foundational components of your code. This might be data models or some other foundational classes and methods that everything else uses directly or indirectly.
- Port the test project that tests the layer of your library that you're currently porting.
- Copy over the base of your library into a new .NET Core project and select the version of the .NET Standard you wish to support.
- Make any changes needed to get the code to compile. Much of this may require adding NuGet package dependencies to your csproj file.
- Run the tests and make any needed adjustments.
- Pick the next layer of code to port over and repeat the prior steps.
If you start with the base of your library and move outward from the base and test each layer as needed, porting is a systematic process where problems are isolated to one layer of code at a time.