distributed-transactions - Hangfire:Azure SQL:添加新作业失败

标签 distributed-transactions hangfire azure-sql-database

出现以下错误

内部异常:“对象引用未设置到对象的实例。” 堆栈跟踪: 在 System.Transactions.Transaction.GetPromotedToken()

错误来自行 connection.EnlistTransaction(Transaction.Current);在下面的代码中,即使我在连接字符串中设置了 enlist=false。

   `internal T UseTransaction<T>([InstantHandle] Func<DbConnection, DbTransaction, T> func, IsolationLevel? isolationLevel)
    {
     #if NETFULL
        using (var transaction = CreateTransaction(isolationLevel ?? _options.TransactionIsolationLevel))
        {
            var result = UseConnection(connection =>
            {
                connection.EnlistTransaction(Transaction.Current);
                return func(connection, null);
            });

            transaction.Complete();

            return result;
        }
}`

当我们设置 enlist=true 时,我们会在 connection.Open(); 这行出错从下面的代码

    internal DbConnection CreateAndOpenConnection()
    {
        if (_existingConnection != null)
        {
            return _existingConnection;
        }

        var connection = new SqlConnection(_connectionString);
        connection.Open();

        return connection;
    }

这两种方法都存在于 Hangfire.SqlServer.SqlServerStorage 类下

相同的代码在连接到本地数据库(SQL Server 2014)时有效。

登陆讨论WCF Transaction against Azure SQL DB ,不确定它是否与此问题相关。

编辑:

挂火配置

GlobalConfiguration.Configuration.UseSqlServerStorage(
                Database.ConnectionString,
                new SqlServerStorageOptions
                {
                    QueuePollInterval = TimeSpan.FromSeconds(10),
                    PrepareSchemaIfNecessary = true
                });

作业排队

BackgroundJob.Enqueue(() => Update(connectionToken, intFileUniqueId));

请帮忙。 提前致谢。

最佳答案

发现问题。调用排队作业的方法在事务内部,这会以某种方式导致分发事务,而 Azure SQL 不支持它。

考虑下面的代码:

    public override void UpdateFile(int intUniqueId)
    {
        using (var scope = GetTransactionScope(...))
        {
             QueueJob<Updater>(x => x.Update(token, intUniqueId));
            scope.Complete();
        }
    }


    private static void QueueJob<T>(Expression<Action<T>> action)
    {
            BackgroundJob.Enqueue(action);
    }

删除被调用方方法中的事务范围是有效的。 我正在检查事务范围的必要性,如果不需要,我会删除它。!!!

关于distributed-transactions - Hangfire:Azure SQL:添加新作业失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41632274/

相关文章:

c# - 如何在 C# 中使用 JobStorage 获取所有 Hangfire 作业的列表?

node.js - 使用 'Azure AD-Universal with MFA' 连接 Azure SQL 数据库的 NodeJs 代码

oracle - "ORA-14450: attempt to access a transactional temp table already in use"在复合触发器中

distributed - 新手术语中的顺序一致性?

wcf - 找不到配置绑定(bind)扩展

c# - 如何安排在 Hangfire 中的特定日期运行的作业

c# - 如何使用 ServiceStack Funq IoC 解析类型

azure - 使用 Set-AzSqlServerAudit 排除 Azure SQL Server 审核日志中的 SQL 语句

azure - 如何为 Azure Web Apps/Azure 网站创建安装程序

mysql - 当前不支持同一事务内的多个同时连接或具有不同连接字符串的连接