-1

I am creating a library that I want to have different behaviour based on the type of project that is dependent on it.

Here are the 3 types of projects I want to separate:

  • AspNet (.NET Framework)
  • AspNetCore 2.1
  • AspNetCore 3.1

Here is my csproj:

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
  <PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
  <PackageReference Include="System.Net.Http" Version="4.3.4" />

  <Content Remove="NetCore\**" />
  <Compile Remove="NetCore\**" />
  <EmbeddedResource Remove="NetCore\**" />
  <None Remove="NetCore\**" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">
  <PackageReference Include="Microsoft.AspNetCore.All" />
  <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />

  <Content Remove="NetFramework\**" />
  <Compile Remove="NetFramework\**" />
  <EmbeddedResource Remove="NetFramework\**" />
  <None Remove="NetFramework\**" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
  <FrameworkReference Include="Microsoft.AspNetCore.App" />

  <Content Remove="NetFramework\**" />
  <Compile Remove="NetFramework\**" />
  <EmbeddedResource Remove="NetFramework\**" />
  <None Remove="NetFramework\**" />
</ItemGroup>

As you can see, I have conditional builds for each of the platforms that I want to target.

  • For normal apps and ASP.NET we have '$(TargetFramework)' == 'netstandard2.0'
  • For ASP.NET Core 2.1 we have '$(TargetFramework)' == 'netcoreapp2.1'
  • For ASP.NET Core 3.1 we have '$(TargetFramework)' == 'netcoreapp3.1'

Here are my dependencies in nuspec:

<dependencies>
    <group targetFramework="netstandard2.0">
        <dependency id="TweetinviAPI" version="5.0.0" />
        <dependency id="Microsoft.AspNetCore.WebUtilities" version="2.2.0" />
        <dependency id="System.Net.Http" version="4.3.4" />
    </group>
    <group targetFramework=" netcoreapp2.1">
        <dependency id="TweetinviAPI" version="5.0.0" />
        <dependency id="Microsoft.AspNetCore.All" />
        <dependency id="Microsoft.AspNetCore.Http.Abstractions" version="2.1.1" />
    </group>
    <group targetFramework="netcoreapp3.1">
        <dependency id="TweetinviAPI" version="5.0.0" />
        <dependency id="Microsoft.AspNetCore.App" />
    </group>
</dependencies>

When adding the nuget package to any of the projects, I receive the following error:

  • ASPNetCore 2.1
TweetinviAPI.AspNet 5.0.0 does not provide an inclusive lower bound for dependency Microsoft.AspNetCore.App. An approximate best match of Microsoft.AspNetCore.App 2.1.0 was resolved.  WebApplication

TweetinviAPI.AspNet 5.0.0 does not provide an inclusive lower bound for dependency Microsoft.AspNetCore.All. An approximate best match of Microsoft.AspNetCore.All 2.0.0 was resolved.  WebApplication-2.1
  • ASPNetCore 3.1
TweetinviAPI.AspNet 5.0.0 does not provide an inclusive lower bound for dependency Microsoft.AspNetCore.App. An approximate best match of Microsoft.AspNetCore.App 2.1.0 was resolved.  

This version of Microsoft.AspNetCore.App is only compatible with the netcoreapp2.1 target framework. Please target netcoreapp2.1 or choose a version of Microsoft.AspNetCore.App compatible with netcoreapp3.1.

Project is not working, any idea to make this work?

Thanks, Linvi

Linvi
  • 2,077
  • 1
  • 14
  • 28
  • 1
    Questions: 1. What happens when you add the .NET Standard DLL to a project? 2. Those "approximate best match of ... was resolved" messages look like warnings rather than errors to me, what errors do you get when you try to add the ASPNETCore 2.1 to another project? 3. Have you tried just adding a `version` number attribute to the `Microsoft.AspNetCore.All` and `Microsoft.AspNetCore.App` dependencies? – simon-pearson Apr 01 '20 at 18:47
  • Question 1 -> This works if I provided the additional dependencies – Linvi Apr 01 '20 at 19:15
  • Question 3 -> I have not as it is recommended not to do so, but I will try to see what happens. – Linvi Apr 01 '20 at 19:18
  • Question 3 -> Adding a version is not respected. -> installed version 2.0.0 with a warning: `TweetinviAPI.AspNet 5.0.0 does not provide an inclusive lower bound for dependency Microsoft.AspNetCore.All. An approximate best match of Microsoft.AspNetCore.All 2.0.0 was resolved.` – Linvi Apr 01 '20 at 19:36
  • Using a nuspec file is the "old way" of packing projects. With SDK style projects, you can just use `dotnet pack`, and it will automatically figure out dependencies and versions for the package. Have you tried this? – zivkan Apr 01 '20 at 21:56
  • @zivkan This is not possible for me as I need to pack up multiple dll. `dotnet pack` does not support such a feature (from my knowledge). – Linvi Apr 01 '20 at 23:45

1 Answers1

0

For anyone interested, this is what I ended up doing.

<dependencies>
    <group targetFramework="netstandard2.0">
        <dependency id="TweetinviAPI" version="5.0.0" />
        <dependency id="Microsoft.AspNetCore.WebUtilities" version="2.2.0" />
        <dependency id="System.Net.Http" version="4.3.4" />
    </group>
    <group targetFramework="netcoreapp2.1">
        <dependency id="TweetinviAPI" version="5.0.0" />
        <dependency id="Microsoft.AspNetCore.Http" version="2.1" />
        <dependency id="Microsoft.AspNetCore.Http.Abstractions" version="2.1" />
        <dependency id="Microsoft.Extensions.Options" version="2.1" />
    </group>
    <group targetFramework="netcoreapp3.1">
        <dependency id="TweetinviAPI" version="5.0.0" />
    </group>
</dependencies>
Linvi
  • 2,077
  • 1
  • 14
  • 28