Ejemplo n.º 1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure <IISServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });

            services.AddElmah <XmlFileErrorLog>(options =>
            {
                options.Path    = @"elmah";
                options.LogPath = Path.Combine(_hostEnvironment.ContentRootPath, "App_Data", "ElmahLogs");
                //options.CheckPermissionAction = context => context.User.Identity.IsAuthenticated; //note: looks like we have to use cookies for it
            });

            services.AddMvcCore(options =>
            {
                options.SuppressInputFormatterBuffering = true;

                options.EnableEndpointRouting = false;
                options.Conventions.Add(new ApiExplorerGroupPerVersionConvention());

                options.EnableDynamicDtoBinding();
                options.AddDynamicAppServices(services);
            })
            .AddApiExplorer()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            IdentityRegistrar.Register(services);
            AuthConfigurer.Configure(services, _appConfiguration);

            services.AddSignalR();

            services.AddCors();

            // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
            services.AddSwaggerGen(options =>
            {
                options.DescribeAllParametersInCamelCase();
                options.IgnoreObsoleteActions();
                options.AddXmlDocuments();

                options.OperationFilter <SwaggerOperationFilter>();

                options.CustomSchemaIds(type => SwaggerHelper.GetSchemaId(type));

                options.CustomOperationIds(desc => desc.ActionDescriptor is ControllerActionDescriptor d
                    ? d.ControllerName.ToCamelCase() + d.ActionName.ToPascalCase()
                    : null);
                options.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Title = "Shesha API", Version = "v1"
                });
                options.DocInclusionPredicate((docName, description) => true);

                options.AddDocumentsPerService();

                // Define the BearerAuth scheme that's in use
                options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });

                options.SchemaFilter <DynamicDtoSchemaFilter>();
            });

            //services.AddApiVersioning(options =>
            //{
            //    options.AssumeDefaultVersionWhenUnspecified = true;
            //    options.DefaultApiVersion = ApiVersion.Default;
            //    options.ReportApiVersions = true;
            //});

            //services.AddVersionedApiExplorer(options =>
            //{
            //    options.GroupNameFormat = "'v'VVV";
            //    options.SubstituteApiVersionInUrl = true;
            //});

            services.AddHttpContextAccessor();
            services.AddHangfire(config =>
            {
                config.UseSqlServerStorage(_appConfiguration.GetConnectionString("Default"));
            });

            // ToDo: fix AbpAuthorizationFilter
            services.AddMvc(options =>
            {
                options.Filters.AddService(typeof(SheshaAuthorizationFilter));
            });

            // Add ABP and initialize
            // Configure Abp and Dependency Injection
            return(services.AddAbp <SheshaWebHostModule>(
                       options =>
            {
                // Configure Log4Net logging
                options.IocManager.IocContainer.AddFacility <LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log4net.config"));
                // configure plugins
                //options.PlugInSources.AddFolder(Path.Combine(_hostingEnvironment.WebRootPath, "Plugins"), SearchOption.AllDirectories);
            }
                       ));
        }