Fixing Unmanaged DLL Not Found Exception On ASP.Net Application

I often deal with DLL issues and I think it is good to document how I fix them as there would be many others facing similar issues. I assume that you have a fair knowledge of working of application.. :)

I received a StackTrace today which said System.DllNotFoundException: Unable to load DLL '< dll name >'. Here is how I tracked it down and fixed it:

So, the binary is available but IIS is unable to locate it. What could be wrong? Let’s figure it out.

The tool I use in this kind of situation is ProcMon from SysInternals. If ProcMon is new to you, go through this quick intro video by Scott Hanselman.

In continuation,

Now, we just know the cause of the issue and know a rough way to fix it. But why would IIS copy all the binaries in to a temp location instead of executing it from site root? Short answer is IIS will copy resources to a directory (called Shadow Copying )which will be used by AppDomain and will have have a lock on the files to save itself from config file modifications and the like. This blog post has a nice and detailed info on Shadow Copying.

Since we know that the binary is available on site root, we can either add site root path to PATH environment variable (binary in question along with all its dependencies are available under SiteRoot directory) or we can just disable Shadow Copying by adding following web.coinfg entry:

	<hostingEnvironment shadowCopyBinAssemblies="false"/>

We need to remember that AppDomain will now consider SiteRoot as it’s location and modifications to files under SiteRoot would cause application to crash and not a good idea on production environments. But since I always deploy a full CSPKG to production environment and never touch the files on production environment, I am good with this change.

Well, that’s it we have fixed it successfully. I have tried to go as detailed as possible, though if you did not understand something, or have confusions, feel free to comment below, I will try to address them.

comments powered by Disqus