using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Church.Net.DAL.EF; using Church.Net.DAL.EFCoreDBF; using Church.Net.Entity; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Serialization; using WebAPI.Hubs; using WebAPI.Logics; using WebAPI.Logics.Core; using WebAPI.Logics.Interface; using WebAPI.Services; using WebAPI.Services.AutoReplyCommands; using WebAPI.Services.Interfaces; using static System.Net.Mime.MediaTypeNames; namespace WebAPI { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(options => { // Use the default property (Pascal) casing options.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = new CamelCaseNamingStrategy(), }; options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified; options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; }); services.AddSignalR(); services.AddSingleton(); //services.AddSingleton(new ChurchNetContext()); services.AddDbContext(options => options.UseNpgsql( //Configuration.GetConnectionString() "Host=192.168.86.131;Port=49154;Database=Church;Username=chris;Password=1124" )); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(typeof(ICrudLogic<>), typeof(LogicBase<>)); services.AddScoped(typeof(ICrudDAL<>), typeof(CrudDALCBase<>)); services.AddScoped(typeof(ICombinedKeyCrudLogic<>), typeof(CombinedKeyLogicBase<>)); services.AddScoped(typeof(ICombinedKeyCrudDAL<>), typeof(CombinedKeyCrudDALCBase<>)); services.AddScoped(); services.AddTransient(); //services.AddMvc(o=>o.Filters.Add(typeof(HandleExceptionFilter))); //services.AddMvc(o => o.Filters.Add(new HandleExceptionFilter(services.BuildServiceProvider().GetService()))); //services.BuildServiceProvider().GetService(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ChurchNetContext dbContext) { //Reference:https://stackoverflow.com/questions/69961449/net6-and-datetime-problem-cannot-write-datetime-with-kind-utc-to-postgresql-ty AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath); AppDomain.CurrentDomain.SetData("WebRootPath", env.WebRootPath); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // global cors policy app.UseCors(x => x //.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .SetIsOriginAllowed(origin => true) // allow any origin .AllowCredentials() ); // allow credentials //app.UseCors(builder => //{ // builder.WithOrigins("http://localhost:4200") // .AllowAnyHeader() // .WithMethods("GET", "POST") // .AllowCredentials(); //}); //app.UseHttpsRedirection(); //app.UsePathBase(new PathString("/api")); app.UsePathBase(new PathString("/")); app.UseRouting(); app.UseEndpoints(endpoints => { //endpoints.MapControllerRoute( // name: "default", // pattern: "{controller}/{id?}"); endpoints.MapHub("/AvalonHub"); endpoints.MapHub("/WhoIsSpyHub"); endpoints.MapHub("/BaseHub"); endpoints.MapControllers(); //endpoints.MapControllerRoute( // name: "default", // pattern: "{controller}/{action=Index}/{id?}"); }); //app.UseExceptionHandler(exceptionHandlerApp => //{ // exceptionHandlerApp.Run(async context => // { // context.Response.StatusCode = StatusCodes.Status500InternalServerError; // // using static System.Net.Mime.MediaTypeNames; // context.Response.ContentType = Text.Plain; // await context.Response.WriteAsync("An exception was thrown."); // var exceptionHandlerPathFeature = // context.Features.Get(); // if (exceptionHandlerPathFeature?.Error is FileNotFoundException) // { // await context.Response.WriteAsync(" The file was not found."); // } // if (exceptionHandlerPathFeature?.Path == "/") // { // await context.Response.WriteAsync(" Page: Home."); // } // }); //}); //app.UseAuthorization(); //app.UseEndpoints(endpoints => //{ // endpoints.MapControllers(); // endpoints.MapHub("/WhoIsSpyHub"); //}); if (env.IsDevelopment()) { //dbContext.Database.EnsureCreated(); dbContext.Database.Migrate(); } } } }