入口文件:App.xaml.cs
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Serilog; using System; using System.IO; using System.Text; using System.Windows; namespace Demo { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { /// <summary> /// 构造函数 /// </summary> public App() { // 创建服务容器 var services = new ServiceCollection(); // 添加服务注册 ConfigureServices(services); // 构建ServiceProvider对象 ServiceProviderHelper.InitServiceProvider(services.BuildServiceProvider()); // 注册编码格式 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); } /// <summary> /// 配置服务 /// </summary> /// <param name="services"></param> private static void ConfigureServices(IServiceCollection services) { // 添加配置文件 IConfigurationBuilder configurationBuilder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")}.json", optional: true, reloadOnChange: false); IConfiguration configuration = configurationBuilder.Build(); services.AddSingleton(configuration); // 添加Logger services.AddLogging(builder => { // 创建Serilog实例 var serilogLogger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .Enrich.FromLogContext() .CreateLogger(); // 添加Serilog builder.AddSerilog(logger: serilogLogger, dispose: true); }); // 添加数据库上下文 services.AddDbContext<SqliteContext>(options => options.UseSqlite(configuration["ConnectionStrings:SQLite"])); // 添加自定义接口和服务 services.AddAppServices(); } /// <summary> /// 程序启动 /// </summary> /// <param name="e"></param> protected override void OnStartup(StartupEventArgs e) { // 获取启动窗口 var window = ServiceProviderHelper.GetWindow<MainWindow>(); window.Show(); } /// <summary> /// 程序退出 /// </summary> /// <param name="e"></param> protected override void OnExit(ExitEventArgs e) { } } }View Code
全局服务帮助类:ServiceProviderHelper.cs
using Microsoft.Extensions.DependencyInjection; using System; using System.Windows; namespace Demo { /// <summary> /// 全局服务帮助类 /// </summary> public static class ServiceProviderHelper { /// <summary> /// 全局服务提供者 /// </summary> public static IServiceProvider ServiceProvider { get; private set; } = null!; /// <summary> /// 初始化构建ServiceProvider对象 /// </summary> /// <param name="serviceProvider"></param> /// <exception cref="ArgumentNullException"></exception> public static void InitServiceProvider(ServiceProvider serviceProvider) { ArgumentNullException.ThrowIfNull(serviceProvider, nameof(serviceProvider)); ServiceProvider = serviceProvider; } /// <summary> /// 获取Window窗口 /// </summary> /// <param name="type"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public static T GetWindow<T>() where T : Window { var service = ServiceProvider.GetRequiredService<T>(); if (service != null && service is Window) { return service; } else { throw new ArgumentException($"{typeof(T).FullName} is not a Window"); } } /// <summary> /// 获取服务 /// </summary> /// <param name="type"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public static T GetService<T>() where T : notnull { return ServiceProvider.GetRequiredService<T>(); } } }View Code
添加自定义接口和服务的类:AppServices.cs
using Microsoft.Extensions.DependencyInjection; namespace Demo { /// <summary> /// 添加自定义接口和服务的类 /// </summary> public static class AppServices { /// <summary> /// 添加自定义接口和服务 /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddAppServices(this IServiceCollection services) { // 接口 services.AddTransient<ISqliteService, SqliteService>(); // 类 services.AddScoped(typeof(Manage)); // 窗口 services.AddSingleton(typeof(MainWindow)); services.AddTransient(typeof(BaseWindow)); // 返回 return services; } } }View Code
翻译
搜索
复制
标签:C#,ServiceProvider,添加,static,using,services,NET6,WPF,public From: https://www.cnblogs.com/smartnn/p/17633848.html