开发者

ASP.NET Load unmanaged dll from bin folder

开发者 https://www.devze.com 2022-12-31 08:48 出处:网络
Question: I use an embedded Firebird database in ASP.NET. Now, Firebird has a .NET wrapper around native dlls.

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 runti开发者_如何转开发me, 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, e.g. 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.....

  1. 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.
  2. 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).
  3. In the properties tab for both of the newly added vendor dll's, made them embedded resources.
  4. 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消