16

A native DotNet application will load all referenced assemblies (and their references) on first use. However, an ASP.NET will load all referenced assemblies (and their references) on first access.

  1. Is this understanding correct?

  2. Is there a way to force ASP.NET to load assemblies on demand (like local applications)?

  3. The specific scenario I am trying to resolve is:

    • The bin folder contains 2 files : A.dll and B.dll.
    • A.dll references B.dll.
    • B.dll references C.dll which is somewhere else on the system. In this case, C.dll is missing.
    • A.dll is loaded (using reflection) by the main application.
    • The error encountered (Could not load file or assembly...) relates to a missing dependency of B.dll.
    • We want the application to function as normal if C.dll is missing as this is an optional component of the main application.
    • We have no control over the contents of B.dll or C.dll.
Iain
  • 10,433
  • 16
  • 56
  • 62

2 Answers2

27

To answer point 3, the setting that causes all the assemblies in the bin folder to be loaded on first access can be found in the file C:\winnt\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config (depending on your environment). Cut-down extract from that file:

<system.web>
    <compilation>
        <assemblies>
            <add assembly="*" />
        </assemblies>
    </compilation>
</system.web>

All assemblies that match the wildcard are loaded as part of the initial compilation.

By modifying the web.config for the application (NOT the global DotNet one) to include the web service assembly and exclude the wildcard match, it appears that the application can function if the optional dependencies are missing:

<system.web>
    <compilation>
        <assemblies>
            <remove assembly="*" />
            <add assembly="Main.Application.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=YOURKEYHERE" />
        </assemblies>
    </compilation>
</system.web>

We're still experimenting with this so not sure if this completely resolves the issue or has any unusual side-effects.

Iain
  • 10,433
  • 16
  • 56
  • 62
  • 2
    This is great, thanks for the post! I used this to exclude a .Net assembly that was early bound to an unmanaged dll, which was causing the dreaded could not find assembly or one of its dependencies exception. Then, to be able to find the unmanged dll when it was actually needed, used the LoadLibrary method found here: http://stackoverflow.com/questions/377181/32-or-64-bit-dll-loading-from-net-managed-code/652845#652845 – Greg Biles Oct 19 '11 at 14:09
  • This was a life saver. Thanks – Alon Catz Mar 10 '16 at 09:58
3

asp.net loads all the assemblies needed because the worker process creates an app domain with all of the required assemblies for every instance.

if you would like to load assemblies on demand try to use reflection in that way you can control wich and when to load your assemblies.

**edit: **

if you doesnt have control over B and C but you are saying that B needs C to run, and A has a hard reference to B. to me it sounds like you need ABC components to work, you can try to remove the B dependency from A by making it loosy couple.

you can use reflection to load B from A but is B still needs C its going to still cause issues.

how is your solution compiling with out the C component?

is C stored in the GAC?

Oscar Cabrero
  • 4,168
  • 8
  • 29
  • 49
  • Thanks for following up on my edits Oscar. C is available at compile time but cannot be shipped with the main application. You are right that ABC all work together and cannot function without each other. However, note that the main application will usually function without ABC. – Iain Feb 03 '09 at 18:01