Search code examples
asp.netmixed-mode

Mixed mode assembly loading in asp.net


I have an asp.net webservice that references a mixed mode dll. The native dll's that the mixed mode assembly reference are in a directory that is added to SYSTEM PATH.

I want to remove the native dll's directory from the SYSTEM PATH, so I copied all the native dll's to webservice's bin directory, but I get file not found exception.

Does asp.net not look in webservice's bin directory for native dlls? Is there a way to dynamically set the SYSTEM PATH? How can I get this to work.


Solution

  • If anyone is facing a similar situation, this is what I did to be able to load a mixed mode assembly from a location not in these locations:

    1. The directory from which the application loaded. In the case of ASP.Net, this will resolve to %windir%\Microsoft.Net\Framework\v###\ or %windir%\system32\inetsrv for IIS 6.
    2. The current directory. In the case of ASP.Net, this will resolve to %windir%\System32\inetsrv for IIS 6. If using the built-in web server, this resolves to a path under C:\Program Files\Microsoft Visual Studio 8.
    3. The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
    4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
    5. The directories that are listed in the PATH environment variable.

    I set the native dlls on the mixed mode assembly to be delay loaded. Then I followed option 2b from http://blogs.msdn.com/b/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx

    I just had to make one minor tweak to the option 2b. It says to update "Application_Start" method in Global.asax, but I had to update the "Application_BeginRequest" method to get this to work with my webservice.