1

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.

shelbypereira
  • 2,097
  • 3
  • 27
  • 50
  • There's no way of doing this with regular "out of the box" tools. There may well be tools to effectively embed NLog within project A, making types internal etc. But it's not a normal way of doing things. – Jon Skeet Sep 30 '20 at 05:47
  • 1
    https://stackoverflow.com/questions/50875356/how-to-exclude-a-package-from-a-class-library-nuget-package – A Farmanbar Sep 30 '20 at 06:32
  • yes solution in that answer works: I added all to my .csproj and it works. – shelbypereira Sep 30 '20 at 08:09
  • Try to you create a method that references NLog in your library class (Project A) and use/invoke that method from your project B – eduherminio Sep 30 '20 at 08:14
  • @Mr.AF as it turns out the solution in that question does not work. Although that successfully hides the dependency, the Dll is no longer available so when I reference in the parent project I get a type initialization exception. I edit the question to add this info. Jon Skeet may be correct. – shelbypereira Oct 01 '20 at 05:05

0 Answers0