简体   繁体   中英

Include Unmanaged DLL from Nuget package to web Deploy package

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)\packages\somepackage\unmanaged\*.dll" />
          </ItemGroup>
          <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(OutputPath)" />
      </Target>
</Project>

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

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

Any suggestions?

Include Unmanaged DLL from Nuget package to web Deploy package

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.

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>

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)packages\somepackage\unmanaged\*.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(OutputPath)" />
    <Copy SourceFiles="@(MyPackageSourceFile)" DestinationFolder="$(ProjectDir)UnmanagedDll" />
  </Target>

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

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

So the targets in the NuGet package should be:

  <Target Name="AfterBuild" DependsOnTargets="CopyFilesToOutputDirectory">
    <ItemGroup>
      <MyPackageSourceFile Include="$(SolutionDir)packages\app.1.0.0\unmanaged\*.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>

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM