0

I am currently working on a project which requires the use of a custom dll for authentication. This dll is registered (so they told me) in the GAC of both the Production and Test environments. The company policies state that you "shouldn't include the dll reference in your application's web.config file", that is, you shouldn't have something like :

<add assembly="" ....="" mydll=""> etc.

in your web.config, at least for this dll I am talking about. They say, since it's registered in the GAC, the application will load it anyway. The problem is , if you don't add the assemby reference in your web .config , the application is not going to find it , no matter what they say.

Since I am sure the dll is actually registered in the GAC, my question is :

shouldn't the application be able to load the dll anyway , no matter if you add the reference in the web.config or not ? I was convinced that, adding the reference to the project, the dll 's GAC location would be stored in the application's assembly so that the application itself would be able to find the assembly in the GAC , no matter what the working system is , given that the dll is actually registered in the working system's GAC . But I must be wrong. What is the real way it works ?

I tried deleting the <add assembly ...> entry from the web.config : I have a runtime error everytime I reference a method in the dll , in my test environment ; same thing happens in the Production environment;

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Booji Boy
  • 15
  • 4

1 Answers1

0

shouldn't the application be able to load the dll anyway , no matter if you add the reference in the web.config or not ?

By the logic applications would load every assembly in the GAC when they start up, could you imagine how slow that would be? No, that's not how it works.

When you reference a DLL locally in your project, all you are doing is adding it into the manifest which tells the application to load it as part of your application. When the CLR loads, it would first of all check if there is an equivalent version of your DLL in the GAC, if one exists it will load that version, if not it would attempt to load the local version.

See How the Runtime Locates Assemblies

James
  • 80,725
  • 18
  • 167
  • 237
  • I read the article at the link you sent , it's very interesting but there's something that baffles me . It says (Step 1: Examining the Configuration Files | Machine Configuration File) : "The settings in the machine configuration file take precedence over all other configuration settings;" Isn't just the other way around ? As far as I know , the web.config (that is , local to the application) settings should take precedence over the machine.config (that are inherited by every application on the local machine). Am I missing something important ? – Booji Boy Jul 23 '19 at 13:43
  • @BoojiBoy I don't think so, I'd say you are correct in your assumption. The Machine.config is effectively the "master" configuration and the web.config can be used to override those settings. However, this is referring to Assembly location resolution, not settings in general. – James Jul 23 '19 at 15:49
  • Actually the problem seems to be in the following configuration tag : if I remove it, a runtime error occurs . This tag seems to be related to compllation : when I Add the reference to the assembly in the project , isn't it enough for the compiler to realize that there's an assembly to bind statically, so that it can look for it using the standard procedure ? – Booji Boy Jul 24 '19 at 09:01
  • @BoojiBoy did you remove _only_ the local project from the `` section or _all_ the entries? `` usually contains references to GAC-related DLLs that your website would need in order to run, if you just remove them all then yes I would expect it to fail. There are plenty of material on how the `web.config` works and what each section does if you want to understand how it works. – James Jul 24 '19 at 09:54
  • Actually I just have one tag in my , and it's exactly the assembly that needs to be configured for avoiding the failure. I read on this link : https://stackoverflow.com/questions/16675171/what-does-the-web-config-compilation-assemblies-element-do that there could be a problem in the environment where it generally wouldn't need the assembly reference in that section when it's inside the application's manifest, but in some cases it's needed ..... – Booji Boy Jul 24 '19 at 11:10
  • @BoojiBoy I suggest you read [this answer](https://stackoverflow.com/a/33170048/82586) which goes into a bit more detail on how DLLs are loaded in an ASP.NET web app. It _appears_ to suggest that any DLLs in the `bin` folder of a web site _are_ included, not necessarily _loaded_ but can by loaded dynamically (presumably if the website is pre-compiled). – James Jul 24 '19 at 12:38