159 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			159 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 
								 | 
							
								using Autofac;
							 | 
						|||
| 
								 | 
							
								using APT.Infrastructure.Core;
							 | 
						|||
| 
								 | 
							
								using APT.Migrations;
							 | 
						|||
| 
								 | 
							
								using log4net;
							 | 
						|||
| 
								 | 
							
								using log4net.Config;
							 | 
						|||
| 
								 | 
							
								using Microsoft.AspNetCore.Authentication.JwtBearer;
							 | 
						|||
| 
								 | 
							
								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 Microsoft.IdentityModel.Tokens;
							 | 
						|||
| 
								 | 
							
								using Newtonsoft.Json;
							 | 
						|||
| 
								 | 
							
								using System;
							 | 
						|||
| 
								 | 
							
								using System.IO;
							 | 
						|||
| 
								 | 
							
								using System.Text;
							 | 
						|||
| 
								 | 
							
								using APT.BaseData.Domain.IServices;
							 | 
						|||
| 
								 | 
							
								using AutoMapper;
							 | 
						|||
| 
								 | 
							
								using APT.BS.WebApi.Models;
							 | 
						|||
| 
								 | 
							
								using System.Text.Encodings.Web;
							 | 
						|||
| 
								 | 
							
								using System.Text.Unicode;
							 | 
						|||
| 
								 | 
							
								using APT.Infrastructure.Core.Utils;
							 | 
						|||
| 
								 | 
							
								using APT.Infrastructure.EF.Infrastructure;
							 | 
						|||
| 
								 | 
							
								using APT.Infrastructure.EF;
							 | 
						|||
| 
								 | 
							
								using APT.Infrastructure.EF.Extensions;
							 | 
						|||
| 
								 | 
							
								using APT.Infrastructure.Api;
							 | 
						|||
| 
								 | 
							
								using ConfigurationManager = APT.Infrastructure.Api.ConfigurationManager;
							 | 
						|||
| 
								 | 
							
								using APT.BS.WebApi.App_Start;
							 | 
						|||
| 
								 | 
							
								using APT.WebApi.Models;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace APT.BS.WebApi
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    public partial 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.AddSqlServerPerConnection<EfDbContext>(settings =>
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                settings.ConnectionPrefix = "";
							 | 
						|||
| 
								 | 
							
								            });
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            ///wyw 添加监视无效?????
							 | 
						|||
| 
								 | 
							
								            //services.AddDbContext<EfDbContext>(options =>
							 | 
						|||
| 
								 | 
							
								            //{
							 | 
						|||
| 
								 | 
							
								            //    //options.UseSqlServer(Controllers.Api.BSSafeCheckController);
							 | 
						|||
| 
								 | 
							
								            //    options.UseSqlServer("Server=47.122.43.22;Database=mh_db;uid=sa;pwd=mhsafe!2021;");
							 | 
						|||
| 
								 | 
							
								            //    // 注册拦截器  
							 | 
						|||
| 
								 | 
							
								            //    options.AddInterceptors(new AddLogInterceptor());
							 | 
						|||
| 
								 | 
							
								            //});
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            services.AddCors(o => o.AddPolicy("AllowAllPolicy", builder =>
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                builder.AllowAnyOrigin()
							 | 
						|||
| 
								 | 
							
								                       .AllowAnyMethod()
							 | 
						|||
| 
								 | 
							
								                       .AllowAnyHeader();
							 | 
						|||
| 
								 | 
							
								            }));
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
							 | 
						|||
| 
								 | 
							
								            //automapper
							 | 
						|||
| 
								 | 
							
								            AutoMapper.IConfigurationProvider config = new MapperConfiguration(cfg =>
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                cfg.AddProfile<MappingProfile>();
							 | 
						|||
| 
								 | 
							
								            });
							 | 
						|||
| 
								 | 
							
								            services.AddSingleton(config);
							 | 
						|||
| 
								 | 
							
								            services.AddScoped<IMapper, Mapper>();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            services.AddResponseCompression();
							 | 
						|||
| 
								 | 
							
								            //
							 | 
						|||
| 
								 | 
							
								            services.AddMvc(option => option.EnableEndpointRouting = false)
							 | 
						|||
| 
								 | 
							
								     .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
							 | 
						|||
| 
								 | 
							
								            .AddNewtonsoftJson(options =>
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
							 | 
						|||
| 
								 | 
							
								                options.SerializerSettings.ContractResolver = new JsonContractResolver();
							 | 
						|||
| 
								 | 
							
								                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
							 | 
						|||
| 
								 | 
							
								                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
							 | 
						|||
| 
								 | 
							
								            });
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
							 | 
						|||
| 
								 | 
							
								            //UpdateDatabase();
							 | 
						|||
| 
								 | 
							
								            services.RegisterBearAuth();
							 | 
						|||
| 
								 | 
							
								            services.AddControllers(o => o.Filters.Add(typeof(APIAttributeActionFilter)));
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        // 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();
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								         
							 | 
						|||
| 
								 | 
							
								            Infrastructure.Api.HttpContext.Register(app.ApplicationServices.
							 | 
						|||
| 
								 | 
							
								                    GetRequiredService<IHttpContextAccessor>()
							 | 
						|||
| 
								 | 
							
								                );
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								       
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseCors("AllowAllPolicy");
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseAuthentication();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseHttpsRedirection();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseResponseCompression();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseMiddleware<TenantInfoMiddleware>();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseMvc();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            app.UseStaticFiles();
							 | 
						|||
| 
								 | 
							
								          
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            //fMOrganizationService.SetOrgCache();
							 | 
						|||
| 
								 | 
							
								            //APT.BD.WebApi.Utils.WebSokectHelper.Init();
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        public virtual void ConfigureContainer(ContainerBuilder builder)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            builder.RegisterComponents();
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								     
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |