我正在为 WCF 服务开发 ASP.NET 主机。该服务引用 C++/CLI 包装器库,该库本身引用非托管 DLL。基于这个问题 https://stackoverflow.com/questions/2907169/asp-net-load-unmanaged-dll-from-bin-folder我已将非托管 DLL 嵌入到 ASP.NET DLL 中。然后我像这样提取它:
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);
}
}
我尝试过将此代码放入Application_Start()
in Global.asax.cs
and in AppInitialize()
in the App_Code
文件夹,但在这两种情况下,我都会出现黄屏死机,提示无法加载包装器 DLL 或其依赖项之一before在任一函数中都命中断点。我可以命中断点的唯一方法是将非托管 DLL 放置在系统路径中的某个位置(例如C:\Windows\system
),但这显然违背了嵌入 DLL 的初衷。在 ASP 开始查找之前,如何获取 DLL 所需的位置?
显然 ASP.NET 的急切加载机制是问题所在。由于托管包装器已复制到输出目录,因此 ASP 找到了它并在启动时尝试链接到非托管 DLL,即使它还不存在。为了解决这个问题,我使用了/DELAYLOAD http://msdn.microsoft.com/en-us/library/yx9zd12s.aspxC++/CLI DLL 上的链接器选项和LoadLibrary() http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspxP/调用Application_Start()
结合上面显示的嵌入式 DLL 提取。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)