Embedded Unmanaged DLLs don't load in ASP.NET

杀马特。学长 韩版系。学妹 提交于 2019-12-24 02:47:11

问题


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);
    }
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/17332895/embedded-unmanaged-dlls-dont-load-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!