locked
problem with AppDomain.CurrentDomain.GetAssemblies() in test mode

    Question

  • Hello,

    (1) I have an util.dll assembly (generated from utils project) that contains function 

    public static class StaticMethods
    {
        /// Find assembly in current application domain. Short name for the assuembly is used.
        public static Assembly FindAssemblyInCAD(string assemblyName)
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            //find the assembly with assemblyName in the assemblies...    
        }
    }

    (2) I have assembly A.dll (generated from A project, which references utils project) that uses the function above.

    (3) Third assembly B.dll generated separately that contains some classes I need to access.

    The Problem:

    I noticed that when the method to find assembly is used in a test project for A (automatically created by "Create Unit Test") one assembly is missing (exactly the one I need, B.dll) in the assemblies array. Thus for some reason AppDomain.CurrentDomain.GetAssemblies() behaves differently in original project and test project.

    I've checked following all assembly references are ok, all dll's sit in proper bin/debug directories:

    project A, bin/debug content:
    ...
    util.dll
    B.dll

    test project for A bin/debug content:
    ...
    util.dll
    B.dll

    Still B.dll cannot be found by AppDomain.CurrentDomain.GetAssemblies()  in test project.

    What could be wrong?
    ZoranK


    Friday, December 12, 2008 9:35 AM

Answers

  • Well, you can't without a direct reference. The CurrentDomain.GetAssemblies() only return loaded assemblies. It seems that you need to change strategy of loading the asseblies you need from using the appdomain to looking for dlls in your applications folder. A little less sexy, but it should be more robust :) 
    Ole - Andre Johansen | Hire Me !
    • Marked as answer by zorank Friday, December 12, 2008 4:23 PM
    Friday, December 12, 2008 2:07 PM

All replies

  • Could it be that the assembly has not yet been loaded? Asseblies are loaded lazy upon referene if I remember correctly. Try using something from the needed assembly before calling FindAssemblyInCAD, and see if it gets loaded.

    Hope this helps!

    Ole - Andre Johansen | Hire Me !
    Friday, December 12, 2008 12:13 PM
  • But how do I get something from the assembly I can not even find (dynamically)?
    Z.
    Friday, December 12, 2008 1:08 PM
  • Well, you can't without a direct reference. The CurrentDomain.GetAssemblies() only return loaded assemblies. It seems that you need to change strategy of loading the asseblies you need from using the appdomain to looking for dlls in your applications folder. A little less sexy, but it should be more robust :) 
    Ole - Andre Johansen | Hire Me !
    • Marked as answer by zorank Friday, December 12, 2008 4:23 PM
    Friday, December 12, 2008 2:07 PM
  • I am not sure what do you exactly mean by reference. Employing using statement? Referencing in VS? I do both of these things...

    Anyway, I will try what you say in a second...

    Z.

    p.s. I think I start understanding what you are after. GetAssemblies() just finds assemblies loaded in memory. CLR will not load assembly if not needed/used. I guess by "referenced" you mean "used".
    Friday, December 12, 2008 4:15 PM
  • Thanks tuxebar! That was exactly the problem. I added dummy statement that instantiated one class from the assembly and then it suddenly started to work...

    Many thanks again.

    Though I think this could be a bug of sorts. If one uses "Create Unit Test" I think that the system should be smart enough to prevent from such thing happening. Stricly speaking test environment should be no different from normal development environment anb user should be happily unaware of all these details...

    Z.
    Friday, December 12, 2008 4:23 PM
  • Glad it worked :)
    Ole - Andre Johansen | Hire Me !
    Friday, December 12, 2008 4:35 PM