コード例 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        /// <summary>
        /// Configures the services.
        /// </summary>
        /// <param name="services">The services.</param>
        /// <returns></returns>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //var logger = Framework.Logging.Log4Net.LoggerFactory.Create(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            Logger.Info("Application startup - Initializing the Infra Services Configuration");
            try
            {
                //services.Configure<CookiePolicyOptions>(options =>
                //{
                //    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                //    options.CheckConsentNeeded = context => true;
                //    options.MinimumSameSitePolicy = SameSiteMode.None;
                //});

                var customSettings = this.Configuration.GetSection("CustomSettings").Get <CustomSettings>();
                Logger.Info($"Application startup - Allowed origins: {string.Join(",", customSettings.AllowedOrigins)}");

                services.AddCors();

                services.AddAutofac();

                //services.AddCors(options => {
                //    options.AddPolicy("Development", builder => builder
                //    .WithOrigins(customSettings.AllowedOrigins)
                //    .AllowAnyHeader()
                //    .AllowAnyMethod()
                //    .AllowCredentials());
                //});

                this.ConfigureAuthenticationServices(services);


                //services.AddElmah(options =>
                //{
                //    //services.AddElmah(options => option.Path = "you_path_here")
                //    //options.CheckPermissionAction = context => context.User.Identity.IsAuthenticated;
                //});

                services.AddSignalR(config =>
                {
                    config.EnableDetailedErrors = true;
                });

                services.AddDbContext <IdentityDBContext>(options => options.UseSqlServer(this.ConnectionString));
                services.AddMvc()
                //.AddControllersAsServices()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());

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

                SwaggerConfig.EnableServiceSwaggerMiddleware(services);

                services.AddSingleton <IAuthorizationHandler, PolicyPermissionRequiredHandler>();

                RefitConfig.Configure(this.Configuration, services);

                var autofacServiceProvider = IoCConfig.Init(Configuration, services);

                return(autofacServiceProvider);
            }
            catch (Exception ex)
            {
                Logger.Fatal("Application startup - Execption on Infra Services Configuration", ex);
                throw;
            }
            finally
            {
                Logger.Info("Application startup - Ending the Infra Services Configuration");
                Logger.FlushBuffers();
            }
        }