构建错误 - 安装 Nuget 包后访问被拒绝

Build error - Access is denied after installing Nuget package

我创建了一个 nuget 包,其中包含构建文件夹中的 .props 和 .targets。

下面是我的 .targets 文件:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build"
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 
  
   <PropertyGroup>
    <UserTargetsPath>$(MSBuildProjectFullPath).user</UserTargetsPath> 
  </PropertyGroup>
  
   <PropertyGroup>
    <SDKInstallPath Condition=" '$(SDKInstallPath)' == ''">$(MSBuildThisFileDirectory)\..\lib\net46</SDKInstallPath>
    <SetupPath>$(SDKInstallPath)\Sample.dll</SetupPath>
    <SDKExtDir Condition=" '$(SDKExtDir)' == ''">$(SDKInstallPath)</SDKExtDir>
    <MyExtension>$(SDKInstallPath)\Sample.dll</MyExtension>
  </PropertyGroup>
  
 <UsingTask TaskName="ResolveReferences" AssemblyFile="$(SDKInstallPath)"/>
 
 <ItemGroup>
    <MyExtension Include="$(MyExtension)"  Condition=" '$(MyExtension)' != '' " ></MyExtension>
  </ItemGroup>
    
     <PropertyGroup>
    <BuildDependsOn>
      BeforeBuild;
      CoreBuild;
      AfterBuild
    </BuildDependsOn>
  </PropertyGroup>  
  
 
     
  <Target
    Name="Build" DependsOnTargets="$(BuildDependsOn)"
   />
   

  
    <Target Name="BeforeBuild" />
    
   
   <Target Name="AfterBuild" />
 
   
   
<PropertyGroup>
    <PrepareForBuildDependsOn></PrepareForBuildDependsOn>
  </PropertyGroup>
  
  
  <Target
    Name="CoreBuild">


    <CreateProperty Condition=" '$(MyExtensionSearchPaths)' == '' " Value="
     $(ReferencePaths);
      {HintPathFromItem};
      {RawFileName};
       $(SDKExtDir)
      ">
      <Output TaskParameter="Value" PropertyName="MyExtensionSearchPaths" />
    </CreateProperty>
    
    <ResolveReferences
      MyReferences="@(MyExtension)"
      SearchPaths="$(MyExtensionSearchPaths)"
      SearchFilenameExtensions=".dll">
      <Output TaskParameter="ResolvedMyReferences" ItemName="_AllResolvedMyExtensionPaths" />
      
    </ResolveReferences>

 
  </Target>


</Project>


我已经在 c# 项目中安装了 nuget 包。 Nuget 包安装成功。 但是当我尝试编译我的 c# 项目时,抛出了一个构建错误,见下文:

"C:\MySample\MySample.csproj" (Build target) (1) ->
(CoreBuild target) ->
  C:\MySample\packages\Sample.4.8.31\build\Sample.targets(69,5): error MSB4062: 
The "ResolveReferences" task could not be loaded from the assembly C:\MySample\packages\Sample.4.8.31\build\..\lib\net46. 
Could not load file or assembly 'file:///C:\MySample\packages\Sample.4.8.31\lib\net46' 
or one of its dependencies. Access is denied. Confirm that the <UsingTask> declaration 
is correct, that the assembly and all its dependencies are available, and that the 
task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\MySample\MySample.csproj]

构建失败在下面属性

 <ResolveReferences
      MyReferences="@(MyExtension)"
      SearchPaths="$(MyExtensionSearchPaths)"
      SearchFilenameExtensions=".dll">
      <Output TaskParameter="ResolvedMyReferences" ItemName="_AllResolvedMyExtensionPaths" />
      
    </ResolveReferences>

Access is denied错误如上图所示。请指出 .targets 文件中哪里出了问题

问题是你没有正确导入程序集dll。

此外,您应该删除 MSBuildThisFileDirectory 之后的 \,它已经包含了那个。

更改此部分:

<PropertyGroup>
    <SDKInstallPath Condition=" '$(SDKInstallPath)' == ''">$(MSBuildThisFileDirectory)..\lib\net46</SDKInstallPath>
    <SetupPath>$(SDKInstallPath)\Sample.dll</SetupPath>
    <SDKExtDir Condition=" '$(SDKExtDir)' == ''">$(SDKInstallPath)</SDKExtDir>
    <MyExtension>$(SDKInstallPath)\Sample.dll</MyExtension>
  </PropertyGroup>
  
 <UsingTask TaskName="ResolveReferences" AssemblyFile="$(SDKInstallPath)\Sample.dll"/>

如果自定义 msbuild 任务 ResolveReferences 写在 Sample.dll 下,那么您应该将程序集 dll 而不是程序集文件夹添加到 AssemblyFile

然后,将你的nuget包重新打包成新的release版本,卸载主工程下的旧版本nuget包,删除C:\Users\xxx\.nuget\packages下的所有缓存文件并删除文件夹 <solution_folder>\packages,然后重新安装新发布版本的 nuget 包。