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.....
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