示例#1
0
        private void ConfigureContainer(HostBuilderContext context, ServiceRegistry serviceRegistry)
        {
            var assembly = Assembly.GetEntryAssembly();

            serviceRegistry
            .AddLamar(this.serviceRegistry)
            .AddApiVersioning(
                config =>
            {
                config.ReportApiVersions = true;
                config.AssumeDefaultVersionWhenUnspecified = true;
                config.DefaultApiVersion = new ApiVersion(1, 0);
                config.ApiVersionReader  = new HeaderApiVersionReader("api-version");
            })
            .AddVersionedApiExplorer(
                options =>
            {
                options.GroupNameFormat = "'v'VVV";

                // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
                // can also be used to control the format of the API version in route templates
                options.SubstituteApiVersionInUrl = true;
            })
            .AddTransient <IConfigureOptions <SwaggerGenOptions>, ConfigureSwaggerOptions>()
            .AddSwaggerGen()
            .AddControllers()
            .AddNewtonsoftJson()
            .AddApplicationPart(assembly)
            .AddControllersAsServices()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        }
示例#2
0
        public void ConfigureContainer(ServiceRegistry services)
        {
            const string variable = "OPEN_WEATHER_MAP_API_KEY";

            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(variable)))
            {
                Environment.SetEnvironmentVariable(variable, "fc28830c17565772df3697a3b91bcd47");
            }

            // It's a good practice to avoid registering the infrastructure layer with the upper layers.
            // This can be done using the method below.
            // https://ardalis.com/avoid-referencing-infrastructure-in-visual-studio-solutions
            //
            //const string name = "Way2.Infrastructure.DependencyResolution.dll";
            //var path = Path.Combine(AppContext.BaseDirectory, name);
            //var assembly = Assembly.LoadFrom(path);
            //services.Scan(_ =>
            //{
            //    _.Assembly(assembly);
            //    _.LookForRegistries();
            //});

            services.AddLamar <InfrastructureRegistry>();

            services.AddControllers();

            services.AddHealthChecks();

            services.AddApiVersioning();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "API v1", Version = "v1"
                });
                options.OperationFilter <RemoveVersionFromParameter>();
                options.DocumentFilter <ReplaceVersionWithExactValueInPath>();
            });

            services.AddHostedService <DatabaseMigrationHostedService>();
            services.AddHostedService <DatabaseSeedHostedService>();
            services.AddHostedService <WeatherFetcherBackgroundService>();
        }