DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Building Multi-Targeted .NET Libraries

Building Multi-Targeted .NET Libraries

In this post, a software engineer discusses how his team went about creating an app that support this interesting feature.

Martin Holman user avatar by
Martin Holman
·
Oct. 23, 18 · Tutorial
Like (3)
Save
Tweet
Share
5.95K Views

Join the DZone community and get the full member experience.

Join For Free

We recently shipped a new version of our .NET notifier to our GitHub repo. This is a major upgrade over our previous version and includes new features like breadcrumbs and session tracking. We’ve also expanded our .NET support to include support for ASP.NET Core.

While we gave our notifier a much-needed upgrade, we wanted existing users to experience our new features too. This meant we had to target the library to a wide variety of framework versions. Here we’re sharing a little about how we achieved this.

How to Support Multiple .NET Frameworks in the Past

Until recently, there have been two ways to achieve this. You could create a project file for each framework that you wanted to support, but this is prone to issues when it comes to keeping the files in sync. Adding files and ensuring that everything builds becomes a fragile process. The other option is to add more build configurations to your project file. Each build configuration would target a different framework version. This makes the project files very complex and error-prone.

Our Approach to Supporting Multiple Frameworks

Instead of these approaches, we achieved multi-framework support by using the new features in Visual Studio 2017 and MSBuild.

If you want to target more than one framework you can now use the TargetFrameworks element. The default for a new project file is TargetFramework which defines the compilation target for the library. By changing this to TargetFrameworks you can list many compilation targets. MSBuild will then compile your library for each framework you have provided. You can see an example of this in our new notifier.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <PackageId>Bugsnag</PackageId>
    <Title>Bugsnag .NET Notifier</Title>
    <TargetFrameworks>net35;net40;net45;netstandard1.3;netstandard2.0</TargetFrameworks>
  </PropertyGroup>
</Project>

Now you have a project that produces many .dlls, one for each target framework.

Other Features We’ve Leveraged in MSBuild

Now we have addressed supporting multiple .NET frameworks, but let’s suppose you need to reference some NuGet packages. Things could get complicated due to all these target frameworks. Some packages have different framework requirements between versions. To solve this we can use MSBuild condition attributes. This is not a new feature of MSBuild, but one that is useful in this situation.

Your code also needs to be able to compile across all the frameworks you are targeting. This can be a little tricky as sometimes the code needs to differ between versions. Luckily, you can use compiler directives to help you write code that applies to one or more versions. MSBuild will set appropriate compiler flags when you are using the TargetFrameworks element. You can use the compiler flags to specify certain branches of code to run in specific versions. We used this in our notifier to cope with the reflection changes in .NET Core here.

using System;
using System.Reflection;

namespace Bugsnag
{
  /// <summary>
  /// Handle the reflection differences between the full .net framework and
  /// what is provided by netstandard
  /// </summary>
  public static class Reflection
  {
    public static Assembly GetAssembly(this Type type)
    {
#if (NET35 || NET40 || NET45)       return type.Assembly;
#else       return type.GetTypeInfo().Assembly;
#endif     }
  }
}

Distributing a Multi-Targeted Library

Once you’ve finished building your library, you’ll need to distribute it. This used to mean writing a separate nuspec file and using this to build a package. No more! Now you can include the relevant information right inside your project file.

If you have more than one project that needs to share NuGet properties, there is another feature you can put to use. Put common properties in a Directory.build.props file. MSBuild will include this file by merging its properties with your project files. We used this in our notifier here.

When we decided we had to target our new notifier library to a wide variety of framework versions. We could have taken a path that would lead us to a less maintainable solution. Instead, we were able to keep it simple by using the new features in Visual Studio 2017 and MSBuild.

Learn more about Bugsnag’s .NET error monitoring or try it free for 14-days.

Library .NET Framework MSBuild

Published at DZone with permission of Martin Holman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • Multi-Cloud Database Deep Dive
  • How to Rescue Your Magento 2 Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: