【问题标题】:Embedded Unmanaged DLLs don't load in ASP.NETASP.NET 中不加载嵌入式非托管 DLL
【发布时间】:2013-06-24 08:22:52
【问题描述】:

我正在为 WCF 服务开发 ASP.NET 主机。该服务引用一个 C++/CLI 包装库,该库本身引用一个非托管 DLL。基于this question,我在 ASP.NET DLL 中嵌入了非托管 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() 中的 Global.asax.csAppInitialize() 中的 App_Code 文件夹中,但在这两种情况下,我都会收到关于包装 DLL 或其一个在任一函数中遇到断点之前无法加载依赖项。我可以打断点的唯一方法是将非托管 DLL 放置在系统路径中的某个位置(例如C:\Windows\system),但这显然违背了首先嵌入 DLL 的目的。在 ASP 开始查找之前,如何获取 DLL 所需的位置?

【问题讨论】:

  • 也许引用非托管DLL的托管DLL也应该被嵌入。 ASP.NET 不执行按需链接:bin 中的任何内容都尝试“立即”加载和链接。

标签: asp.net wcf dll c++-cli unmanaged


【解决方案1】:

显然 ASP.NET 的急切加载机制是问题所在。因为托管包装器被复制到输出目录,ASP 找到它并在启动时尝试链接到非托管 DLL,即使它还不存在。为了解决这个问题,我在 C++/CLI DLL 上使用了/DELAYLOAD 链接器选项,并在Application_Start() 中使用了LoadLibrary() P/Invoke,并结合上面显示的嵌入式 DLL 提取。

【讨论】:

    猜你喜欢
    • 2010-10-14
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2014-03-08
    • 2017-10-16
    相关资源
    最近更新 更多