You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.2 KiB
C#
99 lines
3.2 KiB
C#
using System.Configuration;
|
|
using System.Data;
|
|
using System.Reflection;
|
|
using System.Windows;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using NeoSmart.Caching.Sqlite;
|
|
using Sln.Wcs.Repository;
|
|
using Sln.Wcs.Serilog;
|
|
using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson;
|
|
using ZiggyCreatures.Caching.Fusion;
|
|
using Com.Ctrip.Framework.Apollo;
|
|
using SqlSugar;
|
|
|
|
namespace Sln_Wpf
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
public static IServiceProvider? ServiceProvider = null;
|
|
// 应用程序启动时调用
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
ConfigureServices(services);
|
|
|
|
ServiceProvider = services.BuildServiceProvider();
|
|
|
|
ServiceProvider.UseSerilogExtensions();
|
|
}
|
|
|
|
public void InitializeServices(IServiceProvider serviceProvider)
|
|
{
|
|
ServiceProvider = serviceProvider;
|
|
|
|
// 可以在这里进行其他依赖注入相关的初始化
|
|
var sqlSugarClient = ServiceProvider.GetService<ISqlSugarClient>();
|
|
// 其他初始化逻辑...
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
// 释放资源
|
|
base.OnExit(e);
|
|
}
|
|
|
|
private static void ConfigureServices(IServiceCollection services)
|
|
{
|
|
var basePath = AppContext.BaseDirectory;
|
|
|
|
ApolloConfigureServices(services, ref basePath, out IConfiguration apolloConfiguration);
|
|
|
|
services.AddSingleton(typeof(SerilogHelper));
|
|
|
|
services.AddSqlSugarSetup();
|
|
|
|
services.AddFusionCache()
|
|
.WithSerializer(
|
|
new FusionCacheNewtonsoftJsonSerializer()
|
|
)
|
|
.WithDistributedCache(new SqliteCache(new SqliteCacheOptions
|
|
{
|
|
CachePath = apolloConfiguration["cachePath"]
|
|
}));
|
|
}
|
|
|
|
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<IConfiguration>(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<IConfiguration>(apolloConfiguration);
|
|
}
|
|
|
|
}
|
|
|
|
}
|