ASP.NET Load unmanaged dll from bin folder

Question: I use an embedded Firebird database in ASP.NET.

Now, Firebird has a .NET wrapper around native dlls.

The problem is, with the ASP.NET compilation and execution process, the dlls get shadow copied to a temporary folder. Unfortunately, only the .NET dlls, and not the native dll.

See http://msdn.microsoft.com/en-us/library/ms366723.aspx for details.

Now, this makes it necessary to put the unmanaged dll somewhere into the system32 directory (or any other directory in the path environment variable).

Now, I want to change the wrapper/native dll (opensource), so it loads the dlls also if they are only in the bin folder.

Now, my problem is, how can I, in .NET, load an unmanaged dll from an absolute path ?
The absolute path is determined at runtime, not at compile-time...


Embed the native dll in your assembly.

On Application_Start() , check Environment.CurrentDirectory or Assembly.GetExecutingAssembly().Location or whatever actually points to where you want to be, for the file and if not present, stream it out via Assembly.GetManifestResourceStream() .

Note, that this will likely cause an appdomain recycle, eg restarting your app, but since you are just starting it up, it is a non-issue.

Not sure why you want an absolute path, epecially for an unmanaged dll. You will get better mileage with less pain if you simply locate the unmanaged dll in the same directory as the assembly that is calling it.


I had a similar issue, but for whatever reason, the binding/loading would fail BEFORE Application_Start() was ever invoked. My scenerio was I had my web service which referenced my own other project (Called Common) which in turn referenced a vendor's managed C++ dll which in turn referenced a non managed C++ dll. The vendor's managed C++ dll was not set to "Delay Load" the vendor's non managed dll. This meant that went ASP.Net/Fusion tried to load the managed one, it would fail right away. Just having the vendor's managed dll in my bin folder cause .Net to try to load it and fail.

My solution was this.....

  • Create a POST build event that deleted the vendor's managed dll (the non managed wasn't copied to my bin, as expected). This allows my web service to start, but it would fail if I tried to use any code that needed the vendor's dll.
  • Right click on the web service and select Add->Existing item. Browse to the two vendor dll's and select "Add As Link" (click the down arrow right next to the Add button).
  • In the properties tab for both of the newly added vendor dll's, made them embedded resources.
  • Now, in Application_Start , I find the directory that my Common library is in by doing Path.GetDirectoryName(Assembly.GetAssembly(typeof(ATypeInMyCommonLibrary))); Using that as my output path I pull the embedded dll's out of Assembly.GetExecutingAssembly().GetManifestResourceStream and write that out via a FileStream .
  • Hope this helps!

    链接地址: http://www.djcxy.com/p/44532.html

    上一篇: BadImageFormatException从ASP.NET使用本机DLL时

    下一篇: ASP.NET从bin文件夹加载非托管dll