1

When creating a new library MyAPI.dll, I am referencing many other (non-standard) libraries such as RestSharp.dll, Newtonsoft.dll,MyUtilities.dll, etc. My library works fine in my development environment because I've downloaded all of those other libraries and they're sitting in my project's bin folder, but as soon as I try to publish that library and use it in a new location, it fails because the referenced libraries cannot be found.

How to I set up my MyAPI.csproj project so that these dlls/libraries get packaged into my published .dll file, and future users of MyAPI.dll don't have to worry about downloading and referencing those dependencies?

Thought this would be simple, but my google-fu is weak today. Setting those external references to CopyLocal = False removes them from the /bin/ directory, giving the illusion that they are getting packaged into MyAPU.dll, but really Visual Studio is just adding them to my Global Assembly Cache (GAC), which doesn't help future users of the API.

4

3 回答 3

4

有两种选择(据我所知):

  1. 合并
  2. 嵌入式资源和 Assembly.Resolve(参见Jeffrey Richter

首先,您可以使用ILMerge,这是一个命令行程序,可以将多个 .NET 程序集合并在一起,创建一个输出文件。它不能合并 WPF 项目。可以添加到构建后事件以使合并自动进行。

其次是将库作为嵌入资源添加到您的项目中,然后注册到Assembly.Resolve 事件并在需要时从资源中加载程序集。Jeffrey Richter 关于此方法的文章:Jeffrey Richter

第二种方法有一个主要缺点,它不适用于将多个库合并为一个(它只能用于将库添加到可执行文件中),至少在没有其他工具的 c# 中是这样。要将库添加到库中,您必须使用另一个工具,该工具在 Jeffrey 在第二页的文章评论中提到:(模块初始化程序注入)。将库嵌入到其他库中的问题是您不能(至少在 c# 中)注册到程序集.Resolve 在需要嵌入库之前的事件,因此您需要使用Module initializer injection将注册注入到模块初始化程序。也可以设置为构建事件,用工具写在apge上。这听起来可能很复杂,但一旦你设置它就很容易了。

于 2013-06-04T21:02:15.640 回答
3

有一个免费的 nuget 包“Costura.Fody”,它将依赖程序集作为资源打包到您的程序集中。该解决方案适用于 WPF 和其他托管程序集。

如果依赖程序集不在执行文件夹中,则自动获取打包的程序集。它还自动配置您的 msbuild 目标,以便在构建期间打包依赖项。您不必在程序集中添加代码。它还允许您配置在 xml 文件中打包或不打包哪些程序集。

它结合了两种方法:

  • Jeffrey Richter 建议使用嵌入式资源作为一种合并程序集的方法。
  • Einar Egilsson 建议使用 cecil 创建模块初始化程序。

您可以在此处找到文档:https ://github.com/Fody/Costura/blob/master/README.md

于 2013-07-04T10:38:54.237 回答
0

它不是免费的(有试用版),但我的一个朋友告诉我一个名为.NET Reactor的程序,它能够将带有依赖 DLL 的 exe 打包成一个可执行文件,非常值得一看。

我想说下一个最直接的选择是 ClickOnce,这里有一个很好的教程

于 2013-06-04T20:57:06.040 回答