/// <summary> /// Build the app container with all services, used to build commands and start the app. /// </summary> /// <param name="args">The command line arguments.</param> /// <returns>A service provider.</returns> public static IServiceProvider BuildAppContainer(string[] args) { // Create container var services = new ServiceCollection(); // Build configuration var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); services.AddSingleton <IConfigurationRoot>(configuration); // Create and add logging var loggingService = new SerilogLoggingService(configuration); loggingService.SetLoggingLevel(args.Any(a => a == OptionsResources.VerboseOptionShort || a == OptionsResources.VerboseOptionLong) ? LogLevel.Debug : LogLevel.Information); services.AddSingleton <ILoggingService>(loggingService); services.AddLogging(builder => loggingService.BuildLogger(builder)); // Add command builders services.AddTransient <ICommandBuilder, AssessCommandBuilder>(); services.AddTransient <ICommandBuilder, MigrateCommandBuilder>(); services.AddTransient <ICommandBuilder, ConvertCommandBuilder>(); services.AddTransient <ICommandBuilder, VerifyCommandBuilder>(); services.AddTransient <RootCommandBuilder>(); // Build provider var provider = services.BuildServiceProvider(); return(provider); }
/// <summary> /// Configures the .NET generic host container with services required to handle command line commands. /// </summary> /// <param name="container">The container to add services to.</param> /// <param name="args">The command line arguments.</param> public static void ConfigureHostContainer(IServiceCollection container, string[] args) { // Build configuration var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); // Add unique ID generator container.AddTransient <IUniqueIdGenerator, UniqueIdGenerator>(); // Create and add logging var loggingService = new SerilogLoggingService(configuration); loggingService.SetLoggingLevel(args.Any(a => a == OptionsResources.VerboseOptionShort || a == OptionsResources.VerboseOptionLong) ? LogLevel.Debug : LogLevel.Information); container.AddSingleton <ILoggingService>(loggingService); container.AddLogging(builder => loggingService.BuildLogger(builder)); // Add options container.AddOptions <AppOptions>().BindCommandLine(); container.AddSingleton <IAppOptionsService, AppOptionsService>(); // Add region provider container.AddSingleton <IAzureRegionProvider, AzureRegionResourceProvider>(); // Add plugin finders container.AddTransient <IPluginFinder <IStageRunner>, PluginFinder <IStageRunner> >(); container.AddTransient <IPluginFinder <IApplicationModelProvider>, PluginFinder <IApplicationModelProvider> >(); // Add plugin host container.AddSingleton <IPluginHost <IRunnerComponent>, PluginHost <IRunnerComponent> >(); // Add stage component provider container.AddTransient <IStageComponentProvider, PluginStageComponentProvider>(); // Add model component provider container.AddTransient <PluginModelComponentProvider>(); // Add migration runner builder container.AddTransient <IRunnerBuilder, RunnerBuilder>(); // Add application service container.AddTransient <IApp, App>(); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services"></param> public void ConfigureServices(IServiceCollection services) { Services = services; //services.DisableAutoModelStateValidation(); //services.AddApiClient(); services.AddMvcCore().AddVersionedApiExplorer( options => { options.GroupNameFormat = "'v'VVV"; }); services.AddMvc(options => { options.OutputFormatters.Clear(); options.OutputFormatters.Add(new JsonOutputFormatter( new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc, Converters = new List <JsonConverter> { new StringEnumConverter() } }, ArrayPool <char> .Shared)); }) // Add Controllers .AddApplicationPart(Assembly.Load(new AssemblyName("LZ.Controllers.efdemo"))); services.AddApiVersioning(o => { o.ApiVersionReader = new HeaderApiVersionReader("x-lz-api-version"); o.DefaultApiVersion = new ApiVersion(1, 0); o.ReportApiVersions = true; o.AssumeDefaultVersionWhenUnspecified = true; }); // Initializes Swagger for the Documentation services.AddSwaggerGen(options => { var provider = services.BuildServiceProvider().GetRequiredService <IApiVersionDescriptionProvider>(); foreach (var description in provider.ApiVersionDescriptions) { options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description)); } options.OperationFilter <SwaggerDefaultValues>(); options.IncludeXmlComments(XmlCommentsFilePath); options.DescribeAllEnumsAsStrings(); options.DescribeStringEnumsInCamelCase(); }); services.AddEntityFrameworkSqlServer(); //services.AddDbContextPool<efdemoContext>((options) => //{ // bool.TryParse(Configuration[ConnectionstringKey], // out bool isEnabled); // if (isEnabled) // { // options.UseLoggerFactory(DbLoggerFactory); // } // options.UseSqlServer(Configuration[LogQueryKey]); //}); services.AddDistributedMemoryCache(); // Add Application Insights //services.AddApplicationInsightsTelemetry(Configuration); // add automapper services.AddSingleton(new efdemoMapper().Mapper); // Add Monitoring //services.AddSingleton<RequestTracker>(); // add instances - lifetime of the service services.AddSingleton(Configuration); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton(factory => LoggingService = new SerilogLoggingService(Configuration)); services.AddScoped <IAuthorizationHandler, CustomerAuthorizationHandler>(); //services.AddScoped<IApiClient, ApiClient>(); //TODO: add instances. services.AddScoped <IefdemoContext, efdemoContextSql>(); services.AddScoped <IefdemoRepository, efdemoRepository>(); services.AddScoped <IefdemoManager, efdemoManager>(); }