Asp.net-Core

成功 dotnet 發布後缺少包

  • March 12, 2021

發布命令工作正常,但執行後,我必須恢復項目,因為缺少包……

因此,如果我執行以下命令:

dotnet 發布 –runtime win-x64

然後發布工作,但之後我的項目中缺少包。

但是,如果我在沒有執行時的情況下執行發布:

點網發布

然後發布工作正常,我沒有任何失去的包。

這是正常行為嗎?我能做些什麼來解決這個問題?這很煩人。

這是我的 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
   <TargetFramework>netcoreapp2.1</TargetFramework>
   <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
   <LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
   <PackageReference Include="AWSSDK.Extensions.NETCore.Setup">
       <Version>3.3.6</Version>
   </PackageReference>
   <PackageReference Include="Microsoft.AspNetCore.App">
   </PackageReference>
   <PackageReference Include="Microsoft.AspNetCore.StaticFiles">
       <Version>2.1.1</Version>
   </PackageReference>
   <PackageReference Include="NSwag.AspNetCore">
       <Version>12.0.13</Version>
   </PackageReference>
   <PackageReference Include="NSwag.MSBuild">
       <Version>12.0.13</Version>
   </PackageReference>
</ItemGroup>
<ItemGroup>
   <ProjectReference Include="..\MyProject.Analytics\MyProject.Analytics.csproj" />
   <ProjectReference Include="..\MyProject.ApiClient\MyProject.ApiClient.csproj" />
   <ProjectReference Include="..\MyProject.CommonApi\MyProject.CommonApi.csproj" />
   <ProjectReference Include="..\MyProject.Common\MyProject.Common.csproj" />
   <ProjectReference Include="..\MyProject.DbAccess\MyProject.DbAccess.csproj" />
   <ProjectReference Include="..\MyProject.Logging\MyProject.Logging.csproj" />
   <ProjectReference Include="..\MyProject.Messaging\MyProject.Messaging.csproj" />
   <ProjectReference Include="..\MyProject.Model\MyProject.Model.csproj" />
   <ProjectReference Include="..\MyProject.Settings\MyProject.Settings.csproj" />
</ItemGroup>
<ItemGroup>
   <Content Update="appsettings.Api.Development.json">
       <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
   </Content>
   <Content Update="appsettings.Api.json">
       <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
   </Content>
   <Content Update="appsettings.Api.Production.json">
       <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
   </Content>
   <Content Include=".ebextensions\never-sleep-again.config">
     <CopyToOutputDirectory>Always</CopyToOutputDirectory>
   </Content>
   <Content Update="appsettings.Api.PreProduction.json">
     <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
     <CopyToOutputDirectory>Always</CopyToOutputDirectory>
   </Content>
</ItemGroup>
<ItemGroup>
 <Folder Include="MyProjectlogs\Development" />
</ItemGroup>
<ItemGroup>
   <ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
<Target Name="NSwag" AfterTargets="Build">
   <Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" />
   <Exec Command="$(NSwagExe_Core21) run nswag.json /variables" />
   <RemoveDir Directories="$(OutDir)References" />
</Target>

編輯:從還原 nugets 日誌中,我為我發布的項目的每個依賴項獲得以下資訊

@   Project 'MyProject.Api' is affected (InstallCount = 0)

所以它實際上認為有些東西是不同的,但似乎沒有安裝任何東西。

挖了一圈,發現了這個文章

有趣的部分是:

restore 和 build 可以作為另一個命令的一部分隱式執行,例如發布。當作為另一個命令的一部分隱式執行時,它們會提供額外的上下文,以便生成正確的工件。當您使用執行時(例如,dotnet publish -r linux-x64)發佈時,隱式還原會恢復 linux-x64 執行時的包。如果您顯式呼叫 restore ,預設情況下它不會恢復執行時包,因為它沒有那個上下文。

基本上我在執行時 .netcore 2.1.1 與恢復 2.1.X (X != 1) 之間存在不匹配。

解決方案(在上面的連結中解釋)是將其添加到項目文件中:

<PropertyGroup>
   ...
   <RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
   <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>

引用自:https://stackoverflow.com/questions/56775338