有人有集成 autofac 和 Quartz.Net 的经验吗?如果是这样,最好在哪里控制生命周期管理——IJobFactory、IJob 的执行中还是通过事件侦听器?
现在,我正在使用自定义 autofac IJobFactory
来创建 IJob
实例,但我没有一种简单的方法来插入 ILifetimeScope< /code> 在 IJobFactory 中,以确保清除注入 IJob 中的所有昂贵资源。作业工厂只是创建一个作业实例并返回它。以下是我当前的想法(希望有更好的想法...)
-
看起来大多数 AutoFac 集成都以某种方式将 ILifetimeScope
包裹在他们创建的工作单元周围。明显的强力方法似乎是将 ILifetimeScope
传递到 IJob
中,并让 Execute
方法创建一个子 ILifetimeScope
code> 并实例化那里的任何依赖项。这似乎有点太接近服务定位器模式,这反过来似乎违背了 autofac 的精神,但它可能是确保正确处理作用域的最明显的方法。
-
我可以插入一些 Quartz 事件来处理作业执行堆栈的不同阶段,并在那里处理生命周期管理。这可能需要做更多的工作,但如果它能够更清晰地分离关注点,那么可能是值得的。
-
确保 IJob 是 IServiceComponent
类型的简单包装器,它将完成所有工作,并将其请求为 Owned
或 Func
。我喜欢 autofac 的感觉,但我不喜欢它对于 IJob 的所有实现者来说不是严格执行的。
Does anyone have any experience integrating autofac and Quartz.Net? If so, where is it best to control lifetime management -- the IJobFactory, within the Execute of the IJob, or through event listeners?
Right now, I'm using a custom autofac IJobFactory
to create the IJob
instances, but I don't have an easy way to plug in to a ILifetimeScope
in the IJobFactory to ensure any expensive resources that are injected in the IJob are cleaned up. The job factory just creates an instance of a job and returns it. Here are my current ideas (hopefully there are better ones...)
-
It looks like most AutoFac integrations somehow wrap a ILifetimeScope
around the unit of work they create. The obvious brute force way seems to be to pass an ILifetimeScope
into the IJob
and have the Execute
method create a child ILifetimeScope
and instantiate any dependencies there. This seems a little too close to a service locator pattern, which in turn seems to go against the spirit of autofac, but it might be the most obvious way to ensure proper handling of a scope.
-
I could plug into some of the Quartz events to handle the different phases of the Job execution stack, and handle lifetime management there. That would probably be a lot more work, but possibly worth it if it gets cleaner separation of concerns.
-
Ensure that an IJob is a simple wrapper around an IServiceComponent
type, which would do all the work, and request it as Owned<T>
, or Func<Owned<T>>
. I like how this seems to vibe more with autofac, but I don't like that its not strictly enforceable for all implementors of IJob.
发布评论
评论(2)
在不太了解 Quartz.Net 和 IJob 的情况下,我仍然大胆提出一个建议。
考虑以下作业包装器:
给定以下注册:
作业工厂现在可以解析此包装器:
注意:a 生命周期范围 将作为
ownedJob
实例的一部分创建,在本例中为Owned
类型。SomeJob
所需的任何InstancePerLifetimeScope
或InstancePerDependency
依赖项都将与Owned
实例一起创建和销毁。Without knowing too much about Quartz.Net and
IJob
s, I'll venture a suggestion still.Consider the following Job wrapper:
Given the following registrations:
A job factory could now resolve this wrapper:
Note: a lifetime scope will be created as part of the
ownedJob
instance, which in this case is of typeOwned<SomeJob>
. Any dependencies required bySomeJob
that isInstancePerLifetimeScope
orInstancePerDependency
will be created and destroyed along with theOwned
instance.看看 https://github.com/alphacloud/Autofac.Extras.Quartz 。它还可以作为 NuGet 包 https://www.nuget.org/packages/Autofac 提供。 Extras.Quartz/
我知道的有点晚了:)
Take a look at https://github.com/alphacloud/Autofac.Extras.Quartz. It also available as NuGet package https://www.nuget.org/packages/Autofac.Extras.Quartz/
I know it a bit late:)