Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 1.0, Enumerate All classes that implement base class

I am working on migrating an ASP.NET project to RC2. I am using AutoFac to try to enumerate classes that implement AutoMapper Profile base class to set up all my mapping profiles without having to call them explicitly. Previously in older version of ASP.NET (even in RC1) I was able to use the following code:

public class AutoMapperModule : Module
{

    protected override void Load(ContainerBuilder builder)
    {

        builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>();

        builder.Register(context =>
        {
            var profiles =
               AppDomain.CurrentDomain.GetAssemblies()
               .SelectMany(IoC.GetLoadableTypes)
               .Where(t => t != typeof(Profile) && t.Name != "NamedProfile" && typeof(Profile).IsAssignableFrom(t));

            var config = new MapperConfiguration(cfg =>
            {
                foreach (var profile in profiles)
                {
                    cfg.AddProfile((Profile)Activator.CreateInstance(profile));
                }
            });
            return config;
        })
        .AsSelf()
        .As<IConfigurationProvider>()
        .SingleInstance();

        builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
        builder.RegisterType<MappingEngine>().As<IMappingEngine>();

    }
}

This worked fantastically, until I tried converting my project to RC2 using the new netcoreapp1.0 framework, except now I am getting a design time error on AppDomain stating the "AppDomain does not exist in the current context". I've seen some suggestions about using ILibraryManager or DependencyContext to do this but I can't figure out how to get any of that to work. Any suggestions?

like image 233
Brandon Avatar asked Jun 03 '16 22:06

Brandon


1 Answers

.Net Core currently (1.0 RTM) does not support AppDomain.GetAssemblies() or a similar API. It's likely that it will support it in 1.1.

Until then, if you need this feature, you will need to stick with net452 (i.e. .Net Framework) instead of netcoreapp1.0.

like image 139
svick Avatar answered Nov 03 '22 07:11

svick