Nuget/.NET Core, how to hide dependencies in package

365 views Asked by At

I have the following (simplified) setup using .NET Core 3.1 (same situation in .NET Framework)

Project A (a low level Tools library) references the NLog package. The Tools package provides our custom wrapper around NLog and exposes our logging interface.

  1. Project A is deployed as a nuget package.

  2. Project B references Project A.

PROBLEM: I am currently able to have using NLog; in my C# files in project B even though NLog is not referenced in project B. The purpose of the tools project A, is to encapsulate and hide NLog, so ideally it should not be possible in project B to use NLog directly.

Question: Is it possible to somehow prevent NLog (or in general any public types in the dependencies of project A) from being visible in project B?

NOTE: I am thinking there might a way to do this using nuspec, but I coudn't find it.

UPDATE: The solution in reference comment does not work, because when we call a method from Project A which references the nuget NLog we get a type Initialization exception. System.IO.FileNotFoundException : Could not load file or assembly 'NLog, Version=4.0.0.0

Perhaps Jon Skeet is correct in his comment that there is no solution for this?

I am thinking it might be possible to somehow copy the NLog.Dll (make it available in the "Files" tag, so that it is present without being directly referenced, but will need to test and this solution is not very elegant even if it works.

0

There are 0 answers