0

I've created a .NET project of type Analyzer with Code Fix (.NET Standard) in Visual Studio 2017. The analyzer created by this project type could be deployed as either a NuGet package or a VSIX extension. I used the Generate NuGet Package on Build setting in project properties and it works very smoothly and without any problem.

The thing is, now I want to add some dependencies to the created NuGet package. I know that this is possible through a .nuspec file for other situations. But in this project type, VS creates the NuGet package in bin folders and there is no .nuspec file whatsoever.

So how can I add dependencies to the created NuGet package in the above scenario?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Mo Sadeghipour
  • 489
  • 8
  • 25
  • *there is no .nuspec file whatsoever* - Yes there is, it is inside of the file. A NuGet file is a `.zip` file with a different extension. So, you can unzip it, make changes to the `.nuspec` file, and then zip it again. – NightOwl888 Feb 21 '18 at 08:51
  • @NightOwl888 I changed the extension of the package from .nupkg to .zip, unzipped it, changed .nuspec file to add dependencies, zipped it again and changed the extension of the file back to .nupkg but that didn't change anything! – Mo Sadeghipour Feb 21 '18 at 09:46
  • *I used the NuGet package option* - Could you clarify what you mean by this? You can create a NuGet package directly in VS 2017 project by setting it to "Generate NuGet Package on Build" in project properties or you can create it using [dotnet pack](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-pack?tabs=netcore2x) or [nuget pack](https://learn.microsoft.com/en-us/nuget/tools/cli-ref-pack). Which did you use? – NightOwl888 Feb 21 '18 at 09:55
  • @NightOwl888 I used the first one, directly in VS 2017 – Mo Sadeghipour Feb 21 '18 at 09:56

1 Answers1

1

In Visual Studio 2017 using the new .csproj format, the NuGet dependencies are automatically copied into the NuGet package based on the .csproj file's PackageReferences.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard1.3;net45</TargetFrameworks>
    <DefineConstants Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(DefineConstants);LIBLOG_PORTABLE</DefineConstants>
  </PropertyGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">

    <!-- /* Package references for .NET Standard 1.3 */ -->
    <PackageReference Include="Microsoft.CSharp" Version="4.4.0" />
    <PackageReference Include="System.IO.MemoryMappedFiles" Version="4.3.0" />
    <PackageReference Include="System.Resources.ResourceManager" Version="4.3.0" />
    <PackageReference Include="System.Reflection.TypeExtensions" Version="4.4.0" />
    <PackageReference Include="System.Runtime" Version="4.3.0" />
    <PackageReference Include="System.Threading.Thread" Version="4.3.0" />
  </ItemGroup>

</Project>

So the only thing extra you need to do is add those dependencies from NuGet to your project. This can be done either by

  1. Manually editing the project file (Right-click the project in Solution Explorer and choose Edit <projectName>.csproj)
  2. Installing the NuGet package via NuGet Package Manager

Changing Dependencies by Updating a .nupkg File

Alternatively, since a .nupkg file is just a .zip file with a different extension, you can change its contents:

  1. Unzip it with a standard zip utility to a temporary directory
  2. In the temporary directory, modify the contents of its .nuspec file, adding additional dependencies as appropriate
  3. Zip the contents of the temporary directory again
  4. Rename the new .zip file, giving it the extension .nupkg
NightOwl888
  • 55,572
  • 24
  • 139
  • 212