gpt4 book ai didi

dependency-injection - 需要访问在同一个 HostBuilder().ConfigureServices 中配置的 HostBuilder 内的服务实例

转载 作者:行者123 更新时间:2023-12-04 15:41:33 25 4
gpt4 key购买 nike

是否可以在 HostBuilder() 中访问 DI 配置服务的实例?配置?

这就是我的意思:

return new HostBuilder()
.UseOrleans((cntx, builder) =>
{
builder.ConfigureApplicationParts(parts =>
{
// This is where I want to access instance of IMyService
// in order to help Orleans builder to configure Orleans properly
}
})
.ConfigureServices(services =>
{
services.AddSingleton<IMyService, MyService>();
})
.RunConsoleAsync();

最佳答案

如果结果是 XY problem ,并且您正在尝试创建一些启动任务,有一个 AddStartupTask 延期

return new HostBuilder()
.UseOrleans((cntx, builder) => {
//Add a startup task to be executed when the silo has started.
builder.AddStartupTask((sp, token) => {
// access instance of IMyService
IMyService service = sp.GetRequiredService<IMyService>();

//...use service as needed

return Task.CompletedTask;
});
})
.ConfigureServices(services => {
services.AddSingleton<IMyService, MyService>();
})
.RunConsoleAsync();

关于dependency-injection - 需要访问在同一个 HostBuilder().ConfigureServices 中配置的 HostBuilder 内的服务实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com