コード例 #1
0
ファイル: Startup.cs プロジェクト: lordpiti/footballAPI
        // This method gets called by the runtime. Use this method to add services to the container.
        public virtual void ConfigureServices(IServiceCollection services)
        {
            services.AddResponseCompression();

            #region CORS setup

            services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
            {
                builder
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials()
                .AllowAnyOrigin()
                .WithOrigins(
                    "http://localhost:4200",
                    "http://localhost:3000",
                    "https://piti-react-test.herokuapp.com",
                    "https://footballpitiangular.herokuapp.com",
                    "https://top-squad.herokuapp.com");
            }));

            #endregion

            // Add framework services.

            services.AddControllers()
            .AddNewtonsoftJson(options =>
                               options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

            #region OData

            services.AddOData();

            //Added for compatibility with OData and Swagger
            services.AddMvcCore(options =>
            {
                foreach (var outputFormatter in options.OutputFormatters.OfType <ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
                foreach (var inputFormatter in options.InputFormatters.OfType <ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
            });

            #endregion

            services.AddSignalR();

            services.AddHttpClient();

            services.Configure <AppSettings>(options => Configuration.GetSection("AppSettings").Bind(options));

            ServiceConfiguration.ConfigureAPIServices(services, Configuration);

            //Since the filters will be used as a ServiceType (Because they use DI), the different custom filters need to be registered with the framework IoC.
            //If the action filters were used directly, this would not be required.
            services.AddScoped <AuthorizationRequiredAttribute>();

            //https://miniprofiler.com/dotnet/HowTo/ProfileEFCore
            services.AddMiniProfiler(options =>
                                     options.RouteBasePath = "/profiler"
                                     ).AddEntityFramework();

            // Inject an implementation of ISwaggerProvider with defaulted settings applied
            // https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.1
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Title       = "Football API specification",
                    Description = ""
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }