/// <summary> /// Registers modules found in an assembly. /// </summary> /// <param name="builder">The builder to register the modules with.</param> /// <param name="assemblies">The assemblies from which to register modules.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="builder"/> is <see langword="null"/>. /// </exception> /// <returns> /// The <see cref="IModuleRegistrar"/> to allow /// additional chained module registrations. /// </returns> public static IModuleRegistrar RegisterAssemblyModules(this ContainerBuilder builder, params Assembly[] assemblies) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var registrar = new ModuleRegistrar(builder); return(registrar.RegisterAssemblyModules <IModule>(assemblies)); }
/// <summary> /// Configures services for the application. /// </summary> /// <remarks> /// This method gets called by the runtime. Use this method to add services to the container. /// </remarks> /// <param name="services">The collection of services.</param> public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddOptions(); // JWT var jwtConfig = Configuration.ConfigAndGet <JwtConfig>(services, Constants.JwtSectionKey); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = jwtConfig.Issuer, ValidAudience = jwtConfig.Audience, IssuerSigningKey = new SymmetricSecurityKey(jwtConfig.KeyBytes) }; }); // Swagger services.AddSwaggerGen(c => { c.SwaggerDoc(Constants.ApiVersion, new Info { Title = Constants.ApiName, Version = Constants.ApiVersion, Description = Constants.ApiDescription, }); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); var registrar = new ModuleRegistrar().Register <UserModule>() .Register <SearchModule>() .Register <SearcherModule>(); registrar.CopyServicesTo(services); // AutoMapper var mapper = new MapperConfiguration(mc => registrar.MappingProfiles.ForEach(mc.AddProfile)).CreateMapper(); services.AddSingleton(mapper); // configuration services.AddSingleton(Configuration); }
/// <summary> /// Add a module to the container. /// </summary> /// <param name="builder">The builder to register the module with.</param> /// <typeparam name="TModule">The module to add.</typeparam> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="builder"/> is <see langword="null"/>. /// </exception> /// <returns> /// The <see cref="IModuleRegistrar"/> to allow /// additional chained module registrations. /// </returns> public static IModuleRegistrar RegisterModule <TModule>(this ContainerBuilder builder) where TModule : IModule, new() { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var registrar = new ModuleRegistrar(builder); return(registrar.RegisterModule <TModule>()); }
public void RegisterModule_ChainsModuleRegistrations() { var builder = new ContainerBuilder(); var registrar = new ModuleRegistrar(builder); registrar.RegisterModule(new ModuleA()).RegisterModule(new ModuleB()); var container = builder.Build(); var strings = container.Resolve <IEnumerable <string> >(); Assert.Contains("foo", strings); Assert.Contains("bar", strings); }
/// <summary> /// Registers modules found in an assembly. /// </summary> /// <param name="builder">The builder to register the modules with.</param> /// <param name="assemblies">The assemblies from which to register modules.</param> /// <typeparam name="TModule">The type of the module to add.</typeparam> /// <exception cref="System.ArgumentNullException"> /// Thrown if <paramref name="builder"/> is <see langword="null"/>. /// </exception> /// <returns> /// The <see cref="Autofac.Core.Registration.IModuleRegistrar"/> to allow /// additional chained module registrations. /// </returns> public static IModuleRegistrar RegisterAssemblyModules <TModule>(this ContainerBuilder builder, params Assembly[] assemblies) where TModule : IModule { if (builder == null) { throw new ArgumentNullException("builder"); } var registrar = new ModuleRegistrar(builder); return(registrar.RegisterAssemblyModules(typeof(TModule), assemblies)); }
/// <summary> /// Add a module to the container. /// </summary> /// <param name="builder">The builder to register the module with.</param> /// <param name="module">The module to add.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="builder"/> or <paramref name="module"/> is <see langword="null"/>. /// </exception> /// <returns> /// The <see cref="IModuleRegistrar"/> to allow /// additional chained module registrations. /// </returns> public static IModuleRegistrar RegisterModule(this ContainerBuilder builder, IModule module) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (module == null) { throw new ArgumentNullException(nameof(module)); } var registrar = new ModuleRegistrar(builder); return(registrar.RegisterModule(module)); }
/// <summary> /// Registers modules found in an assembly. /// </summary> /// <param name="builder">The builder to register the modules with.</param> /// <param name="moduleType">The <see cref="Type"/> of the module to add.</param> /// <param name="assemblies">The assemblies from which to register modules.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown if <paramref name="builder"/> or <paramref name="moduleType"/> is <see langword="null"/>. /// </exception> /// <returns> /// The <see cref="Autofac.Core.Registration.IModuleRegistrar"/> to allow /// additional chained module registrations. /// </returns> public static IModuleRegistrar RegisterAssemblyModules(this ContainerBuilder builder, Type moduleType, params Assembly[] assemblies) { if (builder == null) { throw new ArgumentNullException("builder"); } if (moduleType == null) { throw new ArgumentNullException("moduleType"); } var registrar = new ModuleRegistrar(builder); return(registrar.RegisterAssemblyModules(moduleType, assemblies)); }
public static void LoadContainer( ApplicationParameters parameters, IUnityContainer container, string path, string pattern) { var importDef = BuildImportDefinition(); using (var dirCat = new DirectoryCatalog(path, pattern)) using (var aggregateCatalog = new AggregateCatalog()) { aggregateCatalog.Catalogs.Add(dirCat); using (var componsitionContainer = new CompositionContainer(aggregateCatalog)) { var modules = componsitionContainer.GetExports(importDef).Select(e => e.Value).OfType<IModule>(); var registrar = new ModuleRegistrar(container); foreach (var module in modules) { module.Initialize(parameters, registrar); } } } }
private static void Main(string[] args) { // setup logging var loggingConfig = new LoggingConfiguration(); var consoleTarget = new ColoredConsoleTarget("consoleTarget") { Layout = @"[${longdate}] ${level} ${logger}: ${message} ${exception}" }; loggingConfig.AddRuleForAllLevels(consoleTarget); LogManager.Configuration = loggingConfig; loggingConfig.AddTarget(consoleTarget); // setup DI var serviceProvider = new ModuleRegistrar().Register <ConfigurationModule>() .Register <UploadModule>() .Build(); // run serviceProvider.GetService <IUploader>().Run(); }
public static void LoadContainer(IUnityContainer container, string path, string pattern) { var dirCat = new DirectoryCatalog(path, pattern); var importDef = BuildImportDefinition(); try { using (var aggregateCatalog = new AggregateCatalog()) { aggregateCatalog.Catalogs.Add(dirCat); using (var componsitionContainer = new CompositionContainer(aggregateCatalog)) { IEnumerable<Export> exports = componsitionContainer.GetExports(importDef); IEnumerable<IModule> modules = exports.Select(export => export.Value as IModule).Where(m => m != null); var registrar = new ModuleRegistrar(container); foreach (IModule module in modules) { module.Initialize(registrar); } } } } catch (ReflectionTypeLoadException typeLoadException) { var builder = new StringBuilder(); foreach (Exception loaderException in typeLoadException.LoaderExceptions) { builder.AppendFormat("{0}\n", loaderException.Message); } throw new TypeLoadException(builder.ToString(), typeLoadException); } }
private static async Task Main(string[] args) { if (args == null || args.Length == 0) { Console.WriteLine($"Usage: {nameof(SearcherClient)} \"query\" [\"Market1[,Market2[,...]]\" [size]] "); return; } // parse arguments var query = args[0]; var markets = args.Length > 1 ? args[1].ParseMarkets().ToImmutableList() : ImmutableList <string> .Empty; var size = args.Length > 2 ? ParseSize(args[2]) : DefaultSize; // setup logging SetupLogging(); // setup DI var serviceProvider = new ModuleRegistrar().Register <ConfigurationModule>() .Register <SearchModule>() .Build(); // run var results = await serviceProvider.GetService <ISearchService>().Search(query, markets, size); foreach (var item in results) { var sb = new StringBuilder($"{item.Id} ({item.ItemType}): {item.Name}"); if (!string.IsNullOrEmpty(item.FormerName)) { sb.Append($" ({item.FormerName})"); } sb.Append($" {item.Address}"); Console.WriteLine(sb.ToString()); } }
public void RegisterModule_RequiresModule() { var registrar = new ModuleRegistrar(new ContainerBuilder()); Assert.Throws <ArgumentNullException>(() => registrar.RegisterModule(null)); }
public static void RegisterModules() { ModuleRegistrar.RegisterAllCommands(new ErrorHandler()); }
protected virtual void OnConfigureContainer(ContainerBuilder builder) { var moduleRegistrar = new ModuleRegistrar(Logger, AssemblyProvider, Configuration); moduleRegistrar.Register(builder, modulePredicate); }