locked
assembly load unload event RRS feed

  • Pergunta

  • I have an assembly for which I need to increment/decrement the counter in registry when assembly load/unload.

      are there any events which tells assembly is loading or unloading ??

    segunda-feira, 24 de julho de 2006 18:10

Respostas

  •  

    Sort of -

    You can attach to the AppDomain.CurrentDomain.AssemblyLoad event, but by the time you attach to it, the assembly that has that code has been loaded into an AppDomain.   This will work for you if you want to track assembly loads from code that is doing the loading.

    There is no corresponding Unload, since an Assembly can not be unloaded from an AppDomain - so the assembly is loaded until the AppDomain is destroyed - you can use AppDomain.CurrentDomain.DomainUnloaded to track when it is being unloaded cleanly.

     

    If you want to track an assembly being loaded/unloaded from within the assembly itself, you can sort-of use a type's static initializer (static constructor) -

    public class SomeType
    {
       static SomeType()
       {
         //some code here
        }
    }

    This code will be called the first time that type in your assembly is referenced   - not the same as when the assembly is loaded but you could conceviably make sure that using any type in your assembly tracks it. (you can use DomainUnloaded to track unload)

    You may want to revisit why you want to do this - AppDomain.DomainUnloaded event may never fire depending on how the appdomain is being destroyed - so you may never get reliable counts this way.  And using the static initializer will only tell you once the type has been referenced, not the assembly.

    segunda-feira, 24 de julho de 2006 18:24

Todas as Respostas

  •  

    Sort of -

    You can attach to the AppDomain.CurrentDomain.AssemblyLoad event, but by the time you attach to it, the assembly that has that code has been loaded into an AppDomain.   This will work for you if you want to track assembly loads from code that is doing the loading.

    There is no corresponding Unload, since an Assembly can not be unloaded from an AppDomain - so the assembly is loaded until the AppDomain is destroyed - you can use AppDomain.CurrentDomain.DomainUnloaded to track when it is being unloaded cleanly.

     

    If you want to track an assembly being loaded/unloaded from within the assembly itself, you can sort-of use a type's static initializer (static constructor) -

    public class SomeType
    {
       static SomeType()
       {
         //some code here
        }
    }

    This code will be called the first time that type in your assembly is referenced   - not the same as when the assembly is loaded but you could conceviably make sure that using any type in your assembly tracks it. (you can use DomainUnloaded to track unload)

    You may want to revisit why you want to do this - AppDomain.DomainUnloaded event may never fire depending on how the appdomain is being destroyed - so you may never get reliable counts this way.  And using the static initializer will only tell you once the type has been referenced, not the assembly.

    segunda-feira, 24 de julho de 2006 18:24
  • Try using the AppDomain AssemblyLoad and DomainUnload events.
    segunda-feira, 24 de julho de 2006 18:26
  • Hi,

    I recently faced a similar need: have a hook, where I could do some assembly level initialization, once. The concrete example: loading the app.config file of the dll, as I would like to avoid polluting the app.config of the executable loading the dll (and the sttings are not specific to the executable, should so not be its responsibility).

    The advice "use a static constructor" is a perfect one. The only problem is that when having 3 classes accessing the app.config, I need 3 static constructors (I do not knwo which one is going to be used first). I may have one single static constructor for the app.config accessor class. But I would like to reuse that feature across several dll-s, so I move it into another assembly --> cannot use reflection (type.Assembly.Location) for guessing the app.config to be loaded, as the approach above points to the assembly containing the app.config accessor, not the assembly needing some configuration entries. I could also use the stack trace to discover which assembly has just invoked the the assembly with the app.config accessor. Quite complicated, isn't it?

    I have the solution for the app.config problem, but it would be much simpler having a method, which I could implement for this simple initialization and the framework would just invoke it (like in UI development: Loading followed by Loaded event).

    If you know the solution (either for app.configs or for the Assembly.OnLoad) I would appreciate sharing it with me and the other cpnytributors of the chain above. Thanks in advance.

    Best regards,

    bigstefi

    sábado, 15 de setembro de 2018 16:40
  • Consider calling a Lazy-initialized variable in the first type you expected to be used in your assembly. Within the Lazy delegate, register for process exit event on the current appdomain. You will have 2 seconds on exit to run the counter update (which is plenty for ETW counters).

    Senior Soft. Dev. Eng. | Microsoft IT | Microsoft Corporation

    quinta-feira, 8 de outubro de 2020 05:55