using log4net; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; namespace NEIntelligentControl2 { /// /// 新能源智能发电集中控制系统 /// public partial class App : Application { /// /// 依赖注入 /// public static IServiceProvider ServiceProvider { get; set; } private ILog log = LogManager.GetLogger("File"); private void Application_Startup(object sender, StartupEventArgs e) { var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); ServiceProvider = serviceCollection.BuildServiceProvider(); var mainWindow = ServiceProvider.GetRequiredService(); mainWindow.Show(); } private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { try { e.Handled = true; log.Error("应用程序出现重大错误,但可继续运行!", e.Exception); } catch (Exception ex) { log.Fatal("应用程序出现致命错误,已经结束运行!", e.Exception); } } /// /// 获取服务 /// /// 类型 /// 服务 public static T GetService() { return (T)ServiceProvider.GetService(typeof(T)); } /// /// 依赖注入 /// private void ConfigureServices(IServiceCollection services) { services.AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddTransient() .AddTransient() .AddTransient() .AddTransient< Pages.User.PageLogin>() .AddTransient< Pages.User.PageUserEdit>() .AddSingleton() .AddTransient() .AddSingleton() .AddSingleton() .AddSingleton() .AddTransient() .AddTransient() .AddSingleton() .AddSingleton() .AddSingleton() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient() .AddTransient(); } } }