在WPF中使用.NET Core 3.0依賴項注入和服務提供程式
阿新 • • 發佈:2019-10-29
前言
我們都知道.NET Core提供了對依賴項注入的內建支援。我們通常在ASP.NET Core中使用它(從Startup.cs檔案中的ConfigureServices方法開始),但是該功能不限於此框架,我們可以在WPF和Windows Forms應用程式中使用它。
實踐
新建專案
將所需的NuGet包新增到專案中。
- Microsoft.Extensions.DependencyInjection
- Microsoft.Extensions.Options.ConfigurationExtensions
- Microsoft.Extensions.Configuration.Json
然後,將一個名為appsettings.json的檔案新增到專案的根資料夾。將其“ 構建操作”屬性設定為“ 內容”,將“複製到輸出目錄”設定為“ 複製”(如果較新):
{ "AppSettings": { "AppName": "SampleNetCore3WpfDependencyInjection" } }
建立一個AppSettings.cs檔案來儲存配置設定。該檔案將對映我們在appsettings.json中編寫的設定:
public class AppSettings { public string AppName { get; set; } }
建立一個示例服務:
public interface ISampleService { Task<string> GetCurrentDate(); }
public class SampleService : ISampleService { public async Task<string> GetCurrentDate() => await Task.FromResult(DateTime.Now.ToLongDateString()); }
然後像往常一樣在IOC容器中註冊服務:
services.AddScoped<ISampleService, SampleService>();
開啟App.xaml檔案並刪除Application類的StartupUri屬性。然後,我們需要重寫App.xaml.cs中的OnStartup方法:
public partial class App : Application { public IServiceProvider ServiceProvider { get; private set; } public IConfiguration Configuration { get; private set; } protected override void OnStartup(StartupEventArgs e) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); Configuration = builder.Build(); var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); ServiceProvider = serviceCollection.BuildServiceProvider(); var mainWindow = ServiceProvider.GetRequiredService<MainWindow>(); mainWindow.Show(); } private void ConfigureServices(IServiceCollection services) { services.Configure<AppSettings>(Configuration.GetSection(nameof(AppSettings))); services.AddScoped<ISampleService, SampleService>(); services.AddTransient(typeof(MainWindow)); } }
MainWindow簡單佈局及程式碼改造
如上所述,MainWindow位於IOC容器中。因此,當我們從服務提供商處獲得服務時,它將自動注入所有必需的服務(如果有)。:
public partial class MainWindow : Window { private readonly ISampleService sampleService; private readonly AppSettings settings; public MainWindow(ISampleService sampleService, IOptions<AppSettings> settings) { InitializeComponent(); this.sampleService = sampleService; this.settings = settings.Value; } private async void Button_Click(object sender, RoutedEventArgs e) { var serviceData =await sampleService.GetCurrentDate(); var settingsData = settings; TextBox1.Text = $"serviceData:{serviceData}{Environment.NewLine}settingsData:{settings.AppName}"; } }
Demo地址:github