在MVVM桌面应用中使用Asp.net Core的Generic Host模型
分类: .Net技术 ◆ 标签: #异步编程 #基础 #.Net #WPF ◆ 发布于: 2023-08-07 22:04:07

熟悉Asp.net Core
的同行应该都非常了解它的Host
模型,特别是Generic Host
,Host
将一系列的功能全部压缩的这个模型里,这包括依赖注入,配置管理,日志管理,生命周期管理等等。如果你想对Host
多了解一下,您可以参考如下的文档:
- https://www.azuredeveloper.cn/article/asp-net-core-host
- https://www.azuredeveloper.cn/article/what-is-dotnet-host
- https://www.azuredeveloper.cn/article/show-example-for-dotnet-host
- https://www.azuredeveloper.cn/article/dotnet-host-service-introduction
- https://www.azuredeveloper.cn/article/using-dotnet-host-run-long-time-task
- https://www.azuredeveloper.cn/article/how-to-implement-queue-worker-by-host-model
- https://www.azuredeveloper.cn/article/how-to-implement-a-windows-service-by-host-model
- https://www.azuredeveloper.cn/article/how-to-implement-hosted-service-by-yourself
这些文章是对.Net Host
模型介绍比较全面,可以加深一些了解。
我们也可以利用.Net Host
模型给MVVM
桌面应用添加一些功能,例如最重要的依赖注入,以及日志,配置等等。
我们先启动Visual studio
,创建一个wpf
项目(使用wpf
项目演示),然后添加如下几个包的引用:
Microsoft.Extensions.Hosting
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Configuration
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Logging.Debug
Microsoft.Extensions.Logging.EventLog
安装好上述包之后,在Solution Explorer
里展开项目,找到App.xaml.cs
打开编辑:
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows;
首先我们引入必要的程序集引用, 然后在App
里定义Host的实例
:
private IHost _host;
然后我们在App
的构造函数里开始初始化:
public App() { //创建Host实例 _host = Host.CreateDefaultBuilder() .ConfigureAppConfiguration((context, builder) => { //设定启动目录 builder.SetBasePath(context.HostingEnvironment.ContentRootPath); //启用从环境变量中读入配置 builder.AddEnvironmentVariables(); //定义配置文件,从配置文件中读入配置。 builder.AddJsonFile("appsettings.json", optional: false); } ) .ConfigureServices( (context, services) => { //配置service, windows, viewview等等,都可以在这里添加到scope里 //这里配置的就是依赖注入。 services.AddSingleton<MainWindow>(); }) .ConfigureLogging(logging => { //配置日志。 logging.AddConsole(); logging.AddDebug(); logging.AddEventLog(); }) .Build(); }
然后需要注意的是在App.xaml
里配置相应的方法,如下:
<Application x:Class="WPFWithHostBuilder.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WPFWithHostBuilder" Startup="Application_Startup" Exit="Application_Exit" > <Application.Resources> </Application.Resources> </Application>
主要是定义Startup
和Exit
定义好方法之后,再回到App.xaml.cs
中定义相应的方法:
private async void Application_Startup(object sender, StartupEventArgs e) { await _host.StartAsync(); var mainWindow = _host.Services.GetService<MainWindow>(); mainWindow!.Show(); } private async void Application_Exit(object sender, ExitEventArgs e) { using (_host) { await _host.StopAsync(TimeSpan.FromSeconds(5)); } }
这样就可以在整个项目中直接使用依赖注入,以及日志,配置等系统了。要做的仅仅只需要从构造函数注入所需要的资源。当然前提是在App
的构造函数中先根据scope
来注入。