none
When is an assembly removed from assembly cache? RRS feed

  • Question

  • Hi,

    How can I know if an assembly is used from the assembly cache or loaded from disk each time I use one class in the assembly?

    I ask this because I have a middleware app that has several assemblies and uses 70 "external" assemblies with bussines components, and I would like to know when/what assemblies are being loaded from assembly cache or from disk.

    Thanks in advance,

    DD

    Thursday, March 17, 2011 2:46 PM

Answers

  • Hello DevDriver,

     

    1. >> if my code was loaded form assembly cache or not and if the "components" I'm using are also loaded from assembly cache os from disk...

    1.1 Not sure if this will help you : you can use the Assembly.GlobalAssemblyCache property which indicates whether an assembly was loaded from the global assembly cache.

    1.2 I also suggest that you wirte an AppDomainManager. A brief outline of this is provided in the next section.

     

    2. Write an AppDomainManager.

    2.1 You can write an AppDomainManager and and connect with the AssemblyLoadEvent by providing an AssemblyLoadEventHandler.

    2.2 The AssemblyLoadEvent is very useful as it allows you to be notified whenever an assembly is loaded into an app domain of a managed application.

    2.3 Inside the AssemblyLoadEventHandler you can log down various details on the currently loading assembly (including querying whether it is loaded from the GAC).

    2.4 The following is a sample event handler :

            // The event handler for AssemblyLoad
            static void TestAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args)
            {
                string strMessage = string.Format("Assembly : {0:S} was loaded into AppDomain : {1:S}.", args.LoadedAssembly.FullName, AppDomain.CurrentDomain.FriendlyName);
                WriteToLog(strMessage);

                if (args.LoadedAssembly.GlobalAssemblyCache)
                {
                    strMessage = string.Format("Assembly was loaded from GAC.");
                    WriteToLog(strMessage);
                }

                strMessage = string.Format("Assembly.CodeBase       : {0:S}.", args.LoadedAssembly.CodeBase);
                WriteToLog(strMessage);

                strMessage = string.Format("Location of Assembly    : {0:S}.", args.LoadedAssembly.Location);
                WriteToLog(strMessage);

                strMessage = string.Format("Referenced assemblies : "); WriteToLog(strMessage);
                foreach (AssemblyName an in args.LoadedAssembly.GetReferencedAssemblies())
                {
                    strMessage = string.Format("Name={0}, Version={1}, Culture={2}, PublicKey token={3}", an.Name, an.Version, an.CultureInfo.Name, (BitConverter.ToString(an.GetPublicKeyToken())));
                    WriteToLog(strMessage);
                }
            }

    2.5 Using the above code, I am able to log the following sample output :

    Assembly : CSConstructors01, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null was loaded into AppDomain : DefaultDomain.
    Assembly was loaded NOT from GAC.
    Assembly.CodeBase       : file:///D:/Limbl/Develope/C#/C#Constructors/CSConstructors01/bin/Debug/CSConstructors01.exe.
    Location of Assembly    : D:\Limbl\Develope\C#\C#Constructors\CSConstructors01\bin\Debug\CSConstructors01.exe.
    Referenced assemblies :
    Name=mscorlib, Version=2.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89

    2.6 Let me know if the above is useful and if you need a full sample code for writing an AppDomainManager.

    2.7 Meantime, besides the MSDN documentation on AppDomainManager Class (http://msdn.microsoft.com/en-us/library/system.appdomainmanager.aspx), you may also want to read the following blog by shawnfa : "Setting up an AppDomainManager" (http://blogs.msdn.com/b/shawnfa/archive/2005/07/21/441588.aspx).

     

    - Bio.

    • Marked as answer by Paul Zhou Friday, March 25, 2011 3:31 AM
    Thursday, March 24, 2011 1:55 AM

All replies

  • There is a tool called Assembly binding log viewer, provided by MS that you can use.

    http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx

     

    hope it helps.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Friday, March 18, 2011 10:47 AM
  • that doesn´t suit me! I wanna log in my app what is appening, i.e: if my code was loaded form assembly cache or not and if the "components" I'm using are also loaded from assembly cache os from disk.

    If I cannot do this my assembly is there any counter I can use to know the overall assembly load density(from cache vs from disk)?

    Thanks,

    DD

    Monday, March 21, 2011 2:06 PM
  • Hi DD,

     

    Welcome to the MSDN forum!

    I know an Assembly Cache Viewer Tool to view and manipulate the contents of global assembly cache. I don’t know other ways to check the assembly cache.

     

    Much further information: Demystifying the .NET Global Assembly Cache.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 22, 2011 5:50 AM
  • Hello DevDriver,

     

    1. >> if my code was loaded form assembly cache or not and if the "components" I'm using are also loaded from assembly cache os from disk...

    1.1 Not sure if this will help you : you can use the Assembly.GlobalAssemblyCache property which indicates whether an assembly was loaded from the global assembly cache.

    1.2 I also suggest that you wirte an AppDomainManager. A brief outline of this is provided in the next section.

     

    2. Write an AppDomainManager.

    2.1 You can write an AppDomainManager and and connect with the AssemblyLoadEvent by providing an AssemblyLoadEventHandler.

    2.2 The AssemblyLoadEvent is very useful as it allows you to be notified whenever an assembly is loaded into an app domain of a managed application.

    2.3 Inside the AssemblyLoadEventHandler you can log down various details on the currently loading assembly (including querying whether it is loaded from the GAC).

    2.4 The following is a sample event handler :

            // The event handler for AssemblyLoad
            static void TestAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args)
            {
                string strMessage = string.Format("Assembly : {0:S} was loaded into AppDomain : {1:S}.", args.LoadedAssembly.FullName, AppDomain.CurrentDomain.FriendlyName);
                WriteToLog(strMessage);

                if (args.LoadedAssembly.GlobalAssemblyCache)
                {
                    strMessage = string.Format("Assembly was loaded from GAC.");
                    WriteToLog(strMessage);
                }

                strMessage = string.Format("Assembly.CodeBase       : {0:S}.", args.LoadedAssembly.CodeBase);
                WriteToLog(strMessage);

                strMessage = string.Format("Location of Assembly    : {0:S}.", args.LoadedAssembly.Location);
                WriteToLog(strMessage);

                strMessage = string.Format("Referenced assemblies : "); WriteToLog(strMessage);
                foreach (AssemblyName an in args.LoadedAssembly.GetReferencedAssemblies())
                {
                    strMessage = string.Format("Name={0}, Version={1}, Culture={2}, PublicKey token={3}", an.Name, an.Version, an.CultureInfo.Name, (BitConverter.ToString(an.GetPublicKeyToken())));
                    WriteToLog(strMessage);
                }
            }

    2.5 Using the above code, I am able to log the following sample output :

    Assembly : CSConstructors01, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null was loaded into AppDomain : DefaultDomain.
    Assembly was loaded NOT from GAC.
    Assembly.CodeBase       : file:///D:/Limbl/Develope/C#/C#Constructors/CSConstructors01/bin/Debug/CSConstructors01.exe.
    Location of Assembly    : D:\Limbl\Develope\C#\C#Constructors\CSConstructors01\bin\Debug\CSConstructors01.exe.
    Referenced assemblies :
    Name=mscorlib, Version=2.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89

    2.6 Let me know if the above is useful and if you need a full sample code for writing an AppDomainManager.

    2.7 Meantime, besides the MSDN documentation on AppDomainManager Class (http://msdn.microsoft.com/en-us/library/system.appdomainmanager.aspx), you may also want to read the following blog by shawnfa : "Setting up an AppDomainManager" (http://blogs.msdn.com/b/shawnfa/archive/2005/07/21/441588.aspx).

     

    - Bio.

    • Marked as answer by Paul Zhou Friday, March 25, 2011 3:31 AM
    Thursday, March 24, 2011 1:55 AM