.Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
没有废话,直接上代码
public static class Mains { static IServiceCollection _services; static IMvcBuilder _mvc; public static WebApplicationBuilder Main ( this WebApplicationBuilder builder, IMvcBuilder mvc= null ) { _services = builder. Services; _mvc = mvc; _services. AddSingleton < IHttpContextAccessor, HttpContextAccessor> ( ) ; Web. _service = _services; AppSettings. ConfigItems = builder. Configuration; Cookies. serviceCollection = builder. Services; AddService ( ) ; _services. AddSingleton < Ixxx, xxx> ( ) ; _services. AddControllersWithViews ( x => { x. Filters. Add < ResAttribute> ( ) ; x. Filters. Add < LogAttribute> ( ) ; x. Filters. Add < TokenAttribute> ( ) ; } ) ; _services. AddControllers ( ) . AddJsonOptions ( options => { options. JsonSerializerOptions. Converters. Add ( new JsonOptionsDate ( "yyyy-MM-dd HH:mm:ss" ) ) ; options. JsonSerializerOptions. Converters. Add ( new JsonOptionsInt ( ) ) ; options. JsonSerializerOptions. Encoder = JavaScriptEncoder. Create ( UnicodeRanges. All) ; } ) ; _services. AddSwaggerGen ( c => { c. OperationFilter < OptionHeaderFilter> ( ) ; c. SwaggerDoc ( "v1" , new OpenApiInfo { Title = "service" , Version = "0.0.1" , Description = "文档描述" } ) ; var path = AppContext. BaseDirectory; var api = Path. Combine ( path, "api.xml" ) ; c. IncludeXmlComments ( api, true ) ; c. OrderActionsBy ( o => o. RelativePath) ; } ) ; _services. AddCors ( policy => { policy. AddPolicy ( "CorsPolicy" , opt => opt. AllowAnyOrigin ( ) . AllowAnyHeader ( ) . AllowAnyMethod ( ) . WithExposedHeaders ( "X-Pagination" ) ) ; } ) ; #region 请求大小设置 builder. WebHost. UseKestrel ( options => { options. Limits. MaxRequestLineSize = int . MaxValue; options. Limits. MaxRequestBufferSize = int . MaxValue; options. Limits. MaxRequestBodySize = int . MaxValue; } ) ; _services. Configure < FormOptions> ( x => { x. ValueCountLimit = 1000000 ; x. ValueLengthLimit = int . MaxValue; x. MultipartBodyLengthLimit = int . MaxValue; } ) ; #endregion return builder; } public static void AddService ( ) { var mods_dll = Directory. GetFiles ( "Mods" , "*.dll" ) ; foreach ( var mod in mods_dll) { Assembly. LoadFrom ( mod) ; } var allAssembly = GetAllAssembly ( ) . Where ( x => x. GetName ( ) . Name != "Microsoft.Data.SqlClient" ) . ToList ( ) ; var mods = allAssembly. Where ( x => x. Location. Contains ( "\\Mods" ) ) . ToList ( ) ; foreach ( var item in mods) { _mvc. AddApplicationPart ( item) ; } #region 注入服务 var _transient = allAssembly. SelectMany ( t => t. GetTypes ( ) ) . Where ( x => x. GetInterface ( "ITransient" ) != null ) . ToList ( ) ; _transient. AddTransient ( ) ; var _scoped = allAssembly. SelectMany ( t => t. GetTypes ( ) ) . Where ( x => x. GetInterface ( "IScoped" ) != null ) . ToList ( ) ; _scoped. AddScoped ( ) ; var _singleton = allAssembly. SelectMany ( t => t. GetTypes ( ) ) . Where ( x => x. GetInterface ( "ISingleton" ) != null ) . ToList ( ) ; _singleton. AddSingleton ( ) ; #endregion } #region 注入服务方法 public static void AddTransient ( this List< Type> list) { foreach ( var item in list) { _services. AddTransient ( item) ; } } public static void AddScoped ( this List< Type> list) { foreach ( var item in list) { _services. AddScoped ( item) ; } } public static void AddSingleton ( this List< Type> list) { foreach ( var item in list) { _services. AddSingleton ( item) ; } } #endregion #region 注入服务方法 public static void AddTransient ( this List< Type> list) { foreach ( var item in list) { _services. AddTransient ( item) ; } } public static void AddScoped ( this List< Type> list) { foreach ( var item in list) { _services. AddScoped ( item) ; } } public static void AddSingleton ( this List< Type> list) { foreach ( var item in list) { _services. AddSingleton ( item) ; } } #endregion private static List< Assembly> GetAllAssembly ( ) { var allAssemblies = AppDomain. CurrentDomain. GetAssemblies ( ) . ToList ( ) ; HashSet< string > loadedAssemblies = new ( ) ; foreach ( var item in allAssemblies) { loadedAssemblies. Add ( item. FullName! ) ; } Queue< Assembly> assembliesToCheck = new ( ) ; assembliesToCheck. Enqueue ( Assembly. GetEntryAssembly ( ) ! ) ; while ( assembliesToCheck. Any ( ) ) { var assemblyToCheck = assembliesToCheck. Dequeue ( ) ; foreach ( var reference in assemblyToCheck! . GetReferencedAssemblies ( ) ) { if ( ! loadedAssemblies. Contains ( reference. FullName) ) { try { var assembly = Assembly. Load ( reference) ; assembliesToCheck. Enqueue ( assembly) ; loadedAssemblies. Add ( reference. FullName) ; allAssemblies. Add ( assembly) ; } catch ( Exception ex) { Console. WriteLine ( ex. Message) ; } } } } return allAssemblies; } public static WebApplication AppMain ( this WebApplication app) { app. UseCors ( "CorsPolicy" ) ; app. UseStaticFiles ( ) ; return app; } }
调用