【问题标题】:How can i make dll.config to be part of Nuget package我怎样才能使 dll.config 成为 Nuget 包的一部分
【发布时间】:2014-08-21 03:15:33
【问题描述】:

由于 .NET 不支持 dll.config,并且在创建 nuget 包时,dll.config 不是包的一部分。

有没有办法让我将 dll.config 作为 nuget 包的一部分?如果有人使用我的 nuget 包,他们应该在其放置位置同时获得 *.dll 和 *.dll.config 文件

如果可能的话,希望得到一些建议。

【问题讨论】:

标签: c# .net nuget nuget-spec


【解决方案1】:

如果您使用的是 SDK 样式的项目,并假设您之前已将应用程序配置文件添加到项目 (App.Config),那么只需将以下内容添加到您的 .csproj 文件中:

<ItemGroup>
  <None Update="App.config">
    <Pack>True</Pack>
    <PackagePath>lib\$(TargetFramework)\$(AssemblyName).dll.config</PackagePath>
  </None>
</ItemGroup>

我最近使用它在部署包时自动包含程序集绑定重定向。

【讨论】:

  • 我试过了,.dll.config 文件没有复制到输出文件夹。
  • 是的,这不起作用。 是文件夹路径,而不是文件名。
  • 对我也不起作用。你能分享你的整个 csproj 信息和你正在使用的 dotnet 版本吗?
【解决方案2】:

已尝试上述要求并能够打包 dll 配置,尝试如下 您可以将 dll 配置文件与 dll 文件捆绑在一起,只需使用文件标签即可

&lt;file src="path\to\dllconfigfile\*.*" target="lib\net35" /&gt;

click here -- > include files in nuget package

【讨论】:

    【解决方案3】:

    如果你使用VS2015,那么试试Nuget Package Explorer,它有友好的GUI界面,并且支持文件删除。你可以把文件放在那里。

    【讨论】:

    • 我已经尝试过了,.dll.config 没有复制到引用包的项目的输出文件夹中。
    【解决方案4】:

    什么对我有用:

    简而言之:将 .props 和 .targets 文件添加到在使用 nuget 包并将 .config 文件复制到输出目录的项目中执行的 nuget 包中:

    我添加了&lt;PackageId&gt;.props&lt;PackageId&gt;.targets文件(将&lt;PackageId&gt;替换为包的真实名称):

    &lt;PackageId&gt;.props:

    <Project>
        <ItemGroup>
            <ConfigFiles Include="$(MSBuildThisFileDirectory)../contentFiles/any/any/<PackageId>.config" />
        </ItemGroup>
    </Project>
    

    &lt;PackageId&gt;.targets:

    <Project>
        <Target Name="CopyConfigFiles" BeforeTargets="Build" Condition="!$(TargetFramework.StartsWith('netstandard'))">
            <Copy SourceFiles="@(ConfigFiles)" DestinationFolder="$(TargetDir)" />
        </Target>
    </Project>
    

    然后将 .props 和 .targets 文件添加到 nuget 包的 buildbuildMultiTargeting 文件夹中,例如通过将以下内容添加到 .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>
    
    

    【讨论】:

      【解决方案5】:

      VS 2019,SDK 样式库项目。

      不知道为什么这是必要的。似乎它应该是一个默认功能。但这对我有用。

      <ItemGroup>
        <None Include="$(OutputPath)\$(AssemblyName).dll.config" Pack="True" PackagePath="lib\$(TargetFramework)" />
      </ItemGroup>
      

      22 年 4 月 11 日更新

      这是针对 SDK 样式的 .NET Framework 库。项目文件的相关部分如下:

      <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 打包代码所在的位置。

      此包包含库的发布和调试版本,并带有 .targets 包含以复制到构建的适当版本中。因此,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://docs.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 &quot;$(BuildTargetsFile)&quot; -o &quot;$(BuildTargetsPath)&quot; -pAssemblyName=&quot;$(AssemblyName)&quot;" />
          <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://docs.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>
      

      希望对你有帮助。

      【讨论】:

      • 对我不起作用。你能分享你的整个 csproj 信息和你正在使用的 dotnet 版本吗?
      • @togarha,抱歉耽搁了。没看到你的消息。如果您仍然需要这个,我会尝试通过 COB 发布。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 2020-01-01
      相关资源
      最近更新 更多