Is there a reliable (even standardized) approach to building a .NET library that supports multiple target frameworks at the same time?

I realize this is a fairly general question, but I couldn't find a reliable resource to solve the following scenario, and it wasn't in the same place. So I decided to ask here, hoping to help others struggling with the same problem:

As a .NET component developer, I want to support a large number of .NET target frameworks. This topic has become very important to me as there has been an explosion of options for target frames lately when the dot net core and dot net standard were introduced .

So, imagine I am writing a C # library MyLib

(which I would call a "product") that compiles to MyLib.dll

. I want to support different ranges of target infrastructures: net35

, net40

, net45

, netstandard1.2

, etc. So I create assemblies (MSBuild or csproj files) for each target framework and bundle them together in one nuget following nuget guidelines for lib folder structure . Thus, a product can be obtained from a single build artifact - the nuget package.

For each version of the target frame, I try to take advantage of its features and benefits, and also provide full caps for lower versions or highlight some of the functionality. In general, the point is that the "product" can be used regardless of the target target structure of the project - I mean that the nuget package for MyLib

must be installed correctly and the corresponding DLL must be consulted .

So, several questions arise:

  • Is it correct to propagate the same assembly version information across different assemblies? How to reuse the same file AssemblyInfo.cs

    ?
  • Which assembly attributes should not be shared between my assemblies? I started another thread regarding the attribute [assembly: Guid("...")]

    ]. Other people display problems in an attribute [assembly: AssemblyTitle("...")]

    and in a different context.
  • Is there a good or standardized approach to organizing the solution to support the above build result. Do most projects really strive to achieve this?

So far, my own way is to use a separate .csproj file for each target framework for the same "product", but as the process evolves, it can get tedious to support multiple projects even if the target frame count is significant now.

+3


source to share


1 answer


So I create assemblies (MSBuild or csproj files) for each target framework and bundle them together in one nuget package.

If you are using the new csproj "SDK" format from Visual Studio 2017, you don't even need to do that - it supports multiple target frameworks. For example:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   <TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
  </PropertyGroup>
</Project>

      



This project supports both .NET Framework 4.5.1 and NETStandard1.3.

This creates compilation conditionals automatically, so you can do things like #if NET451

or #if NETSTANDARD1_3

so you can conditionally apply the code if needed. You can also do this in the .csproj itself to include nuget packages for only one framework. The Dapper project has an example of this in the .csproj file.

Using the new dotnet

cli you can use dotnet pack

to package the whole thing into a nuget package.

+5


source







All Articles