public void ConfigureServices()

in dotnet/dotnet-guestbook/src/backend/Startup.cs [20:42]


        public void ConfigureServices(IServiceCollection services)
        {
            // GUESTBOOK_DB_ADDR environment variable is provided in guestbook-backend.deployment.yaml.
            var databaseAddr = Environment.GetEnvironmentVariable("GUESTBOOK_DB_ADDR");
            if (string.IsNullOrEmpty(databaseAddr))
            {
                throw new ArgumentException("GUESTBOOK_DB_ADDR environment variable is not set");
            }

            // PORT environment variable is provided in guestbook-backend.deployment.yaml.
            var port = Environment.GetEnvironmentVariable("PORT");
            if (string.IsNullOrEmpty(port))
            {
                throw new ArgumentException("PORT environment variable is not set");
            }

            services.AddControllers();

            // Pass the configuration for connecting to MongoDB to Dependency Injection container
            services.Configure<MongoConfig>(c => c.DatabaseAddress = $"mongodb://{databaseAddr}");

            services.AddScoped<IMessageRepository, MessageRepository>();
        }