我正在将Hangfire 1.5.6与MSMQ和DTC事务配合使用(以便使用远程MSMQ)。问题在于,每一个长时间运行的作业(> 1分钟)都会在1分钟后自动重新计划。这也以奇怪的顺序完成:在取消旧作业之前,再次启动作业。

如果我将Hangfire配置为不使用DTC事务,则作业运行正常。

在源代码中,我注意到MsmqDtcTransaction正在打开TransactionScope。在作业执行期间是否需要打开此作用域?对于长时间运行的作业,SQL Server事务日志会发生什么(此特定作业正在向DB中插入大量数据)?

我尝试在app.config中设置事务超时(这也需要在machine.config中进行更改):

<system.transactions>
<machineSettings maxTimeout="02:00:00"/>
<defaultSettings timeout="02:00:00" />
</system.transactions>


经过这些更改,作业在DTC事务打开的情况下运行正常。

Hangfire是如何与远程MSMQ一起工作的?无需交易即可使用吗?

最佳答案

Hangfire作者已在1.6.3中检查了此修复程序:Hangfire Forums

从Hangfire源代码:

public MsmqDtcTransaction()
{
   _scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.Zero);
}




public Message Receive(MessageQueue queue, TimeSpan timeout)
{
    var message = queue.Receive(timeout, MessageQueueTransactionType.Automatic);
    _suppressedScope = new TransactionScope(TransactionScopeOption.Suppress, TimeSpan.Zero);

    return message;
}

关于c# - 将MSMQ与DTC结合使用时,Hangfire作业在1分钟后会自动重新安排,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37415896/

10-17 00:25