If you’ve ever worked on a Sitecore XP project, especially using the Helix patterns, you’ve probably experienced assembly version conflicts with your nuget package dependencies. Essentially, a package referenced in nuget is pulls in a dependency that overwrites an assembly that ships with Sitecore with a newer version, leading to an error.
Sometimes you just need to reference an assembly in your code, but not actually deploy it. At first glance there seems to be no way to do this in the nuget package manager. You can, however, do this directly in the .csproj file.
In this example, I needed to reference Microsoft.AspNet.Owin
to work on a feature using federated authentication. When I published my project, it pulled in a bunch of other DLLs, the package’s transitive dependencies, that overwrote the ones Sitecore shipped with. In this example, the easiest solution is to tell MSBuild to not publish this package, since it’s already in the /bin folder. But, we still need to include this package for compilation.
The solution is to add <IncludeAssets>compile</
to the .csproj in the package declaration. For example, with my IncludeAssets
> Microsoft.AspNet.Owin
package, the package declaration would look like this:
<PackageReference Include="Microsoft.AspNet.Identity.Owin">
<Version>2.2.4</Version>
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
Now when I publish my project, the assembly and its dependencies (Newtonsoft.Json
anyone?) do not publish and overwrite the stock assemblies. For mor info on how to configure package behavior, see this article: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files