.Net 6 中的 IServiceProvider 实例
在 Startup.cs 的 .Net 5 ASP.NET 应用程序上,我有以下内容(对于 Hangfire):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
...
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(serviceProvider));
...
}
我想转移到 .Net 6 配置方式(在 Program.cs 中),但我不知道如何获取提供给 ServiceProviderJobActivator 方法的 IServiceProvider 实例。
方法是:
internal class ServiceProviderJobActivator : Hangfire.JobActivator
{
private readonly IServiceProvider _serviceProvider;
public ServiceProviderJobActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}
我已经尝试过:
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(app.Services));
我也尝试过:
public override object ActivateJob(Type type)
{
return _serviceProvider.GetRequiredService(type);
}
但 ActivateJob 在这两种情况下都返回 null
谢谢
On my .Net 5 ASP.NET application at Startup.cs I have the follwing (for Hangfire):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
...
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(serviceProvider));
...
}
I want to move to the .Net 6 way of configuration (in Program.cs), but I don't know how to get an instance of IServiceProvider to provide to the ServiceProviderJobActivator method.
The method is:
internal class ServiceProviderJobActivator : Hangfire.JobActivator
{
private readonly IServiceProvider _serviceProvider;
public ServiceProviderJobActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}
I have tried:
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(app.Services));
I also tried:
public override object ActivateJob(Type type)
{
return _serviceProvider.GetRequiredService(type);
}
but the ActivateJob returns null in both cases
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何想了解如何获取
IServiceProvider
实例的人,在调用builder.Build()
后,app.Services
属性将解析为IServiceProvider
实例。For anyone looking how to get a
IServiceProvider
instance, after you callbuilder.Build()
theapp.Services
property will resolve as anIServiceProvider
instance.您是否尝试过类似 将 .NET 5“基于启动”的应用升级到 .NET 6 ?
它应该导致类似的结果:
Have you tried something like option 3 from Upgrading a .NET 5 "Startup-based" app to .NET 6 ?
It should lead to something like :