Making a simple dotnet class library available for .net and .net core

Image from http://webjunk.ru/wp-content/uploads/2008/08/target.jpg
Recently I faced a challenge trying to use one of the old libraries in a new freshly baked .Net Core applications. Obviously, .net and .net core are not the same thing so I had some issues trying to get the library working for both new and old apps.

The solution - recreate the library as a .Net Core one and make it target multiple frameworks. This is actually a quite simple exercise that requires only a slight meddling with the .csproj file for the app.

In the end of the day, the project file should look like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;net451;net461</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-version" Version="1.1.0" />
  </ItemGroup>
</Project>

 This results in the library being accessible for multiple frameworks that would just need to import it. An additional benefit is that you could always see how the build process looks like for each target and even see that in VS:
This is all nice and easy. Awesome!
The only minor issue is that now the project property will not tell you what is the target framework when you try to access them from Visual Studio. Not a big deal, really - you still have the targets available as shown on the screenshot.

Comments