Search code examples
wpf.net-coreclass-library.net-framework-version

Consume .NET Core Class Library in .NET Framework app


My .NET Core class library provides a model for my database and my WPF application needs to reference this class library in order to access the database. Is it possible to add a reference from the WPF application to the class library and use this model?

  • My Class Library is built on .NET Core 3.1
  • My WPF app is built on .NET Framework 4.7.2

I use Entity Framework Core to access the database and my model is generated by EF Core tool.


Solution

  • You have got a class library for .NET Core 3.1 using Microsoft.EntityFrameworkCore and a .NET Framework 4.7.2 application that shall reference this library. There are different options that differ in effort and limitations.

    Migration to .NET Core

    Since .NET Core 3.0, WPF is supported for Windows applications. That means you can migrate your application to .NET Core 3.1 and directly consume your library project. There are lots of tutorials out there, see MSDN for reference. Potential issues for migration include the following aspects.

    • You cannot or do not want to drop .NET Framework
    • Differences in .NET Core WPF implementation and runtime behavior
    • References / Dependencies to .NET Framework projects or packages
    • C++\CLI interop dependencies

    Usually for small projects migration should be easy and straight-forward, so you can give it a try. If it does not work out for you or you run into issues, the .NET Standard approach might be more convenient for you.

    Migration to .NET Standard

    An alternative is to migrate your .NET Core library to .NET Standard. To share the library between both frameworks, you can at most target .NET Standard 2.0, because .NET Framework does not support anything beyond that. Fortunately, the Microsoft.EntityFrameworkCore library complies with this requirement. However, there are also potential blockers.

    • You cannot change the library project itself or it is an external library
    • You have dependencies to packages that do not target .NET Standard <= 2.0
    • You rely on .NET Core specific code that you cannot replace

    If you can migrate .NET Standard and want to support both .NET Framework and .NET Core this is the way to make your library reusable and easily maintainable. In fact, if your dependencies and your code comply with .NET Standard 2.0 migration will almost take no effort.

    Multi-Targeting

    For the sake of completeness, there is also the option to multi-target your library or the application project. In this process there are two versions of the corresponding project, one for each framework. However, in your case there is no need to do that and you will not benefit from the effort put in.