ARTICLE :
 

.NET Assembly Lookup Precedence/sequence

by SBajra on June 14th, 2011

Introduction

This article covers precedence or sequence of assembly lookup by .NET framework. This is very short article but I am hoping it will be helpful to all.

Background

We all know in .NET framework, library files (dll files) are called as assembly. Let me start little more on .NET assembly. .NET assembly is compiled to Intermediate Language (IL) code which gets compiled to machine code just-in-time by .NET framework. It contains metadata called Manifest which stores assembly name, version, public key, references etc. Since .NET assembly contains metadata to describe itself, we do not need to register .NET dll like we had to do with COM dlls. Unlike with traditional COM library which allow only one version of dll to be registered in the registry, .NET allows interaction with multiple version of same library/dll which kind of resolves dll hell issue. .NET dlls can be directly referenced by the application from file location or can be installed in global location called Global Assembly Cache (GAC). GAC is not really Windows registry but maintains multiple versions of same dll. It’s like a cache of dlls. GAC had changed little bit from .NET 2.0 to .NET 4.0 frameworks. GAC in .NET framework 4.0 not just handles multiple versions of same assemblies but also handles assemblies compiled by multiple versions of .NET framework. I think this is the biggest benefit of .NET 4.0 GAC.

GAC location in .NET 2.0 framework: %windir%\assembly\

GAC location in .NET 4.0 framework: %windir%\Microsoft.NET\assembly\

Detail

We had a brief background on what is assembly, GAC and assemblies can be loaded from either GAC (which supports multiple versions of same dll) or directly from file location. And there is also something called Probing with .NET framework. Probing in .NET is you can define your own path explicitly to tell .NET framework to look for assemblies. Now the question is how does .NET framework look for the assembly when application loads? Does it look first at GAC or file location or some other location? What’s the sequence, what’s the priority? I have been doing .NET development for long time but still sometime it confuses me.So, I thought of writing this article.

In .NET Windows based application, .NET framework first looks into the GAC then looks into the application location. Application location means the location where the application (.exe / .dll) resides. Then finally it looks into probing path defined. So, if you have assembly installed in GAC then .NET framework will never look into your application folder for the assembly.

So, the lookup sequence for .NET Windows application is:
GAC->Application location->Probing location

Probing path can be defined in App.Config for Windows application.
Probing path example:
<configuration>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<probing privatePath="dll"/>
		</assemblyBinding>
	</runtime>
</configuration>

In Web based application (ASP.NET), .NET framework first look into GAC then looks into "bin" subfolder under web root and then finally it looks into probing path defined. So, if you have dll installed in the GAC then the dll in the "bin" folder will never get used.

So, the lookup sequence for .NET Web application is:
GAC->"bin" subfolder ->Probing location

Probing path can be defined in Web.Config for Web application.
Probing path example:
<configuration>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<probing privatePath="dll"/>
		</assemblyBinding>
	</runtime>
</configuration>

Note: If your Web application is configured as "ASP.NET website", you cannot compile your website project without assemblies in "bin" subfolder or GAC. However, once you precompile your website, you can remove bin and it can reference from probing location. But if your Web application is configured as "ASP.NET Web Application", you should be able to compile from probing location without your assemblies in "bin" subfolder or GAC.

Conclusion

In summary, here is the assembly lookup sequence for .NET framework.
For .NET Windows application:
GAC->Application location->Probing location

For .NET Web application / website:
GAC->"bin" subfolder ->Probing location

Author:

Profile Photo
SBajra
Date: June 14th, 2011

Comments:



Profile Photo
Commented by Muhammad Arif
on March 7th, 2012 at 14:35:52 EST
I have a scenario, my .NET assembly uses a config file. When I deploy it in GAC in .NET Framework 4.0, from where it will use my config file?


Profile Photo
Commented by Muhammad Arif
on March 7th, 2012 at 14:48:52 EST
I have a scenario, my .NET assembly uses a config file. When I deploy it in GAC in .NET Framework 4.0, from where it will use my config file?

Profile Photo
Commented by SBajra
on April 4th, 2012 at 14:23:56 EST
unfortunately you can add config file or xml file to GAC. So, you would need to change your code to explicitly load config file.
You can also use ConfigurationManager.OpenMappedExeConfiguration() to load config file.