示例#1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure <GitHubOption>(Configuration.GetSection("github"));
            RepositoryCloningStep.SetBasePath(Configuration.GetValue <string>("git:baseClonePath"));


            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddSeq(Configuration.GetSection("Seq"));
            });

            services.Configure <CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext <SophiaDbContext>(
                options => options.UseSqlServer(Configuration.GetConnectionString("SophiaConnection"),
                                                providerOptions => providerOptions.EnableRetryOnFailure()));

            services.AddHttpClient <GitHubRepositoryPullRequestService>()
            .AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(10, _ => TimeSpan.FromMilliseconds(600)))
            .AddPolicyHandler(GetRetryPolicy());

            services.AddHangfire(configuration => configuration
                                 .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                                 .UseSimpleAssemblyNameTypeSerializer()
                                 .UseRecommendedSerializerSettings()
                                 .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
            {
                CommandBatchMaxTimeout       = TimeSpan.FromMinutes(5),
                SlidingInvisibilityTimeout   = TimeSpan.FromMinutes(5),
                QueuePollInterval            = TimeSpan.Zero,
                UseRecommendedIsolationLevel = true,
                UsePageLocksOnDequeue        = true,
                DisableGlobalLocks           = true
            }));

            services.AddScoped <IssuesEventHandler>();
            services.AddScoped <IssueCommentEventHandler>();
            services.AddScoped <PullRequestEventHandler>();
            services.AddScoped <PushEventHandler>();
            services.AddScoped <GitHubWebHookHandler>();

            services.AddGitHubWebHookHandler(registry => registry
                                             .RegisterHandler <IssueCommentEventHandler>("issue_comment")
                                             .RegisterHandler <IssuesEventHandler>("issues")
                                             .RegisterHandler <PullRequestEventHandler>("pull_request")
                                             .RegisterHandler <PushEventHandler>("push"));

            CommandHandlerFactory.RegisterCommandHandlers();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            var containerBuilder = new ContainerBuilder();

            containerBuilder.Populate(services);
            containerBuilder.RegisterModule <DefaultModule>();
            containerBuilder.RegisterModule <KaftarBootstrapModule>();

            var container = containerBuilder.Build();

            GlobalConfiguration.Configuration.UseAutofacActivator(container, false);
            return(new AutofacServiceProvider(container));
        }