using System.Reflection; using Com.Ctrip.Framework.Apollo; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using NeoSmart.Caching.Sqlite; using Newtonsoft.Json; using Sln.Wcs.HikRoBotSdk; using Sln.Wcs.Model.Configs; using Sln.Wcs.Repository; using Sln.Wcs.Repository.service; using Sln.Wcs.Serilog; using ZiggyCreatures.Caching.Fusion; using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson; namespace Sln.Wcs { public class Program { static async Task Main(string[] args) { var services = new ServiceCollection(); ConfigureServices(services); var serviceProvider = services.BuildServiceProvider(); serviceProvider.UseSerilogExtensions(); var config = serviceProvider.GetService(); var log = serviceProvider.GetService(); log.Info($"系统启动成功,日志存放位置:{config["logPath"]}"); } private static void ConfigureServices(IServiceCollection services) { var basePath = AppContext.BaseDirectory; ApolloConfigureServices(services,ref basePath,out IConfiguration apolloConfiguration); Assembly[] assemblies = { Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Common.dll")), Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Cache.dll")), Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Repository.dll")), Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotApi.dll")), Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotSdk.dll")), }; services.Scan(scan => scan.FromAssemblies(assemblies) .AddClasses() .AsImplementedInterfaces() .AsSelf() .WithTransientLifetime()); services.AddSingleton(typeof(SerilogHelper)); services.AddSqlSugarSetup(); services.AddFusionCache() .WithSerializer( new FusionCacheNewtonsoftJsonSerializer() ) .WithDistributedCache(new SqliteCache(new SqliteCacheOptions { CachePath = apolloConfiguration["cachePath"] })); } /// /// Apollo 配置中心 /// /// /// /// private static void ApolloConfigureServices(IServiceCollection services, ref string basePath, out IConfiguration apolloConfiguration) { var localConfiguration = new ConfigurationBuilder() .SetBasePath(basePath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var apolloConfigSection = localConfiguration.GetSection("apollo"); services.AddSingleton(localConfiguration); var configurationBuilder = new ConfigurationBuilder() .SetBasePath(basePath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddApollo(apolloConfigSection) .AddDefault(); apolloConfiguration = configurationBuilder.Build(); services.Remove(new ServiceDescriptor(typeof(IConfiguration), localConfiguration)); services.AddSingleton(apolloConfiguration); } } }