Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Enable CORS
            // Set it before of enable MVC
            services.AddCors();
            // Add framework services.
            services.AddMvc();
            // Setup options with DI
            services.AddOptions();
            // Setup Swagger
            services.AddSwaggerGen();
            SwaggerConfiguration.AddSwagger(services);
            // Configuration Properties
            services.Configure <MySqlDataSourcePropertyConfiguration>(Configuration.GetSection("DatabaseConfiguration"));

            // Create the container builder.
            var builder = new ContainerBuilder();

            RepositoryConfiguration.AddRepositories(builder);
            ServiceConfiguration.AddServices(builder);
            builder.Populate(services);
            this.ApplicationContainer = builder.Build();

            // Create the IServiceProvider based on the container.
            return(new AutofacServiceProvider(this.ApplicationContainer));
        }
Esempio n. 2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        /// </summary>
        /// <param name="services">collection that holds all used services</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.Configure <GatewayInformation>(Configuration.GetSection("ClientInfo"));
            services.Configure <AuthServerInfoOptions>(Configuration.GetSection("AuthServerInfo"));

            ServiceConfiguration.AddServices(services, Configuration);

            services.AddLogging();

            services.AddApiVersioning(o =>
            {
                o.AssumeDefaultVersionWhenUnspecified = true;
                o.DefaultApiVersion = new ApiVersion(new DateTime(2016, 12, 18), 1, 0);
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("admin", policyUser => { policyUser.RequireClaim("role", "admin"); });
                options.AddPolicy("recipe-write", policyAdmin => { policyAdmin.RequireClaim("role", "his.recipe.write"); });
                options.AddPolicy("recipe-read", policyAdmin => { policyAdmin.RequireClaim("role", "his.recipe.read"); });
                options.AddPolicy("ha-read", policyUser => { policyUser.RequireClaim("role", "his.ha.read"); });
                options.AddPolicy("ha-write", policyUser => { policyUser.RequireClaim("role", "his.ha.write"); });
            });

            #region Swagger

            // Inject an implementation of ISwaggerProvider with defaulted settings applied.
            services.AddSwaggerGen();

            services.ConfigureSwaggerGen(options =>
            {
                options.SingleApiVersion(new Info
                {
                    Version     = "v1",
                    Title       = "HIS API Gateway",
                    Description = "Api Gateways to interact with Apis of the Home Information System",
                });

                //Determine base path for the application.
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;

                //Set the comments path for the swagger json and ui.
                var xmlPath = Path.Combine(basePath, "HIS.Gateway.WebApi.xml");
                options.IncludeXmlComments(xmlPath);
                options.DescribeAllEnumsAsStrings();
            });

            #endregion

            // Add framework services.
            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(CheckForInvalidModelFilter));
                options.Filters.Add(typeof(DataObjectNotFoundExceptionFilter));
                options.Filters.Add(typeof(IdsNotIdenticalExceptionFilter));
            });
        }