在hangfire工作中执行DI服务状态

发布于 2025-02-13 02:55:48 字数 153 浏览 2 评论 0原文

帮助! 当我们在hangfire作业中放置一个表达式时,例如,在此表达式中,我们

在执行hangfire作业时使用一些服务(与数据库一起使用的DI服务),在创建作业时,这些服务的快照是否是该州的这些服务快照?

还是这些服务会反映在执行HF作业时应用程序的真实状态?

Help!
When we put an expression in a hangFire job, and for example, in this expression we use some services (DI services that work with database)

When doing a hangFire job, are these services snapshots of the state at the time the job was created?

or will these services reflect the real state in which the application is at the time the HF Job is executed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2025-02-20 02:55:48

HangFire只是将MethodInfo(包括类/接口类型)和方法的参数序列化。

当应处理作业时,它会要求DI的实例对已定义的类或接口请求。

如果DI不提供实例,并且类型是类,则试图创建该类的实例。如果类具有构造函数参数,它将尝试通过di(递归)接收这些参数的实例,或者在可用的情况下调用默认构造函数。

因此,如果使用或不使用新的实例,则主要取决于DI,并且所有工作均以自己的范围运行,这意味着只有单身人士可以共享。

显然,如果过程A创建作业和处理B处理它,则不会通过DI共享任何内容,并且必须通过数据库,分布式缓存,BLOB存储或类似的数据库共享。

Hangfire just serializes the MethodInfo (including class/interface type) and the arguments of the method.

When the job should be processed it requests an instance from DI for the defined class or interface.

If DI doesn't provide an instance and the type is a class it tries to create an instance of that class. If the class has constructor arguments it tries to receive instances of these arguments also through DI (recursive) or calls the default constructor if available.

So, mainly it is up to DI if fresh instances are used or not and all jobs are run in their own scope, which means only Singletons are shared.

Clearly if process A creates a job and process B processes it, nothing will be shared through DI and must be shared through a database, distributed cache, blob storage or similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文