嵌入式托管DLL不加载ASP.NET [英] Embedded Unmanaged DLLs don't load in ASP.NET
问题描述
我工作的ASP.NET主机WCF服务上。该服务引用了一个C ++ / CLI封装库,其本身引用了一个非托管的DLL。基于这个问题我已经嵌入在非托管的DLL ASP.NET的DLL。然后,我将其解压缩是这样的:
I'm working on an ASP.NET host for a WCF service. The service references a C++/CLI wrapper library, which itself references an unmanaged DLL. Based on this question I've embedded the unmanaged DLL in the ASP.NET DLL. I then extract it like this:
string[] dlls = new [] { "myDLL.dll", "myDLLD.dll" };
Assembly assembly = Assembly.GetExecutingAssembly();
string location = Path.GetDirectoryName(assembly.Location);
Dictionary<string, Stream> streams =
(from dll in dlls
select new KeyValuePair<string, Stream>(
dll, assembly.GetManifestResourceStream(typeof(Global), dll)))
.ToDictionary(p => p.Key, p => p.Value);
foreach (KeyValuePair<string, Stream> stream in streams)
{
using (FileStream file = new FileStream(Path.Combine(location, stream.Key),
FileMode.Create))
{
stream.Value.CopyTo(file);
}
}
我试过把这个code在的Application_Start()
在的Global.asax.cs
和在 AppInitialize()
在 APP_ code
文件夹,但在这两种情况下,我得到的死亡黄屏有关如何包装DLL或它的一个依赖无法加载的前的一个断点时在任一功能。我可以打一个断点的唯一方法是通过在系统路径某处放置非托管的DLL(如 C:\\ WINDOWS \\ SYSTEM
),但是这显然违背了嵌入的目的这些DLL摆在首位。我怎样才能得到它需要是ASP开始寻找之前的DLL?
I've tried putting this code in Application_Start()
in Global.asax.cs
and in AppInitialize()
in the App_Code
folder, but in both cases I get a yellow screen of death about how the wrapper DLL or one of its dependencies could not be loaded before a breakpoint is hit in either function. The only way I can hit a breakpoint is by placing the unmanaged DLL somewhere in the system path (e.g. C:\Windows\system
), but this obviously defeats the purpose of embedding the DLLs in the first place. How can I get the DLL where it needs to be before ASP starts looking?
推荐答案
显然ASP.NET的渴望加载机制是问题。由于托管包装被复制到输出目录,ASP发现了它,并试图链接到非托管的DLL在启动时,尽管它还不存在。为了解决这个问题,我用了 / DELAYLOAD
对C ++ / CLI DLL和的 调用LoadLibrary()
的P / Invoke在的Application_Start()
在与以上所示的嵌入的DLL萃取组合
Apparently ASP.NET's eager loading mechanism was the problem. Because the managed wrapper was copied to the output directory, ASP found it and tried to link to the unmanaged DLL on startup, even though it didn't exist yet. To solve the problem, I used the /DELAYLOAD
linker option on the C++/CLI DLL and a LoadLibrary()
P/Invoke in Application_Start()
in combination with the embedded DLL extraction shown above.
这篇关于嵌入式托管DLL不加载ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!