简体   繁体   English

无法加载.NET程序集

[英]Unable to load .NET assembly

I have the following situation: I have a windows service that loads many external .dlls as AddIns using the Microsoft AddIn Framework. 我有以下情况:我有一个Windows服务,该服务使用Microsoft AddIn Framework将许多外部.dll作为AddIns加载。 The AddIn .dlls are each in their own directory inside a 'AddIns' directory that is located in the directory containing the service exe. AddIn .dlls分别位于包含服务exe的目录中的'AddIns'目录中的其自己目录中。 Every AddIn .dll references a common assembly that is basically an API for the AddIns. 每个AddIn .dll都引用一个通用程序集,该程序集基本上是AddIns的API。 I put this in the service directory. 我把它放在服务目录中。 The problem I am having is that the API .dll must be in every AddIn directory also or I get the error: 我遇到的问题是API .dll也必须位于每个AddIn目录中,否则我将收到错误消息:

Exception has been thrown by the target of an invocation. 调用的目标已引发异常。 - System.IO.FileNotFoundException: Could not load file or assembly 'OSAE.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. -System.IO.FileNotFoundException:无法加载文件或程序集“ OSAE.API,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空”或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

I have also tried add this assembly to the GAC which seems to put it at C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL\\OSAE.API\\v4.0_1.0.0.0__f47a6446f36f79f7 , but this doesn't seem to help. 我也尝试过将此程序集添加到GAC,它似乎放在C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL\\OSAE.API\\v4.0_1.0.0.0__f47a6446f36f79f7 ,但这似乎无济于事。

How can I either tell the AddIns to look in a specific directory for the assembly or set it up where it is globally accessible somehow? 如何告诉外接程序在程序集的特定目录中查找或将其设置为可通过某种方式全局访问的位置?

Use the assembly binding log viewer (Fuslogvw.exe) to see what the runtime is trying to load and where it fails. 使用程序集绑定日志查看器 (Fuslogvw.exe)查看运行时尝试加载的内容以及失败的位置。

The Assembly Binding Log Viewer displays details for assembly binds. 程序集绑定日志查看器显示程序集绑定的详细信息。 This information helps you diagnose why the .NET Framework cannot locate an assembly at run time. 此信息可帮助您诊断.NET Framework为什么无法在运行时找到程序集。 These failures are usually the result of an assembly deployed to the wrong location, a native image that is no longer valid, or a mismatch in version numbers or cultures. 这些失败通常是由于程序集部署到错误的位置,不再有效的本机映像或版本号或区域性不匹配所导致的。 The common language runtime's failure to locate an assembly typically shows up as a TypeLoadException in your application. 公共语言运行库无法找到程序集通常在应用程序中显示为TypeLoadException。

(emphasis mine) (强调我的)

I solved it changing my project target platform from Any Cpu to x86 . 我解决了将项目目标平台Any Cpu更改为x86的问题 This option is located in Project > Properties > Build . 此选项位于“ 项目” >“ 属性” >“ 构建”中 For more information, refer to Microsoft's library BadImageFormatException 有关更多信息,请参见Microsoft的库BadImageFormatException

I think you problen is already solved at: C# can't find assembly which is already loaded 我认为您的问题已经在以下位置解决: C#找不到已经加载的程序集
Take a look at my answer i think it would also fit for your problem! 看看我的回答,我认为它也适合您的问题!

Why not load the API.dll manually before you load each add-in DLL. 为什么不在加载每个加载项DLL之前手动加载API.dll。 If the add-in DLLs are loading into their own app domain, then you would have to load API.dll into each app domain also. 如果加载项DLL正在加载到它们自己的应用程序域中,那么您还必须将API.dll加载到每个应用程序域中。 If they are loading into the same app domain, then you wouldl only need to load API.dll once. 如果它们正在加载到同一个应用程序域中,则只需要加载一次API.dll。

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

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