Michael H
Michael H

Reputation: 185

Why does renaming a dll extension (I.e. to dllold) cause a FileNotFound exception at runtime?

It appears at some point a dev working on one of our sites had to manually update a production dll, which we'll call "MyLibrary.dll". In doing so, they changed the old dll file extension to "dllold". Upon loading this site, we received a System.IO.FileNotFoundException error, that said Could not load file or assembly 'MyLibrary.dllold' or one of its dependencies. The system cannot find the file specified. We received this error even though both "MyLibrary.dll" and "MyLibrary.dllold" were in the correct directory. It was only after removing this "dllold" file, or completely removing the "dll" part of the extension (i.e. MyLibrary.old) that we were able to load the website.

So I'm wondering why exactly this happens? It almost seems like whatever part of the .NET framework pulls in the dlls at runtime only looks for "dll" and doesn't care about anything after, but that wouldn't explain why it always grabs the ".dllold" library and not the working ".dll" library.

For reference we're using .NET version 4.0 and ASP.NET version 4.6.

Upvotes: 3

Views: 258

Answers (2)

Thrifty2100
Thrifty2100

Reputation: 125

In addition, depending on what changes were made to the DLL, if both copies were in the bin folder you would end up loading two DLL's with the same namespaces. This could cause additional issues with the application not behaving correctly. Unlike other .NET applications, which load the assemblies ON DEMAND, ASP.NET loads all the DLL's at startup.

Upvotes: 1

DavidG
DavidG

Reputation: 118957

When an ASP.Net project loads, it will attempt to load all of the DLLs in the applications bin folder, it does this by calling an internal method called LoadAllAssembliesFromAppDomainBinDirectory. Internally to that method, the DLLs are retrieved using the DirectortInfo.GetFiles(string) method with a pattern of *.dll. This matches .dllold, as the documentation confirms:

When using the asterisk wildcard character in a searchPattern (for example, ".txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file.txt" returns both files.

Elsewhere in the assembly loading process, the file name is assumed to need .dll suffixing which is a file that doesn't exist and hence throws the exception you see.

So the moral of this story is: If you need to stop a DLL loading, remove it from your bin folder, or if you must leave it there, give it a suffix that won't get matched.

Upvotes: 4

Related Questions