0

I had created a nuget package which includes .props and .targets in build folder.

Below is my .targets file:

<?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>


I have installed nuget package in c# project. Nuget package is installed successfully. But when i tried to compile my c# project, a build error is thrown see below:

"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]

Build got failed In the below property

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

Access is denied error is shown in the above error. Please suggest what had done wrong in the .targets file

Kalyani Reddy
  • 131
  • 10
  • Hi, any update about this issue? – Sara Liu - MSFT Apr 09 '21 at 02:00
  • Hi, any update about this issue? If my answer helps you handle the issue, please do not forget to [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). And if not, please feel free to let us know:) – Sara Liu - MSFT Apr 12 '21 at 01:47
  • Hi, any update about this issue? If my answer helps you handle the issue, please do not forget to [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). And if not, please feel free to let us know:) – Sara Liu - MSFT Apr 14 '21 at 07:44

1 Answers1

0

The problem is that you did not import the assembly dll correctly.

Also, you should remove \ after MSBuildThisFileDirectory, it already contains that.

Change this part:

<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"/>

If the custom msbuild task ResolveReferences is written under Sample.dll, then you should add the assembly dll rather then assembly folder into AssemblyFile.

Then, re-pack your nuget package into a new release version, uninstall the old version nuget package under the main project, delete all cache files under C:\Users\xxx\.nuget\packages and delete the folder <solution_folder>\packages, then reinstall the new release version of nuget package.

Sara Liu - MSFT
  • 6,007
  • 1
  • 21
  • 27