C# .NET 控制台应用程序如何查找引用的程序集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1214269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does a .NET console application look for referenced assemblies?
提问by
I created a console application using C# that references external DLLs. When I run it on my dev machine, everything works fine. On the production machine, I get a "type initiatization" error. Looking into this, it seems it may because the app can't find the referenced DLLs.
我使用引用外部 DLL 的 C# 创建了一个控制台应用程序。当我在我的开发机器上运行它时,一切正常。在生产机器上,我收到“类型初始化”错误。对此,似乎可能是因为应用程序找不到引用的 DLL。
On my dev box, the referenced DLLs are in the GAC, but not on the production one. When removing the DLLs from the GAC on the dev box, the same error occurs (unless I run it from a local Visual Studio build in debug mode).
在我的开发框中,引用的 DLL 在 GAC 中,但不在生产环境中。从开发框中的 GAC 中删除 DLL 时,会发生相同的错误(除非我在调试模式下从本地 Visual Studio 构建运行它)。
I'm more familiar with web site setups, and know there that the DLLs can be placed in the bin directory or the GAC so they can be found by the web application. But I'm not sure about how this works for console apps.
我更熟悉网站设置,并且知道可以将 DLL 放在 bin 目录或 GAC 中,以便 Web 应用程序可以找到它们。但我不确定这对控制台应用程序是如何工作的。
I'm reluctant to put the DLL into the GAC on the production box, as it's only needed for this one small application. Are there other ways for me to deploy the console app and have it find its required assemblies?
我不愿意将 DLL 放入生产盒上的 GAC 中,因为只有这个小应用程序需要它。我还有其他方法可以部署控制台应用程序并让它找到所需的程序集吗?
Here is the exception I'm getting:
这是我得到的例外:
Error 1 The type or namespace name 'Entry' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Hacker\My Documents\Visual Studio 2005\Projects\basic\basic\Program.cs 10 8 basic
错误 1 找不到类型或命名空间名称“Entry”(您是否缺少 using 指令或程序集引用?)C:\Documents and Settings\Hacker\My Documents\Visual Studio 2005\Projects\basic\basic\Program .cs 10 8 基本
回答by JaredPar
The simplest answer is to put the DLL's in the same directory as the Console Application and it will find them.
最简单的答案是将 DLL 放在与控制台应用程序相同的目录中,它会找到它们。
The longer answer is fairly complex as there are many factors which influence how and where the CLR looks for referenced assemblies. I encourage you to take a look at the following article from MSDN which goes into great detail as to how this works.
较长的答案相当复杂,因为有许多因素会影响 CLR 查找引用程序集的方式和位置。我鼓励您查看 MSDN 中的以下文章,其中详细介绍了其工作原理。
回答by Michael Petrotta
When you examine the exception you get (and you may need to dive into the inner exception), you should see a log of all the locations searched (the "fusion log").
当您检查您得到的异常(并且您可能需要深入研究内部异常)时,您应该看到所有搜索位置的日志(“融合日志”)。
I'd recommend just placing the dependent DLLs in the same directory as your console app.
我建议只将依赖的 DLL 放在与控制台应用程序相同的目录中。
回答by Zack
The GAC (Global Assembly Cache) registers your .dll's so you don't have to have them in the working directory of your application. All of .NET's .dll's (System.IO.dll, System.dll, etc) are registered through the GAC which means you don't have to have them in your application's directory. By default, if a .dll is not registered in the GAC, then the program will look in it's own directory for the missing .dll.
GAC(全局程序集缓存)注册您的 .dll,因此您不必将它们放在应用程序的工作目录中。所有 .NET 的 .dll(System.IO.dll、System.dll 等)都是通过 GAC 注册的,这意味着您不必将它们放在应用程序的目录中。默认情况下,如果 .dll 未在 GAC 中注册,则程序将在其自己的目录中查找丢失的 .dll。
So, you have three choices:
所以,你有三个选择:
- Add your referenced .dll's to the GAC, or
- Add your referenced .dll's to the working directory of your application
- Go to your references on the solution/project and select properties for that reference, set "CopyLocal = true" (Credit to Partha above for this)
- 将您引用的 .dll 添加到 GAC,或
- 将引用的 .dll 添加到应用程序的工作目录中
- 转到您对解决方案/项目的引用并选择该引用的属性,设置“CopyLocal = true”(为此归功于上面的 Partha)
回答by Partha Choudhury
- Right click on the assembly name in your project reference. - select Properties - In the properties window set CopyLocal to true
回答by Marco
You can deploy referenced dlls where you want. You have to add an App.config (add/new item/application configuration file) to your base project and use the tag probing (configuration/runtime/assemblybinding/probing) to indicate a path for your dlls.
您可以在需要的地方部署引用的 dll。您必须向基础项目添加 App.config(添加/新项目/应用程序配置文件)并使用标记探测(配置/运行时/程序集绑定/探测)来指示 dll 的路径。
You have to copy the dll or dlls to that path and add a reference to it in your project. In reference properties put "copy local" to "false".
您必须将 dll 或 dll 复制到该路径并在您的项目中添加对它的引用。在参考属性中,将“copy local”设置为“false”。