C
C#2mo ago
Denis

NuGet package project references

I need to share a project from my solution as a NuGet, to provide base implementation types for my application's plugin system. Let's call this project BasePlugin. BasePlugin has project dependencies that must be supplied with the produced NuGet package. I was able to configure that; the built nuspec file contains the required project references. Done based on: https://dev.to/yerac/include-both-nuget-package-references-and-project-reference-dll-using-dotnet-pack-2d8p However, projects referencing this NuGet cannot access types from the BasePlugin dependencies. How do I go about this? Not sure how to search for this.
1 Reply
Latch
Latch2mo ago
When you create a NuGet package from your project, it should automatically include information about its dependencies. This means that when another project uses your NuGet package, it should also have access to the types from your package's dependencies. For example, if your 'BasePlugin' package uses Newtonsoft.Json, projects that reference 'BasePlugin' should be able to use Newtonsoft.Json classes too. This happens because NuGet handles what's called "transitive dependencies" - it brings in not just your package, but also the packages your package depends on. If this isn't working as expected, it's worth checking your project file and NuGet package settings to ensure all dependencies are correctly specified and included. Imagine this is your BasePlugin project (that gets built into a nuget package). It has a reference to Newtonsoft.Json:
// BasePlugin.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>

// BasePlugin.cs
using Newtonsoft.Json;

namespace BasePlugin
{
public class PluginBase
{
public string Serialize<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
}
}
// BasePlugin.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>

// BasePlugin.cs
using Newtonsoft.Json;

namespace BasePlugin
{
public class PluginBase
{
public string Serialize<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
}
}
Now, if you had a consumer that is using that package, that would also be able to use the Newtonsoft.Json package, because it's a transitive dependency.
// ConsumerProject.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BasePlugin" Version="1.0.0" />
</ItemGroup>
</Project>

// Program.cs in ConsumerProject
using BasePlugin;
using Newtonsoft.Json.Linq; // This should work if dependencies are correctly included

class Program
{
static void Main(string[] args)
{
var plugin = new PluginBase();
var json = plugin.Serialize(new { Name = "Test" });
var jObject = JObject.Parse(json); // Using Newtonsoft.Json from the dependency
Console.WriteLine(jObject["Name"]);
}
}
// ConsumerProject.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BasePlugin" Version="1.0.0" />
</ItemGroup>
</Project>

// Program.cs in ConsumerProject
using BasePlugin;
using Newtonsoft.Json.Linq; // This should work if dependencies are correctly included

class Program
{
static void Main(string[] args)
{
var plugin = new PluginBase();
var json = plugin.Serialize(new { Name = "Test" });
var jObject = JObject.Parse(json); // Using Newtonsoft.Json from the dependency
Console.WriteLine(jObject["Name"]);
}
}
Want results from more Discord servers?
Add your server