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

问题描述

我创建了一个 nuget 包,其中在 build 文件夹中包含 .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>

访问被拒绝错误显示在上述错误中。请指出 .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下的所有缓存文件,删除文件夹{ {1}},然后重新安装 nuget 软件包的新发行版。