将 Nuget 包中的非托管 DLL 包含到 Web 部署包中

Include Unmanaged DLL from Nuget package to web Deploy package(将 Nuget 包中的非托管 DLL 包含到 Web 部署包中)

本文介绍了将 Nuget 包中的非托管 DLL 包含到 Web 部署包中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Nuget 包,其中包含非托管 DLL 和目标将此 DLL 复制到输出文件夹:

I have Nuget package which contains unmanaged DLL and target to copy this DLL to output folder:

<?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Target Name="AfterBuild" DependsOnTargets="CopyFilesToOutputDirectory">
          <ItemGroup>
              <MyPackageSourceFile Include="$(SolutionDir)packagessomepackageunmanaged*.dll" />
          </ItemGroup>
          <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(OutputPath)" />
      </Target>
</Project>

这在我构建项目时非常有效(使用 Visual Studio)

This perfectly works when I am building project(Using Visual Studio)

但是,如果我想创建发布包(到文件系统,或再次使用 VS 进行 Web 部署),则不包括这些 dll.

However if I want to create publish package(To File System, or Web Deploy again using VS) those dlls are not included.

有什么建议吗?

推荐答案

将 Nuget 包中的非托管 DLL 包含到 Web 部署包中

Include Unmanaged DLL from Nuget package to web Deploy package

您可以在 NuGet 包中的目标 AfterBuild 之后添加另一个目标,以将这些非托管 DLL 文件动态包含到您的项目文件中,或者简单地将目标添加到项目文件中.

You can add another target after target AfterBuild in your NuGet package to dynamically include those unmanaged DLL files to your project file or simple add the target to the project file.

为此,请在 NuGet 包中添加一个目标顺序为 AfterTargets="AfterBuild" 的目标:

To accomplish this, add a target with target order AfterTargets="AfterBuild" in your NuGet package:

  <Target Name="AddUnmanagedDll" AfterTargets="AfterBuild">  
    <ItemGroup>
      <Content Include="$(OutputPath)*.dll" />
    </ItemGroup>
  </Target>

但是这个目标会添加所有的 dll 文件,包括托管的 dll 文件.为了解决这个问题,我们需要更改之前的目标 AfterBuild 以添加另一个复制任务,将那些非托管的 dll 文件复制到单独的文件夹中:

But this target will add all dll files, including managed dll files. To resolve this issue, we need to change previous target AfterBuild to add another copy task to copy those unmanaged dll files to a separate folder:

  <Target Name="AfterBuild" DependsOnTargets="CopyFilesToOutputDirectory">
    <ItemGroup>
      <MyPackageSourceFile Include="$(SolutionDir)packagessomepackageunmanaged*.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(OutputPath)" />
    <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(ProjectDir)UnmanagedDll" />
  </Target>

添加另一个复制任务后<Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(ProjectDir)UnmanagedDll"/> 将非托管的dll文件复制到单独的文件夹中<代码>$(ProjectDir)UnmanagedDll.

After add the another copy task <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(ProjectDir)UnmanagedDll" /> to copy the unmanaged dll files to the separate folder $(ProjectDir)UnmanagedDll.

然后我们可以将目标 AddUnmanagedDll 中的 ItemGroup <Content Include="$(OutputPath)*.dll"/> 更改为 <Content Include="UnmanagedDll*.dll"/>

Then we could change the ItemGroup <Content Include="$(OutputPath)*.dll" /> in the target AddUnmanagedDll to <Content Include="UnmanagedDll*.dll" />

所以 NuGet 包中的目标应该是:

So the targets in the NuGet package should be:

  <Target Name="AfterBuild" DependsOnTargets="CopyFilesToOutputDirectory">
    <ItemGroup>
      <MyPackageSourceFile Include="$(SolutionDir)packagesapp.1.0.0unmanaged*.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(OutputPath)" />
    <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(ProjectDir)UnmanagedDll" />
  </Target>

  <Target Name="AddUnmanagedDll" AfterTargets="AfterBuild">  
    <ItemGroup>
      <Content Include="UnmanagedDll*.dll" />
    </ItemGroup>
  </Target>

发布项目后,那些非托管的都包含在 web Deploy 包中:

After publish the project, those unmanaged are included in the web Deploy package:

这篇关于将 Nuget 包中的非托管 DLL 包含到 Web 部署包中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 Nuget 包中的非托管 DLL 包含到 Web 部署包中

基础教程推荐