none
Get list of all assemblies in application in a Core 2.0 application RRS feed

  • Question

  • I'm working on a Core 2.0 back-end solution. I want to use the Core DI container. I have a class in each project that handles the registrations.  This is the code that I use to find all these bootstrap classes. 

                var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().FullName.StartsWith("MyCorp."));
                var bootstrapperTypes =
                    assemblies            
                    .SelectMany(x => x.GetTypes().Where(y => y.GetInterfaces().Contains(typeof(IBootstrapper))))
                    .ToList();

    However not all referenced assemblies are retruned by this code. I if no classes have been hit in the assebly, it is not returned.
    What do I need to do, to get a complete list in a reliable fashion?

    Tuesday, February 20, 2018 8:23 PM

All replies

  • .Net Cor can be discussed at ASP.NET forums that has a Core forum.

    http://forums.asp.net/

    Wednesday, February 21, 2018 3:45 AM
  • Consider Assembly.GetExecutingAssembly().GetReferencedAssemblies(). Also, try calling GetReferencedAssemblies for other assemblies returned by GetAssemblies. If you need information for assemblies that were not loaded yet, then consider Assembly.ReflectionOnlyLoad.

    Wednesday, February 21, 2018 6:01 AM
  • I'm not using ASP .net / core or otherwise
    Thursday, February 22, 2018 5:16 PM
  • I'm not using ASP .net / core or otherwise
    Well do you see a MSDN .NET Core forum anywhere? At least in the ASP.NET Core forum, you at least have developers working with Core that can possibly help you.
    Thursday, February 22, 2018 7:10 PM
  • Assembly.GetExecutingAssembly() did not do the trick. The statement is executed in the helper class that filters the assemblies. The helper is not in the top level assembly glueing the application together.
    Thursday, February 22, 2018 7:32 PM
  • Hi ksighem,

    For your question try to use Module.FindTypes.

      static Type[] test(Assembly ass)
            {
                return ass.GetModules()[0].FindTypes((type, obj) => type.Name.StartsWith("MyCorp"), null);
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, February 25, 2018 4:24 PM
    Moderator
  • public static class ConfigureServices
        {
            public static void ScanAndRegisterServices(this IServiceCollection services)
            {
                try
                {
                    //Load Assemblies
                    //Get All assemblies.
                    var refAssembyNames = Assembly.GetExecutingAssembly()
                        .GetReferencedAssemblies();
                    //Load referenced assemblies
                    foreach (var asslembyNames in refAssembyNames)
                    {
                        Assembly.Load(asslembyNames);
                    }
    
                    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    
                    foreach (var assembly in assemblies)
                    {
                        //Find assemblies with your ServiceType
                        var types = assembly.GetTypes()
                                    .Where(p => p.GetInterfaces().Contains(typeof(IMyServiceRegistrationType)));
    
                        foreach (var type in types)
                        {
                            var instance = Activator.CreateInstance(type) as IMyServiceRegistrationType;
                            if (instance != null)
                            {
                                instance.Register(services);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


    Friday, November 16, 2018 2:44 PM