|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Diagnostics; |
| 4 | +using System.IdentityModel.Tokens.Jwt; |
4 | 5 | using System.IO; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Net; |
|
11 | 12 | using Microsoft.AspNetCore.Authentication.Cookies; |
12 | 13 | using Microsoft.AspNetCore.Authentication.JwtBearer; |
13 | 14 | using Microsoft.AspNetCore.Authentication.OAuth; |
14 | | -using Microsoft.AspNetCore.Identity; |
15 | 15 | using Microsoft.AspNetCore.Http; |
| 16 | +using Microsoft.AspNetCore.Identity; |
16 | 17 | using Microsoft.AspNetCore.Mvc.ApplicationParts; |
17 | | -using Microsoft.Extensions.Localization; |
| 18 | +using Microsoft.AspNetCore.Razor.TagHelpers; |
18 | 19 | using Microsoft.EntityFrameworkCore; |
19 | 20 | using Microsoft.Extensions.Configuration; |
20 | 21 | using Microsoft.Extensions.DependencyInjection; |
| 22 | +using Microsoft.Extensions.Localization; |
| 23 | +using Microsoft.OpenApi.Models; |
21 | 24 | using SimplCommerce.Infrastructure; |
| 25 | +using SimplCommerce.Infrastructure.Data; |
22 | 26 | using SimplCommerce.Infrastructure.Modules; |
| 27 | +using SimplCommerce.Infrastructure.Web; |
23 | 28 | using SimplCommerce.Infrastructure.Web.ModelBinders; |
24 | 29 | using SimplCommerce.Module.Core.Data; |
25 | 30 | using SimplCommerce.Module.Core.Extensions; |
26 | 31 | using SimplCommerce.Module.Core.Models; |
| 32 | +using SimplCommerce.Module.Localization.TagHelpers; |
27 | 33 | using SimplCommerce.WebHost.IdentityServer; |
28 | | -using System.IdentityModel.Tokens.Jwt; |
29 | 34 |
|
30 | 35 | namespace SimplCommerce.WebHost.Extensions |
31 | 36 | { |
@@ -226,6 +231,65 @@ public static IServiceCollection AddCustomizedDataStore(this IServiceCollection |
226 | 231 | return services; |
227 | 232 | } |
228 | 233 |
|
| 234 | + /// <summary> |
| 235 | + /// Registers core application services with the dependency injection container. |
| 236 | + /// </summary> |
| 237 | + /// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param> |
| 238 | + /// <param name="configuration">The application configuration manager containing settings and connection strings.</param> |
| 239 | + /// <returns>The same service collection so that multiple calls can be chained.</returns> |
| 240 | + public static IServiceCollection AddApplicationServices(this IServiceCollection services, ConfigurationManager configuration) |
| 241 | + { |
| 242 | + services.AddHttpClient(); |
| 243 | + services.AddTransient(typeof(IRepository<>), typeof(Repository<>)); |
| 244 | + services.AddTransient(typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>)); |
| 245 | + services.AddScoped<SlugRouteValueTransformer>(); |
| 246 | + services.AddScoped<ITagHelperComponent, LanguageDirectionTagHelperComponent>(); |
| 247 | + services.AddTransient<IRazorViewRenderer, RazorViewRenderer>(); |
| 248 | + services.AddAntiforgery(options => options.HeaderName = "X-XSRF-Token"); |
| 249 | + services.AddCloudscribePagination(); |
| 250 | + services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly)); |
| 251 | + |
| 252 | + return services; |
| 253 | + } |
| 254 | + |
| 255 | + /// <summary> |
| 256 | + /// Discovers and configures all modules in the application by finding and initializing their module initializers. |
| 257 | + /// </summary> |
| 258 | + /// <param name="services">The <see cref="IServiceCollection"/> to add module services to.</param> |
| 259 | + /// <returns>The same service collection so that multiple calls can be chained.</returns> |
| 260 | + public static IServiceCollection ConfigureModules(this IServiceCollection services) |
| 261 | + { |
| 262 | + foreach (var module in GlobalConfiguration.Modules) |
| 263 | + { |
| 264 | + var moduleInitializerType = module.Assembly.GetTypes() |
| 265 | + .FirstOrDefault(t => typeof(IModuleInitializer).IsAssignableFrom(t)); |
| 266 | + |
| 267 | + if (moduleInitializerType != null && moduleInitializerType != typeof(IModuleInitializer)) |
| 268 | + { |
| 269 | + var moduleInitializer = (IModuleInitializer)Activator.CreateInstance(moduleInitializerType); |
| 270 | + services.AddSingleton(typeof(IModuleInitializer), moduleInitializer); |
| 271 | + moduleInitializer.ConfigureServices(services); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + return services; |
| 276 | + } |
| 277 | + |
| 278 | + /// <summary> |
| 279 | + /// Adds and configures Swagger/OpenAPI documentation services to the service collection. |
| 280 | + /// </summary> |
| 281 | + /// <param name="services">The <see cref="IServiceCollection"/> to add Swagger services to.</param> |
| 282 | + /// <returns>The same service collection so that multiple calls can be chained.</returns> |
| 283 | + public static IServiceCollection AddSwagger(this IServiceCollection services) |
| 284 | + { |
| 285 | + services.AddSwaggerGen(c => |
| 286 | + { |
| 287 | + c.SwaggerDoc("v1", new OpenApiInfo { Title = "SimplCommerce API", Version = "v1" }); |
| 288 | + }); |
| 289 | + |
| 290 | + return services; |
| 291 | + } |
| 292 | + |
229 | 293 | private static void TryLoadModuleAssembly(string moduleFolderPath, ModuleInfo module) |
230 | 294 | { |
231 | 295 | const string binariesFolderName = "bin"; |
|
0 commit comments