使用 Autofac 和 IHostBuilder 解析服务
Resolve service with Autofac and IHostBuilder
我有一个 MVVM wpf 应用程序,我在其中尝试使用 Autofac 实现依赖注入。
App.xaml.cs :
public App(){
// getting config path here
...
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(configFilePath, optional: true, reloadOnChange: true)
.Build();
// Host builder
host = Host.CreateDefaultBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterModule(new ConfigurationModule(config));
builder.RegisterType<MyService>().As<IMyService>();
builder.RegisterType<MainViewModel>().As<IMainViewModel>();
builder.Register(c => c.Resolve<IHttpClientFactory>().CreateClient()).As<HttpClient>();
_containerBuilder = builder;
})
.Build();
}
然后在应用程序启动时我想设置我的视图模型数据上下文
protected override async void OnStartup(StartupEventArgs e)
{
await host.StartAsync();
// How to get a reference to my container or scope here to be able to resolve IMainViewModel ?
var container = ...
var mainVM = container.Resolve<IMainViewModel>();
var window = new MainWindow { DataContext = mainVM };
window.Show();
base.OnStartup(e);
}
如何在 OnStartup 方法中解析我的 MainViewModel?
来自IHost
的服务提供者可用于解析主视图模型
using Microsoft.Extensions.DependencyInjection;
//...
protected override async void OnStartup(StartupEventArgs e) {
await host.StartAsync();
IServiceProvider services = host.Services;
IMainViewModel mainVM = services.GetService<IMainViewModel>();
var window = new MainWindow { DataContext = mainVM };
window.Show();
base.OnStartup(e);
}
它将在后台调用 Autofac 容器。服务提供者只是 Autofac 容器之上的抽象,因为
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
已调用。
我有一个 MVVM wpf 应用程序,我在其中尝试使用 Autofac 实现依赖注入。
App.xaml.cs :
public App(){
// getting config path here
...
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(configFilePath, optional: true, reloadOnChange: true)
.Build();
// Host builder
host = Host.CreateDefaultBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterModule(new ConfigurationModule(config));
builder.RegisterType<MyService>().As<IMyService>();
builder.RegisterType<MainViewModel>().As<IMainViewModel>();
builder.Register(c => c.Resolve<IHttpClientFactory>().CreateClient()).As<HttpClient>();
_containerBuilder = builder;
})
.Build();
}
然后在应用程序启动时我想设置我的视图模型数据上下文
protected override async void OnStartup(StartupEventArgs e)
{
await host.StartAsync();
// How to get a reference to my container or scope here to be able to resolve IMainViewModel ?
var container = ...
var mainVM = container.Resolve<IMainViewModel>();
var window = new MainWindow { DataContext = mainVM };
window.Show();
base.OnStartup(e);
}
如何在 OnStartup 方法中解析我的 MainViewModel?
来自IHost
的服务提供者可用于解析主视图模型
using Microsoft.Extensions.DependencyInjection;
//...
protected override async void OnStartup(StartupEventArgs e) {
await host.StartAsync();
IServiceProvider services = host.Services;
IMainViewModel mainVM = services.GetService<IMainViewModel>();
var window = new MainWindow { DataContext = mainVM };
window.Show();
base.OnStartup(e);
}
它将在后台调用 Autofac 容器。服务提供者只是 Autofac 容器之上的抽象,因为
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
已调用。