private Type GetStartupType(Assembly assembly) { bool IsAConventionBasedStartupClass(Type t) { return(t.Name.StartsWith("Startup") && !t.GetTypeInfo().IsAbstract && MethodLoader.TryGetMethodInfo <IServiceCollection>(t, nameof(IStartup.ConfigureServices), out var @delegate)); } var startupTypes = assembly? .GetTypes() .Where(t => t.GetTypeInfo().BaseType == typeof(IStartup) || IsAConventionBasedStartupClass(t)) .ToList(); if (!startupTypes.Any()) { throw new InvalidOperationException("There is no Startup class"); } var startupType = startupTypes .Where(st => st.Name == $"Startup_{_environment.EnvironmentName}") .FirstOrDefault() ?? startupTypes .Where(st => st.Name == "Startup") .First(); return(startupType); }
private Action GetConfigureServicesdDelegateAsAction(object instance, IServiceCollection serviceCollection) { if (MethodLoader.TryGetMethodInfo <IServiceCollection>(instance.GetType(), $"Configure{_environment.EnvironmentName}Services", out var configureServicesMethod)) { return(MethodLoader.GetMethod(instance, $"Configure{_environment.EnvironmentName}Services", serviceCollection)); } else { return(MethodLoader.GetMethod(instance, nameof(IStartup.ConfigureServices), serviceCollection)); } }
private (IStartup startup, IServiceProvider provider) GetConventionalStartup(object instance, IServiceCollection serviceCollection) { var configureServicesMethod = GetConfigureServicesdDelegate(instance, serviceCollection); var startup = new ConventionBasedStartup(instance) { ConfigurationCallBack = configureServicesMethod }; startup.ConfigureServices(serviceCollection); var provider = serviceCollection.BuildServiceProvider(); var runMethod = MethodLoader.GetMethod(instance, nameof(IStartup.Run), _app, _environment); startup.RunCallback = runMethod; return(startup, provider); }