using System; using System.IO; using System.Net.Http; using System.Reflection; using IdentityModel.AspNetCore.OAuth2Introspection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using xApi.Extensions; using xCommons.Configurations; using xCommons.Extensions; // using xDataHelper; // using xDataHelper.DbSeeder; // using xDataHelper.Helpers; using xDataService.Configuration; using xDataService.Constants; using xDataService.DI; using xDataService.Interfaces; using xHttpService.DI; using xIdentityService.DI; // using xPushHelper.DI; using xPushService.DI; using xPushService.Helpers; using xServices.Databases; using xServices.DI; namespace xApi { public class Startup { // public IConfiguration Configuration { get; } private readonly XApiDatabaseDescriptor apiDatabaseDescriptor; public Startup(IConfiguration configuration) { // Configuration = configuration; apiDatabaseDescriptor = new XApiDatabaseDescriptor(); } public void ConfigureServices(IServiceCollection services) { // // Register Validation Provider and all XCommons Module Services ... services.AddXCommons(); // // Register App Configuration ... services.AddXAppConfiguration(Configuration); var appConfiguration = services.GetRegisteredService(); // // Register Allowed Origins ... services.AddXCors(appConfiguration.AllowedOrigins); // // Register Swagger ... var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); services.AddXSwagger(Configuration, xmlFilePath: xmlPath); // // Register HttpService ... services.AddXHttpService(Configuration); // // Register Api Versioning ... services.AddApiVersioning(opt => { // // Set Default Api Version ... opt.DefaultApiVersion = new ApiVersion(1, 0); // // Set Routing to Default API Version, if Version unspecified ... opt.AssumeDefaultVersionWhenUnspecified = true; // // Report All Available Api Versions on Response ... opt.ReportApiVersions = true; }); // var lifeTime = ServiceLifetime.Scoped; // // OAuthIntrospectin Http Client Handler ... // this is for handling SSLErrors ... services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName) .ConfigurePrimaryHttpMessageHandler(() => { return new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true }; }); // // Register Authorizations ... services.AddXAuthorization(); // services.AddControllers() .AddNewtonsoftJson(x => { x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; x.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); // // // // Check Data Service ... // var dataServiceConfig = Configuration.GetXDataServiceConfiguration(); // // Check DataBase Registration ... services.AddXDatabase( lifetime: lifeTime, configuration: Configuration, descriptor: apiDatabaseDescriptor ); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // var withPlayground = false; if (env.IsDevelopment()) { // withPlayground = true; app.UseDeveloperExceptionPage(); } // // Use Swagger Middleware ... app.UseXSwagger(); // app.UseHttpsRedirection(); // // Using Cors ... app.UseXCors(); // app.UseRouting(); // // Use xIdentityService ... app.UseXIdentityService(); // app.UseAuthorization(); // app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // #region XPushService ... // // // var helper = new XPushServiceHelper(); // // // app.UseXPushService(helper); #endregion // // Use xDataService Middleware ... app.UseXDatabase(apiDatabaseDescriptor); // // Using Services ... // app.UseXServices(); // // Use xGraphQL Middleware ... // app.UseXGraphQL(withPlayground: withPlayground); } } }