在针对.NET Framework的Linux上构建NuGet软件包

编程入门 行业动态 更新时间:2024-10-17 13:38:05
本文介绍了在针对.NET Framework的Linux上构建NuGet软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个NuGet程序包,它可以同时明确地同时面向 .NET Framework 4.6.2和.Net Standard 1.5.这是VS 2017中的.csproj缩写文件:

I want to create a NuGet package that can simultaneously and explicitly target both .NET Framework 4.6.2 and .Net Standard 1.5. Here's an abbreviated .csproj file from VS 2017:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net462;netstandard1.5</TargetFrameworks> ... </PropertyGroup> </Project>

当我从本地Windows计算机执行dotnet build和pack命令时,NuGet程序包的创建与预期的一样完美.

When I execute the dotnet build and pack commands from my local Windows machine, the NuGet package is created perfectly well as expected.

但是,当我尝试在Linux上执行相同的dotnet命令时,出现以下错误:

However, when I attempt to execute the same dotnet commands on Linux, I receive the following error:

/opt/dotnet/sdk/1.0.4/Microsoft.Common.CurrentVersion.targets(1111,5): 错误MSB3644:框架的参考程序集 找不到".NETFramework,Version = v4.6.2".为了解决这个问题, 为此框架版本安装SDK或Targeting Pack或 将您的应用程序重新定位到您所针对的框架版本 安装了SDK或Targeting Pack.请注意,程序集将 可以从全局程序集缓存(GAC)中解析,并将在 参考装配体的位置.因此,您的程序集可能不会 正确定位到您想要的框架.

/opt/dotnet/sdk/1.0.4/Microsoft.Common.CurrentVersion.targets(1111,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.2" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

然后我突然意识到Linux盒子上没有任何常规的.NET Framework程序集(更不用说了.因此,看来我将无法使用Linux来构建我的NuGet软件包.适用于定位包",但仅适用于Windows.

It then dawned on me that there aren't any regular .NET Framework assemblies on the Linux box (let alone . Thus, it appears that I won't be able to use Linux to build my NuGet package. I searched around for a "Targeting Pack", but it's only available for Windows.

有人冒着天真的风险,有没有人成功地在Linux上构建可以针对.NET Framework的NuGet软件包?

At the risk of sounding naïve, is anyone successfully building NuGet packages on Linux that can target .NET Framework?

推荐答案

.NET CLI的发行版不包含.NET Framework的任何参考程序集,因此其MSBuild版本无法解析所需的编译时资产.不过,此方案在GitHub上跟踪,并且在迁移到MSBuild之前已经起作用(CLI可以使用Mono的参考程序集.

The distribution of the .NET CLI doesn't contain any reference assemblies for .NET Framework so its version of MSBuild cannot resolve the needed compile-time assets. This scenario is tracked on GitHub though and has worked before the migration to MSBuild (the CLI could use mono's reference assemblies).

尽管有一些替代方法可以用来在非Windows计算机上构建您的库:

There are a few alternatives though that can be used to build your library on non-windows machines:

1.使用mono 5+构建库.

这可能是最稳定的路径.

This is probably the most stable path.

Mono 5和更高版本包含构建.NET Standar和.NET Core应用程序所需的构建逻辑.在Linux上,可能需要将mono的msbuild作为单独的软件包安装.因此,代替以下常用命令

Mono 5 and higher contains the needed build logic to build .NET Standar and .NET Core applications. On linux, mono's msbuild may need to be installed as a separate package. So instead of the following commonly used commands

dotnet restore dotnet build dotnet publish -c Release

您将使用mono的msbuild执行以下操作:

you would use mono's msbuild to do the following:

msbuild /t:Restore msbuild msbuild /t:Publish /p:Configuration=Release

单声道< 5.2:

唯一的限制是mono(<5.2)无法开箱即用地生成NuGet软件包,但是有解决方法,其中涉及在项目中使用NuGet.Build.Tasks.Pack NuGet软件包,允许您通过这样修改项目文件来执行msbuild /t:Pack /p:Configuration=Release(尤其要注意在<Project>元素):

The only limitation is that mono (< 5.2) cannot produce NuGet packages out of the box but there is a workaround involving using the NuGet.Build.Tasks.Pack NuGet package in the project which allows you to do msbuild /t:Pack /p:Configuration=Release by modifying the project file like this (especially note the removed Sdk="..." attribute on the <Project> element):

<Project> <PropertyGroup> <NuGetBuildTasksPackTargets>junk-value-to-avoid-conflicts</NuGetBuildTasksPackTargets> </PropertyGroup> <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" /> <!-- All your project's other content here --> <ItemGroup> <PackageReference Include="NuGet.Build.Tasks.Pack" Version="4.0.0" PrivateAssets="All" /> </ItemGroup> <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" /> </Project>

2.使用.NET CLI并告诉MSBuild使用Mono的参考程序集.

在构建net*目标框架时,可以将FrameworkPathOverride属性设置为环境变量或csproj文件中的属性.它需要指向一组参考程序集-这里可以使用mono的参考程序集.但是某些文件包含一个特殊文件(红色列表),该文件包含对其他目录的引用,.NET CLI中的MSBuild版本无法遵循这些目录.但是,它确实可以在很多情况下工作:

When building for net* target frameworks, you can set the FrameworkPathOverride property both as an environment variable or as a property in the csproj file. It needs to point to a set of reference assemblies - mono's reference assemblies can be used here. But some contain a special file (redist list) containing references to other directories which the MSBuild version in the .NET CLI cannot follow. It does work in a lot of scenarios though:

export FrameworkPathOverride=/usr/lib/mono/4.5/ dotnet build -f net45

已使用此文件,并且由F#团队记录.

3.使用包含参考程序集的NuGet程序包.

在某些MyGet提要上,Microsoft发布了包含参考程序集的NuGet程序包.尽管它们不是公开的或官方的",所以此过程可能会在某个时间点失败.但是,他们确实计划调查将此路径正式化.

On some MyGet feeds, Microsoft publishes NuGet packages containing reference assemblies. They are not published or "official" though so this process may fail at some point in time. However they do plan to investigate making this path official.

首先在解决方案的目录中创建一个NuGet.Config文件,并添加以下内容以添加供稿:

First create a NuGet.Config file in your solution's directory with to following contents to add the feed:

<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="dotnet-core" value="dotnet.myget/F/dotnet-core/api/v3/index.json" /> </packageSources> </configuration>

然后,您可以添加一个项目组以将PackageReference添加到目标包中,并添加一个PropertyGroup来设置参考程序集的路径,如下所示:

Then you can add an item group to add the PackageReference to a targeting pack and a PropertyGroup to set the path to the reference assemblies like this:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks> </PropertyGroup> <PropertyGroup Condition=" '$(TargetFramework)' == 'net461' "> <RuntimeIdentifier>win7-x64</RuntimeIdentifier> <FrameworkPathOverride>$(NuGetPackageFolders)microsoft.targetingpackframework.v4.6.1\1.0.1\lib\net461\</FrameworkPathOverride> </PropertyGroup> <ItemGroup Condition=" '$(TargetFramework)' == 'net461' "> <PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.6.1" Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" /> </ItemGroup> </Project>

如果您使用本机资产(例如,获取linux的.so文件)或在构建库时将其完全删除,则可以为不同的平台更改RuntimeIdentifier.

You can change the RuntimeIdentifier for different platforms if you use native assets (e.g. to get .so files for linux) or remove it entirely when building libraries.

更多推荐

在针对.NET Framework的Linux上构建NuGet软件包

本文发布于:2023-11-16 11:38:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:软件包   Framework   NET   NuGet   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!