简体   繁体   English

为什么应用程序需要一些 nuget 包引用?

[英]Why are some nuget package references required by applications?

I have a question that has been bothering me for awhile.我有一个问题困扰了我一段时间。 I ran across this problem a few years back when I was dealing with writing a generic logging wrapper around some hosted provider instances using log4net.几年前,当我使用 log4net 处理围绕某些托管提供程序实例编写通用日志记录包装程序时,我遇到了这个问题。

The idea was simple enough, I wanted to write a logging and metrics that hid all the implementation in a separate visual studio project so when you wanted to add any telemetry support to another application you could just include the project, new up an instance of the logger and start logging using generic calls.这个想法很简单,我想编写一个日志记录和指标,将所有实现隐藏在一个单独的 Visual Studio 项目中,所以当你想向另一个应用程序添加任何遥测支持时,你可以只包含该项目,新建一个实例logger 并使用通用调用开始记录。 If you ever switched providers or tweak logging settings, it wouldn't require any changes to the host applications.如果您曾经切换过提供程序或调整日志记录设置,则不需要对主机应用程序进行任何更改。

This creates a strong decoupling point, where the main application used an interface in a logging class library, but would know nothing about the packages or providers that the logging class library was using to do the real work.这创建了一个强解耦点,其中主应用程序使用日志类库中的接口,但对日志类库用于执行实际工作的包或提供程序一无所知。

When I did this and tried out using Loggly's nuget package and log4net, I found that the calling application had to have a ref to the nuget package or else the dependent assembly would not be copied to the build directory.当我这样做并尝试使用 Loggly 的 nuget 包和 log4net 时,我发现调用应用程序必须有一个对 nuget 包的引用,否则依赖程序集不会被复制到构建目录中。 At the time I just wrote this off as something odd that they Loggly engineers were doing.当时我只是把这写成他们 Loggly 工程师正在做的一些奇怪的事情。 But I have since encountered the same behavior in some, but not all other packages.但此后我在某些包中遇到了相同的行为,但不是所有其他包。 (DogstatsD doesn't have a problem, Raygun does, etc..) (DogstatsD 没有问题,Raygun 有问题,等等。)

I have noticed that some nuget packages in assemblies are automatically copied into the parent output directory, but when I look for the setting that controls this, I cannot find it.我注意到程序集中的一些 nuget 包会自动复制到父输出目录中,但是当我查找控制它的设置时,我找不到它。

I have written dozens of class libraries over the years, and I have never had a problem with 'chained dependency assemblies (a refs b, b refs c, etc.) resolving when I build.多年来,我编写了数十个类库,并且我从未遇到过在构建时解决“链接依赖程序集(a refs b、b refs c 等)”的问题。 It only seems to be some nuget packages that are a problem.似乎只是一些有问题的 nuget 包。

How do I force nuget packages referenced by a class library project to copy into the build directory without an explicit reference in the application?如何强制类库项目引用的 nuget 包复制到构建目录中,而无需在应用程序中明确引用?

Ok, I figured this one out.好的,我想通了这一点。

This is a problem only for the Log4Net & Loggly wrapper assembly combo in particular because it is referenced entirely at runtime.这仅是 Log4Net 和 Loggly 包装器程序集组合的问题,特别是因为它在运行时完全被引用。 Log4net loads up its required log appenders at runtime, and because .net doesn't see a ref to the assembly at build time, it assumes that it isn't being used and omits copying the required assembly to the bin directory. Log4net 在运行时加载其所需的日志附加程序,并且因为 .net 在构建时没有看到对程序集的引用,它假定它没有被使用并省略将所需的程序集复制到 bin 目录。 The solution when you know this is simple, just write an empty dummy method in the referenced library that you can call in the main application.当您知道这很简单时,解决方案很简单,只需在引用的库中编写一个空的虚拟方法,您可以在主应用程序中调用该方法。 This will cause .net to include the assembly in the build.这将导致 .net 在构建中包含程序集。

While this problem is specific to the Log4net library, it could occur anywhere that you are using an assembly that is only used with runtime reflection.虽然此问题特定于 Log4net 库,但它可能发生在您使用仅用于运行时反射的程序集的任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM