Home > Software design >  How to build a .net5 project with .net framework
How to build a .net5 project with .net framework

Time:09-16

I'm trying to build the same project with 2 different framework.

If relevant, the why : Because I need my project to be used as a reference in a .netcore project and a .netframework project, it has to be compatible with both to avoid duplicating the code.

I thought having build configurations would be the way to go, but the configuration manager makes an error stating the configuration is wrong when adding the framework build configuration.

My csproj looks like this

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">netcore</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>
    
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">netframework</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
    </PropertyGroup>
    
</Project>

But it's not working and I'm confused at to why or what the next step can be.

Any clue?

CodePudding user response:

Your csproj looks really confused. It's not clear why Configuration, which is normally Debug or Release, has a netcore or netframework option, for example.

If you want to build for multiple target frameworks, use a single TargetFrameworks element, e.g.

<TargetFrameworks>net5.0;net451</TargetFrameworks>

If you use TargetFrameworks, do not also use TargetFramework.

CodePudding user response:

You can use .NET Standard project. And have reference to it from both of the projects mentioned above. You can find more info here: .NET Standard

Quote from Microsoft themselves:

We recommend you target .NET Standard 2.0, unless you need to support an earlier version. Most general-purpose libraries should not need APIs outside of .NET Standard 2.0. .NET Standard 2.0 is supported by all modern platforms and is the recommended way to support multiple platforms with one target.

  • Related