Create libraries that can be used in both the. NET core and the. NET Framework and publish to NuGet

Source: Internet
Author: User

We often see the following types of packages when referencing a class library on NuGet

This means that it can be used with. NETFramework or projects that use. NET core, respectively. Such a package can take into account different framework versions of the project, but it is convenient to simply publish a package on NuGet.

So how do you create a generic class library like this?

Start by using vs2017 to create a new standard library

Edit Csproj File

Edit the label tagetframework, add the marked content

Overloading the project and then building the project can find that two folders have been generated in the debug directory, stating that you have created a class library that can be used for multiple target frameworks, called multi-target libraries

This is not enough because the API provided by NET45 is different from the API provided by Netstandard, such as the way to get the path to the program's working folder

The method of using. NET framework4.5 is

string RootDir = AppDomain.CurrentDomain.BaseDirectory

Using the. NET Core

string rootdir = appcontext.basedirectory;
For such differentiated code we should use conditional compilation method to be compatible, the method is as follows

To view the compilation symbols for a project, create a property, right-click Project

You can see that the build symbol for the project is NET45, and our compatible code can write it

And you can switch between different frame versions in the navigation bar to debug

The final step is to generate a nupkg package that cannot be sent to NuGet, right click on the item

Find NuGet package files under build directory and publish to NuGet

Create libraries that can be used in both the. NET core and the. NET Framework and publish to NuGet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.