示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // #ContentNegociation
            services.AddMvc(config =>
            {
                // Add XML Content Negotiation
                config.RespectBrowserAcceptHeader = true;
                config.InputFormatters.Add(new XmlSerializerInputFormatter(null));
                config.OutputFormatters.Add(new XmlSerializerOutputFormatter());

                // #GlobalFilters
                config.Filters.Add(typeof(ModelValidationAttribute));
            }).AddFluentValidation(options =>
            {
                // # FluentValidaton
                options.RegisterValidatorsFromAssemblyContaining <Startup>();
            })
            .ConfigureLinks(); // Configure Hataeos but not working


            // #AutoMapper
            services.AddAutoMapper(typeof(Startup));

            // #ApiVersioning
            services.AddApiVersioning();

            // Register lazy cache
            services.AddLazyCache();

            // #Swagger
            SwaggerConfig.ConfigureService(services);

            // Add sorting, filtering, paging
            services.AddScoped <SieveProcessor>();

            services.AddControllers();

            services.AddScoped <ModelValidationAttribute>();

            // #injectingcontext
            services.AddDbContext <TodoContext>(options => options.UseMySql("server = localhost; user id = todoUser; password = todoUser; port = 3306; database = todos;"));

            // Application level services DI configuration
            services.ConfigureApplicationDependencies();
        }