[英]How can i make dll.config to be part of Nuget package
由於 dll.config 不受 .NET 支持,並且在創建 nuget 包時,dll.config 不是 package 的一部分。
有沒有辦法讓我將 dll.config 作為 nuget package 的一部分? 如果有人使用我的 nuget package 他們應該在他們的放置位置同時獲得 *.dll 和 *.dll.config 文件
如果可能的話,想要一些建議。
已經嘗試過上述要求並且能夠打包 dll 配置,請嘗試如下您可以將 dll 配置文件與 dll 文件捆綁在一起,只需使用文件標簽即可
<file src="path\\to\\dllconfigfile\\*.*" target="lib\\net35" />
如果您使用的是 SDK 樣式的項目,並假設您之前已將應用程序配置文件添加到項目 ( App.Config
),則只需將以下內容添加到您的.csproj
文件中:
<ItemGroup>
<None Update="App.config">
<Pack>True</Pack>
<PackagePath>lib\$(TargetFramework)\$(AssemblyName).dll.config</PackagePath>
</None>
</ItemGroup>
我最近使用它在部署包時自動包含程序集綁定重定向。
什么對我有用:
簡而言之:將 .props 和 .targets 文件添加到在使用 nuget 包的項目中執行的 nuget 包並將 .config 文件復制到輸出目錄:
我添加了一個<PackageId>.props
和<PackageId>.targets
文件(將<PackageId>
替換為包的真實名稱):
<PackageId>.props
:
<Project>
<ItemGroup>
<ConfigFiles Include="$(MSBuildThisFileDirectory)../contentFiles/any/any/<PackageId>.config" />
</ItemGroup>
</Project>
<PackageId>.targets
:
<Project>
<Target Name="CopyConfigFiles" BeforeTargets="Build" Condition="!$(TargetFramework.StartsWith('netstandard'))">
<Copy SourceFiles="@(ConfigFiles)" DestinationFolder="$(TargetDir)" />
</Target>
</Project>
然后將 .props 和 .targets 文件添加到 nuget 包的build
和buildMultiTargeting
文件夾中,例如將以下內容添加到 .csproj 文件中:
<ItemGroup>
<None Include="App.config" Pack="true"
PackagePath="contentFiles\any\any\$(AssemblyTitle).dll.config" />
</ItemGroup>
<Target Name="CollectRuntimeOutputs" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<None Include="$(PackageId).props" Pack="true" PackagePath="build" />
<None Include="$(PackageId).props" Pack="true" PackagePath="buildMultiTargeting" />
<None Include="$(PackageId).targets" Pack="true" PackagePath="build" />
<None Include="$(PackageId).targets" Pack="true" PackagePath="buildMultiTargeting" />
</ItemGroup>
</Target>
VS 2019, SDK 樣式庫項目。
不知道為什么這是必要的。 似乎它應該是默認值 function。但這對我有用。
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll.config" Pack="True" PackagePath="lib\$(TargetFramework)" />
</ItemGroup>
22 年 4 月 11 日更新
這是針對 SDK 樣式 .NET 框架庫的。 項目文件的相關部分如下:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>...</RootNamespace>
<TargetFramework>net452</TargetFramework>
<Platforms>AnyCPU</Platforms>
<NoWarn>1701;1702;1591</NoWarn>
<AssemblyTitle>...</AssemblyTitle>
<Description>...</Description>
<Company>...</Company>
<Copyright>...</Copyright>
<Version>...</Version>
</PropertyGroup>
<Import Project="$(MSBuildProjectDirectory)\Package.targets" />
...
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
</Project>
您可以在下面看到項目文件包含package.targets
的位置。 這是所有 NuGet 打包代碼所在的位置。
這個 package 包括庫的發布和調試版本,帶有.targets
include 以復制到構建的適當版本中。 因此, OutputPath
固定在第 45-59 行。
您正在尋找的是第 108-114 行:
<!--****************************************************************************
* NuGet Package Configuration
*****************************************************************************-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!--=======================================================================-->
<!-- T4 template used to create `build\{tfm}\$(AssemblyName).targets` for -->
<!-- inclusion in the `.nupkg` file. Requires `dotnet-t4` being installed. -->
<!--=======================================================================-->
<BuildTargetsFile>Build.targets.tt</BuildTargetsFile>
<!--===========================================================================-->
<!-- Normally set `true` so deterministic Source Link URLs are generated and -->
<!-- all source requests will go to the remote repository. Setting this to -->
<!-- `false` (debugging only) will look for the source files in the local -->
<!-- directory where this library was built before looking to the remote repo. -->
<!--===========================================================================-->
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<!--====================================-->
<!-- Temporary folder where other tools -->
<!-- puts files required by this file. -->
<!--====================================-->
<TempFilesPath>$(SolutionDir)\output</TempFilesPath>
</PropertyGroup>
<ItemGroup>
<!--================================================================-->
<!-- Change Source Link host to proxy where GitLab repo URLs are -->
<!-- converted to GitLab API requests with required authentication. -->
<!--================================================================-->
<SourceLinkGitLabHost Include="gitlab.com" ContentUrl="https://gitlabproxy.loc" />
</ItemGroup>
<!--========================================================-->
<!-- `Version.targets` is updated by `npm version` command. -->
<!--========================================================-->
<Import Project="$(MSBuildProjectDirectory)\Version.targets" />
<!--==============================================-->
<!-- Fix the output paths so we can build a NuGet -->
<!-- package with both Release and Debug builds. -->
<!--==============================================-->
<PropertyGroup>
<DebugOutputPath>bin\Debug</DebugOutputPath>
</PropertyGroup>
<Choose>
<When Condition="'$(Configuration)' == 'Debug'">
<PropertyGroup>
<OutputPath>$(DebugOutputPath)</OutputPath>
</PropertyGroup>
</When>
<When Condition="'$(Configuration)' == 'Release'">
<PropertyGroup>
<OutputPath>bin\Release</OutputPath>
</PropertyGroup>
</When>
</Choose>
<!--==================================================================-->
<!-- Nuget Package can only be created in Release mode, using `dotnet -->
<!-- pack`, after the Debug build, and must include the Debug output. -->
<!-- -->
<!-- Title tag missing from UI. -->
<!-- https://github.com/dotnet/project-system/issues/2937 -->
<!--==================================================================-->
<PropertyGroup>
<Title>$(AssemblyTitle)</Title>
<Authors>$(Company)</Authors>
<!--===========================================================-->
<!-- Include `.pdb` file in `.nupkg` so it will be downloaded -->
<!-- with the `.dll`. GitLab does not provide a symbol server. -->
<!-- https://gitlab.com/gitlab-org/gitlab/-/issues/342157 -->
<!--===========================================================-->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<!--<IncludeSymbols>true</IncludeSymbols>-->
<!--<SymbolPackageFormat>snupkg</SymbolPackageFormat>-->
</PropertyGroup>
<!--===========================================================-->
<!-- Build Intellisense file in Release mode. This is required -->
<!-- for `pack` to include the file in the NuGet package. -->
<!--===========================================================-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<!--==============================================================================-->
<!-- Add files to include in `.nupkg` file before packing. -->
<!-- -->
<!-- These are temporary files so we don't want them referenced in the project -->
<!-- but only included files can be packed (`Pack="True"` is ignored for `<None -->
<!-- Remove="..."/>`). To keep the references out of the project, these files -->
<!-- are only included here. -->
<!-- -->
<!-- https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target -->
<!--==============================================================================-->
<Target Name="BeforePack" BeforeTargets="_GetPackageFiles">
<!--===============================================================-->
<!-- Pack is only allowed in Release build. Debug build must have -->
<!-- already been done so debug assembly can be added to `.nupkg`. -->
<!--===============================================================-->
<Error Condition="'$(Configuration)' != 'Release'" Text="`pack` target is only available in 'Release' mode." />
<!--==================================================-->
<!-- This copies the library config file to the -->
<!-- package. Seems like this shouldn't be necessary. -->
<!--==================================================-->
<ItemGroup Condition="Exists('$(OutputPath)\$(AssemblyName).dll.config')">
<None Include="$(OutputPath)\$(AssemblyName).dll.config" Pack="True" PackagePath="lib\$(TargetFramework)" />
</ItemGroup>
<!--=================================================-->
<!-- Pack debug assembly files into `libdbg` folder. -->
<!--=================================================-->
<ItemGroup>
<None Include="$(DebugOutputPath)\$(TargetFramework)\$(AssemblyName).dll" Pack="True" PackagePath="libdbg\$(TargetFramework)" />
<None Include="$(DebugOutputPath)\$(TargetFramework)\$(AssemblyName).pdb" Pack="True" PackagePath="libdbg\$(TargetFramework)" />
</ItemGroup>
<!--==========================================-->
<!-- Copy Intellisense file created from -->
<!-- Doxygen output over the file VS created. -->
<!--==========================================-->
<copy Condition="Exists('$(TempFilesPath)\$(AssemblyName).xml')" SourceFiles="$(TempFilesPath)\$(AssemblyName).xml" DestinationFolder="$(OutputPath)" />
<!--==================================================-->
<!-- Create the `build\{tfm}\$(AssemblyName).targets` -->
<!-- file from T4 template and add to package. -->
<!-- -->
<!-- https://github.com/nogic1008/T4Sample -->
<!--==================================================-->
<PropertyGroup>
<BuildTargetsPath>$(IntermediateOutputPath)\$(AssemblyName).targets</BuildTargetsPath>
</PropertyGroup>
<Exec WorkingDirectory="$(ProjectDir)"
Command="dotnet t4 "$(BuildTargetsFile)" -o "$(BuildTargetsPath)" -pAssemblyName="$(AssemblyName)"" />
<ItemGroup>
<None Include="$(BuildTargetsPath)" Pack="True" PackagePath="build\$(TargetFramework)\" />
</ItemGroup>
</Target>
<!--=============================================-->
<!-- Source Link -->
<!-- https://github.com/dotnet/sourcelink#gitlab -->
<!--=============================================-->
<ItemGroup>
<!--======================================================================================================-->
<!-- Assembly Info generated into `AssemblyInfo.cs` and embedded in `.dll`. -->
<!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#assembly-attribute-properties -->
<!--======================================================================================================-->
<EmbeddedFiles Include="$(GeneratedAssemblyInfoFile)" />
</ItemGroup>
<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!--=================================================-->
<!-- Embed generated `AssemblyInfo.cs`. -->
<!-- https://github.com/dotnet/sourcelink/issues/572 -->
<!--=================================================-->
<!--<EmbedUntrackedSources>true</EmbedUntrackedSources>-->
<TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
</PropertyGroup>
</Project>
希望能幫助到你。
22 年 4 月 15 日更新
好吧,我的錯。 事實證明,這適用於 package.dll.config 文件中的 .nupkg 文件,但在構建時,它不會復制到使用項目的bin
文件夾中。
參考上面的文件,替換為:
<!--==================================================-->
<!-- This copies the library config file to the -->
<!-- package. Seems like this shouldn't be necessary. -->
<!--==================================================-->
<ItemGroup Condition="Exists('$(OutputPath)\$(AssemblyName).dll.config')">
<None Include="$(OutputPath)\$(AssemblyName).dll.config" Pack="True" PackagePath="lib\$(TargetFramework)" />
</ItemGroup>
有了這個,修復它:
<!--==================================================================-->
<!-- This includes the library config file in th package file and -->
<!-- copies it to the consumer bin folder. Seems like this shouldn't -->
<!-- be necessary. -->
<!-- -->
<!-- https://github.com/dotnet/sdk/issues/3249#issuecomment-662562709 -->
<!--==================================================================-->
<ItemGroup Condition="Exists('App.config')">
<None Include = "App.config"
Pack = "true"
PackagePath = "contentFiles/any/$(TargetFramework)/$(TargetFileName).config"
PackageCopyToOutput = "true">
</None>
</ItemGroup>
但是,請注意一點。 雖然這.dll.config
文件復制到使用項目的bin
文件夾,但它不會以傳遞方式執行此操作。 也就是說,如果package被項目B消費,項目A消費了,那么.dll.config
的.dll.config會被復制到項目B的bin
文件夾下,而不會復制到項目A的bin
文件夾下,即使.dll
和.pdb
文件將是。 必須將 package 安裝到項目 A 才能使配置文件副本生效。
到目前為止,我還沒有找到解決辦法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.