using System.Reflection; using System.Text.RegularExpressions; using Autofac; using Autofac.Extensions.DependencyInjection; using ce.autofac.extension; using IdentityServer4.AccessTokenValidation; using Infrastructure; using Infrastructure.Extensions.AutofacManager; using Infrastructure.Middleware; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.FileProviders; using Microsoft.IdentityModel.Logging; using Microsoft.OpenApi.Models; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Npgsql; using OpenAuth.App; using OpenAuth.App.BaseApp.ImMsgManager; using OpenAuth.App.HostedService; using OpenAuth.Repository; using OpenAuth.WebApi.Model; using SqlSugar; using Swashbuckle.AspNetCore.SwaggerUI; namespace OpenAuth.WebApi { public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; } public Startup(ConfigurationManager configuration) { Configuration = configuration; } public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); #region log4net //在startup中需要强制创建log4net //var loggerFactory = LoggerFactory.Create(builder => { builder.AddLog4Net(); }); //ILogger logger = loggerFactory.CreateLogger(); services.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.AddLog4Net(); }); #endregion #region identityServer var identityServer = ((ConfigurationSection)Configuration.GetSection("AppSetting:IdentityServerUrl")).Value; if (!string.IsNullOrEmpty(identityServer)) { services.AddAuthorization(); services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = identityServer; options.RequireHttpsMetadata = false; // 指定是否为HTTPS options.Audience = "openauthapi"; }); } #endregion #region MiniProfiler // 添加MiniProfiler服务 //services.AddMiniProfiler(options => //{ // // 设定访问分析结果URL的路由基地址 // options.RouteBasePath = "/profiler"; // options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto; // options.PopupRenderPosition = StackExchange.Profiling.RenderPosition.BottomLeft; // options.PopupShowTimeWithChildren = true; // options.PopupShowTrivial = true; // options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter(); // // options.IgnoredPaths.Add("/swagger/"); //}).AddEntityFramework(); //显示SQL语句及耗时 #endregion #region swagger //添加swagger services.AddSwaggerGen(option => { option.CustomSchemaIds(type => type.FullName); option.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "基础架构 API", Description = "by hopetry" }); foreach (var name in Directory.GetFiles(AppContext.BaseDirectory, "*.*", SearchOption.AllDirectories).Where(f => Path.GetExtension(f).ToLower() == ".xml")) { option.IncludeXmlComments(name, includeControllerXmlComments: true); } option.OperationFilter(); // 添加httpHeader参数 }); #endregion #region AppSetting services.Configure(Configuration.GetSection("AppSetting")); #endregion #region 限制文件大小 services.Configure(options => { options.Limits.MaxRequestBodySize = long.MaxValue; }); services.Configure(x => { x.ValueLengthLimit = int.MaxValue; x.MultipartBodyLengthLimit = long.MaxValue; x.MemoryBufferThreshold = int.MaxValue; }); #endregion #region Controllers services.AddControllers(option => { option.Filters.Add(); //option.Filters.Add(); //option.Filters.Add(); }) .ConfigureApiBehaviorOptions(options => { // 禁用自动模态验证 // options.SuppressModelStateInvalidFilter = true; //启动WebAPI自动模态验证,处理返回值 options.InvalidModelStateResponseFactory = context => { var problems = new CustomBadRequest(context); return new BadRequestObjectResult(problems); }; }).AddNewtonsoftJson(options => { //忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //不使用驼峰样式的key //options.SerializerSettings.ContractResolver = new DefaultContractResolver(); options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); #endregion #region MemoryCache services.AddMemoryCache(); #endregion #region Cors services.AddCors(options => { //options.AddPolicy("CorsPolicy", // builder => builder.SetIsOriginAllowed(origin => true) // .AllowAnyMethod() // .AllowAnyHeader() // .AllowCredentials() // ); options.AddPolicy("SignalR", builder => builder.SetIsOriginAllowed(origin => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() ); //options.AddPolicy("CorsPolicy", // builder => builder.AllowAnyOrigin() // .AllowAnyMethod() // .AllowAnyHeader()); }); #endregion #region SqlSugar ////没有直接 using SqlSugar,因为如果引用命名空间的话,会和Microsoft的一个GetTypeInfo存在二义性,所以就直接这么使用了 services.AddScoped(s => { StaticConfig.CustomSnowFlakeFunc = () => { return Yitter.IdGenerator.YitIdHelper.NextId(); }; var sqlSugar = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.PostgreSQL, ConnectionString = Configuration.GetConnectionString("OpenAuthDBContext"), IsAutoCloseConnection = true, MoreSettings = new SqlSugar.ConnMoreSettings() { PgSqlIsAutoToLower = false, PgSqlIsAutoToLowerCodeFirst = false } }, db => { NpgsqlConnection.GlobalTypeMapper.UseNetTopologySuite(); //单例参数配置,所有上下文生效 db.Aop.OnLogExecuting = (sql, pars) => { //获取原生SQL推荐 5.1.4.63 性能OK //UtilMethods.GetNativeSql(sql, pars); //获取无参数化SQL 影响性能只适合调试 //Console.WriteLine(UtilMethods.GetSqlString(DbType.PostgreSQL, sql, pars)); //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); //LogHelper.LogInformation(sql + "\r\n" +db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); //Console.WriteLine(); }; }); return sqlSugar; }); services.AddScoped>(s => new SqlSugar.SugarUnitOfWork(s.GetService())); #endregion #region HttpClient services.AddHttpClient(); #endregion #region DataProtection services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Configuration["DataProtection"])); #endregion #region Quartz //设置定时启动的任务 //services.AddHostedService(); #endregion #region SignalR services.AddSignalR(); #endregion } public void ConfigureContainer(ContainerBuilder builder) { foreach (Assembly assembly in GetAssemblies()) { (from t in builder.RegisterAssemblyTypes(assembly) where IntrospectionExtensions.GetTypeInfo(typeof(BLL)).IsAssignableFrom(t) select t).InstancePerLifetimeScope(); } AutofacExt.InitAutofac(builder); } public void Configure(IApplicationBuilder app, IHostEnvironment env) { //loggerFactory.AddLog4Net(); IocManager.Instance.Container = app.ApplicationServices.GetAutofacRoot(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMiniProfiler(); //可以访问根目录下面的静态文件 var staticfile = new StaticFileOptions { FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory), OnPrepareResponse = (ctx) => { //可以在这里为静态文件添加其他http头信息,默认添加跨域信息 ctx.Context.Response.Headers["Access-Control-Allow-Origin"] = "*"; }, ContentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(new Dictionary { { ".amr","audio/AMR" }, { ".mp3","audio/mpeg" }, { ".mp4","video/mp4" }, { ".apk","application/vnd.android.package-archive" }, { ".cpg","application/vnd.android.package-archive" }, { ".dbf","application/vnd.android.package-archive" }, { ".shp","application/vnd.android.package-archive" }, { ".shx","application/vnd.android.package-archive" }, { ".zip","application/zip" }, { ".jpeg","image/jpeg" }, { ".jpg","image/jpg" }, { ".png","image/png" }, { ".docx","application/msword" }, { ".doc","application/msword" }, { ".txt","application/octet-stream" }, { ".xlsx","application/octet-stream" }, { ".xls","application/vnd.ms-excel" }, { ".pdf","application/pdf"} }) }; app.UseStaticFiles(staticfile); //todo:测试可以允许任意跨域,正式环境要加权限 app.UseRouting(); app.UseCors("SignalR"); app.UseAuthentication(); // 启用日志追踪记录和异常友好提示 app.UseLogMiddleware(); //配置ServiceProvider AutofacContainerModule.ConfigServiceProvider(app.ApplicationServices); //app.UseSwagger(); app.UseSwagger(options => { options.PreSerializeFilters.Add((swagger, httpReq) => { if (httpReq.Headers.ContainsKey("X-Request-Uri")) { var index = httpReq.Headers["X-Request-Uri"].ToString().IndexOf("/swagger/"); if (index > 0) { var serverUrl = $"{httpReq.Headers["X-Request-Uri"].ToString().Substring(0, index)}/"; swagger.Servers = new List { new OpenApiServer { Url = serverUrl } }; } } }); }); app.UseSwaggerUI(c => { c.SwaggerEndpoint("v1/swagger.json", "V1 Docs"); c.DocExpansion(DocExpansion.List); c.OAuthClientId("OpenAuth.WebApi"); //oauth客户端名称 c.OAuthAppName("开源版webapi认证"); // 描述 }); app.UseEndpoints(endpoints => { endpoints.MapHub("/chathub").RequireCors("SignalR"); // 配置 SignalR Hub 的路由 endpoints.MapControllers(); //endpoints.MapHub("/chathub"); // 配置 SignalR Hub 的路由 // endpoints.MapControllers().RequireCors("CorsPolicy"); }); } private static List _referenceAssembly; public static IEnumerable GetAssemblies() { if (_referenceAssembly == null) { _referenceAssembly = new List(); List list = new List(); string[] filterAssemblies = GetFilterAssemblies((from p in DependencyContext.Default.GetDefaultAssemblyNames() select p.Name).ToArray()); foreach (string assemblyString in filterAssemblies) { list.Add(Assembly.Load(assemblyString)); } _referenceAssembly.AddRange(list); } return _referenceAssembly; } private static string[] GetFilterAssemblies(string[] assemblyNames) { string pattern = "^Microsoft.\\w*|^System.\\w*|^DotNetty.\\w*|^runtime.\\w*|^ZooKeeperNetEx\\w*|^StackExchange.Redis\\w*|^Consul\\w*|^Newtonsoft.Json.\\w*|^Newtonsoft.Json\\w*|^Autofac.\\w*|^Autofac\\w*|^SOS.NETCore\\w*|^System\\w*|^WindowsBase\\w*|^mscorlib\\w*|^netstandard\\w*"; Regex notRelatedRegex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline); return assemblyNames.Where((string name) => !notRelatedRegex.IsMatch(name)).ToArray(); } } }