如何将服务注入工作和设置IOC容器配置?
我设置了一个serviceCollection
中注入了服务。问题在于,该作业(执行方法)无法运行,尽管在注入任何服务之前,它都可以正常工作。
我认为问题源于依赖性注入生命周期
,因为当我将所有生命的时间更改为singleton
时,作业就会发射。由于dbContext
应该是瞬态
我必须以另一种方式解决它。
statup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Cnn")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking), ServiceLifetime.Transient);
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddTransient<CheckTransactionConfirmJob>();
services.AddTransient<CheckPaymentConfirmationScheduler>();
services.AddTransient<IPaymentRepository, EfPlanRepository>();
}
PaymentsVice.cs:
private async Task RunPaymentCheckerJob(Payment payment, Guid userId,
CheckPaymentConfirmationScheduler scheduler)
{
IJobDetail job = JobBuilder.Create<CheckTransactionConfirmJob>()
.WithIdentity(Guid.NewGuid().ToString(), "CheckConfirmationJob")
.UsingJobData("paymentId", payment.Id)
.UsingJobData("userId", userId)
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(Guid.NewGuid().ToString(), "CheckConfirmationGroup")
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(20)
.WithRepeatCount(12))
.ForJob(job)
.Build();
await scheduler.GetScheduler().ScheduleJob(job, trigger);
}
checkTransactionConfirmjob.cs
public class CheckTransactionConfirmJob : IJob
{
private readonly IPaymentRepository _paymentRepository;
private readonly IServiceProvider _provider;
public CheckTransactionConfirmJob(IPaymentRepository paymentRepository, IServiceProvider provider)
{
_paymentRepository = paymentRepository;
_provider = provider;
}
public async Task Execute(IJobExecutionContext context)
{
try
{
/// business code
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
I set up a Job
in which I injected a service from servicecollection
. The problem is that this job (Execute method) could not be run although before injecting any services, it was working properly.
I think the problem stems from Dependency Injection Lifetime
because when I change all lifetimes to Singleton
the job is fired. For the reason that DbContext
should be Transient
I have to solve it in another way.
Statup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Cnn")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking), ServiceLifetime.Transient);
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddTransient<CheckTransactionConfirmJob>();
services.AddTransient<CheckPaymentConfirmationScheduler>();
services.AddTransient<IPaymentRepository, EfPlanRepository>();
}
PaymentService.cs:
private async Task RunPaymentCheckerJob(Payment payment, Guid userId,
CheckPaymentConfirmationScheduler scheduler)
{
IJobDetail job = JobBuilder.Create<CheckTransactionConfirmJob>()
.WithIdentity(Guid.NewGuid().ToString(), "CheckConfirmationJob")
.UsingJobData("paymentId", payment.Id)
.UsingJobData("userId", userId)
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(Guid.NewGuid().ToString(), "CheckConfirmationGroup")
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(20)
.WithRepeatCount(12))
.ForJob(job)
.Build();
await scheduler.GetScheduler().ScheduleJob(job, trigger);
}
CheckTransactionConfirmJob.cs
public class CheckTransactionConfirmJob : IJob
{
private readonly IPaymentRepository _paymentRepository;
private readonly IServiceProvider _provider;
public CheckTransactionConfirmJob(IPaymentRepository paymentRepository, IServiceProvider provider)
{
_paymentRepository = paymentRepository;
_provider = provider;
}
public async Task Execute(IJobExecutionContext context)
{
try
{
/// business code
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
石英配有两个内置替代方案,可以通过调用
usemicRosoftDependentIndentionjobfactory
或usemicRosoftDependendendentionscopedjobfactory
(everected)。为了使用Microsoft依赖项
UsemicRosoftDependentientIndoctionJobFactory
应在configureservices
方法中调用:Quartz comes with two built-in alternatives for job factory which can be configured via either calling
UseMicrosoftDependencyInjectionJobFactory
orUseMicrosoftDependencyInjectionScopedJobFactory
(deprecated).For using Microsoft dependency
UseMicrosoftDependencyInjectionJobFactory
should be called inConfigureServices
method: