mh_frame_sps/APT.API/Startup.cs

123 lines
3.8 KiB
C#
Raw Permalink Normal View History

2026-04-07 13:47:52 +08:00
using Autofac;
using APT.Infrastructure.Core;
using APT.Infrastructure.Core.Utils;
using APT.Infrastructure.EF;
using log4net;
using log4net.Config;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.IO;
using APT.Infrastructure.Api.WebFilters;
using Autofac.Extras.DynamicProxy;
using APT.Infrastructure.EF.Infrastructure;
using APT.Infrastructure.EF.Extensions;
using APT.Infrastructure.Api;
using ConfigurationManager = APT.Infrastructure.Api.ConfigurationManager;
namespace APT.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
var repository = LogManager.GetRepository
(System.Reflection.Assembly.GetEntryAssembly());
var fileInfo = new FileInfo("log4net.config");
if (!fileInfo.Exists)
fileInfo = new FileInfo(Path.Combine(System.AppContext.BaseDirectory, "log4net.config"));
XmlConfigurator.Configure(repository, fileInfo);
ConfigurationManager.Register(configuration);
InitUtils.Init();
}
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.AddScoped<TenantInfo>();
services.AddScoped<IConnectionResolver, HttpHeaderSqlConnectionResolver>();
services.AddScoped<IConnectionGenerator, CombindedConnectionGenerator>();
//services.AddPostgrePerConnection<EfDbContext>(settings =>
//{
// settings.ConnectionPrefix = "postgre_store";
//});
services.AddMvc(t=> {
t.EnableEndpointRouting = false;
});
// services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//UpdateDatabase();
}
void setupDb(TenantSettings<EfDbContext> settings)
{
//settings.ConnectionPrefix = "de";
//settings.DbContextSetup = (serviceProvider, connectionString, optionsBuilder) =>
//{
// var tenant = serviceProvider.GetService<TenantInfo>();
// optionsBuilder
// .UseLazyLoadingProxies(false)
// .UseNpgsql(connectionString, builder =>
// {
// // not necessary, if you are not using the table or schema
// builder.TenantBuilderSetup(serviceProvider, settings, tenant);
// });
//};
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseMiddleware<TenantInfoMiddleware>();
Infrastructure.Api.HttpContext.Register(app.ApplicationServices.
GetRequiredService<IHttpContextAccessor>()
);
app.Use(next => context => {
context.Request.EnableBuffering();
return next(context);
});
app.UseHttpsRedirection();
app.UseMvc();
}
private static void UpdateDatabase()
{
//using (var context = new MigrationContext())
//{
// context.Database.Migrate();
//}
}
public virtual void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterComponents();
//var builders = new ConfigurationBuilder()
//.AddJsonFile("appsettings.json");
//ConfigurationHelper.Configuration = builders.Build();
}
}
}