|
| 1 | +using System.Threading.RateLimiting; |
| 2 | +using Dotnet.Samples.AspNetCore.WebApi.Configurations; |
| 3 | +using Dotnet.Samples.AspNetCore.WebApi.Data; |
| 4 | +using Dotnet.Samples.AspNetCore.WebApi.Mappings; |
| 5 | +using Dotnet.Samples.AspNetCore.WebApi.Repositories; |
| 6 | +using Dotnet.Samples.AspNetCore.WebApi.Services; |
| 7 | +using Dotnet.Samples.AspNetCore.WebApi.Utilities; |
| 8 | +using Dotnet.Samples.AspNetCore.WebApi.Validators; |
| 9 | +using FluentValidation; |
| 10 | +using Microsoft.EntityFrameworkCore; |
| 11 | +using Microsoft.OpenApi.Models; |
| 12 | +using Serilog; |
| 13 | + |
| 14 | +namespace Dotnet.Samples.AspNetCore.WebApi.Extensions; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Extension methods for WebApplicationBuilder to encapsulate service configuration. |
| 18 | +/// </summary> |
| 19 | +public static class ServiceCollectionExtensions |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Adds DbContextPool with SQLite configuration for PlayerDbContext. |
| 23 | + /// </summary> |
| 24 | + /// <param name="services">The IServiceCollection instance.</param> |
| 25 | + /// <param name="environment">The web host environment.</param> |
| 26 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 27 | + public static IServiceCollection AddDbContextPoolWithSqlite( |
| 28 | + this IServiceCollection services, |
| 29 | + IWebHostEnvironment environment |
| 30 | + ) |
| 31 | + { |
| 32 | + services.AddDbContextPool<PlayerDbContext>(options => |
| 33 | + { |
| 34 | + var dataSource = Path.Combine( |
| 35 | + AppContext.BaseDirectory, |
| 36 | + "storage", |
| 37 | + "players-sqlite3.db" |
| 38 | + ); |
| 39 | + options.UseSqlite($"Data Source={dataSource}"); |
| 40 | + |
| 41 | + if (environment.IsDevelopment()) |
| 42 | + { |
| 43 | + options.EnableSensitiveDataLogging(); |
| 44 | + options.LogTo(Log.Logger.Information, LogLevel.Information); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + return services; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Adds a default CORS policy that allows any origin, method, and header. |
| 53 | + /// <br /> |
| 54 | + /// <see href="https://learn.microsoft.com/en-us/aspnet/core/security/cors"/> |
| 55 | + /// </summary> |
| 56 | + /// <param name="services">The IServiceCollection instance.</param> |
| 57 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 58 | + public static IServiceCollection AddCorsDefaultPolicy(this IServiceCollection services) |
| 59 | + { |
| 60 | + services.AddCors(options => |
| 61 | + { |
| 62 | + options.AddDefaultPolicy(corsBuilder => |
| 63 | + { |
| 64 | + corsBuilder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + return services; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Adds rate limiting configuration with IP-based partitioning. |
| 73 | + /// <br /> |
| 74 | + /// <see href="https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit"/> |
| 75 | + /// </summary> |
| 76 | + /// <param name="services">The IServiceCollection instance.</param> |
| 77 | + /// <param name="configuration">The application configuration.</param> |
| 78 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 79 | + public static IServiceCollection AddRateLimiting( |
| 80 | + this IServiceCollection services, |
| 81 | + IConfiguration configuration |
| 82 | + ) |
| 83 | + { |
| 84 | + // Configure and register options |
| 85 | + services.Configure<RateLimitingOptions>(configuration.GetSection("RateLimiting")); |
| 86 | + |
| 87 | + var rateLimitingOptions = |
| 88 | + configuration.GetSection("RateLimiting").Get<RateLimitingOptions>() |
| 89 | + ?? new RateLimitingOptions(); |
| 90 | + |
| 91 | + services.AddRateLimiter(options => |
| 92 | + { |
| 93 | + options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>( |
| 94 | + httpContext => |
| 95 | + { |
| 96 | + var partitionKey = RateLimitingUtilities.CreatePartitionKey(httpContext); |
| 97 | + |
| 98 | + return RateLimitPartition.GetFixedWindowLimiter( |
| 99 | + partitionKey: partitionKey, |
| 100 | + factory: _ => new FixedWindowRateLimiterOptions |
| 101 | + { |
| 102 | + PermitLimit = rateLimitingOptions.PermitLimit, |
| 103 | + Window = TimeSpan.FromSeconds(rateLimitingOptions.WindowSeconds), |
| 104 | + QueueProcessingOrder = QueueProcessingOrder.OldestFirst, |
| 105 | + QueueLimit = rateLimitingOptions.QueueLimit |
| 106 | + } |
| 107 | + ); |
| 108 | + } |
| 109 | + ); |
| 110 | + |
| 111 | + options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; |
| 112 | + |
| 113 | + // Enhanced OnRejected handler with essential headers |
| 114 | + options.OnRejected = (context, _) => |
| 115 | + { |
| 116 | + var response = context.HttpContext.Response; |
| 117 | + |
| 118 | + var windowSeconds = rateLimitingOptions.WindowSeconds; |
| 119 | + |
| 120 | + // Essential headers for rate limiting |
| 121 | + response.Headers.RetryAfter = windowSeconds.ToString(); |
| 122 | + response.Headers.Append( |
| 123 | + "X-RateLimit-Limit", |
| 124 | + rateLimitingOptions.PermitLimit.ToString() |
| 125 | + ); |
| 126 | + response.Headers.Append("X-RateLimit-Remaining", "0"); |
| 127 | + response.Headers.Append("X-RateLimit-Window", windowSeconds.ToString()); |
| 128 | + |
| 129 | + return ValueTask.CompletedTask; |
| 130 | + }; |
| 131 | + }); |
| 132 | + |
| 133 | + return services; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Adds FluentValidation validators for Player models. |
| 138 | + /// <br /> |
| 139 | + /// <see href="https://docs.fluentvalidation.net/en/latest/aspnet.html"/> |
| 140 | + /// </summary> |
| 141 | + /// <param name="services">The IServiceCollection instance.</param> |
| 142 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 143 | + public static IServiceCollection AddValidators(this IServiceCollection services) |
| 144 | + { |
| 145 | + services.AddValidatorsFromAssemblyContaining<PlayerRequestModelValidator>(); |
| 146 | + return services; |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Sets up Swagger documentation generation and UI for the API. |
| 151 | + /// <br /> |
| 152 | + /// <see href="https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle" /> |
| 153 | + /// </summary> |
| 154 | + /// <param name="services">The IServiceCollection instance.</param> |
| 155 | + /// <param name="configuration">The application configuration.</param> |
| 156 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 157 | + public static IServiceCollection AddSwaggerConfiguration( |
| 158 | + this IServiceCollection services, |
| 159 | + IConfiguration configuration |
| 160 | + ) |
| 161 | + { |
| 162 | + services.AddSwaggerGen(options => |
| 163 | + { |
| 164 | + options.SwaggerDoc("v1", configuration.GetSection("OpenApiInfo").Get<OpenApiInfo>()); |
| 165 | + options.IncludeXmlComments(SwaggerUtilities.ConfigureXmlCommentsFilePath()); |
| 166 | + options.AddSecurityDefinition("Bearer", SwaggerUtilities.ConfigureSecurityDefinition()); |
| 167 | + options.OperationFilter<AuthorizeCheckOperationFilter>(); |
| 168 | + }); |
| 169 | + |
| 170 | + return services; |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Registers the PlayerService with the DI container. |
| 175 | + /// <br /> |
| 176 | + /// <see href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection"/> |
| 177 | + /// </summary> |
| 178 | + /// <param name="services">The IServiceCollection instance.</param> |
| 179 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 180 | + public static IServiceCollection RegisterPlayerService(this IServiceCollection services) |
| 181 | + { |
| 182 | + services.AddScoped<IPlayerService, PlayerService>(); |
| 183 | + return services; |
| 184 | + } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Adds AutoMapper configuration for Player mappings. |
| 188 | + /// <br /> |
| 189 | + /// <see href="https://docs.automapper.io/en/latest/Dependency-injection.html#asp-net-core"/> |
| 190 | + /// </summary> |
| 191 | + /// <param name="services">The IServiceCollection instance.</param> |
| 192 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 193 | + public static IServiceCollection AddMappings(this IServiceCollection services) |
| 194 | + { |
| 195 | + services.AddAutoMapper(typeof(PlayerMappingProfile)); |
| 196 | + return services; |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Registers the PlayerRepository service with the DI container. |
| 201 | + /// <br /> |
| 202 | + /// <see href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection"/> |
| 203 | + /// </summary> |
| 204 | + /// <param name="services">The IServiceCollection instance.</param> |
| 205 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 206 | + public static IServiceCollection RegisterPlayerRepository(this IServiceCollection services) |
| 207 | + { |
| 208 | + services.AddScoped<IPlayerRepository, PlayerRepository>(); |
| 209 | + return services; |
| 210 | + } |
| 211 | +} |
0 commit comments