C# System.InvalidOperationException: 当前 TransactionScope 已经完成

C# System.InvalidOperationException: The current TransactionScope is already complete

我在执行我的源代码时遇到了这个错误。但是我在互联网上获得的信息似乎不多。因此,我希望能从这里的专业人士那里得到一些线索。

源代码

public List<string> GetData (List<long> Id)
{
    List<string> data;
    string sql = "select * from tblSample with(nolock) where SampleId in @sampleId";
    Dapper.DynamicParameters param = new Dapper.DynamicParameters();
    param.Add("@sampleId", Id);

    try
    {
        data = this.queryrunner.QueryList(sql, param);
    }
    catch (Exception ex)
    {
        logger.Error(ex.Message, ex);
    }

    return data;
}

QueryRunner class 方法

    private void OpenConnection()
    {
        if (this.conn.State != ConnectionState.Open)
        {
            this.conn.Open();
        }
    }

    public List<t> QueryList(String sql, DynamicParameters param)
    {
        List<t> t;

        try
        {
            OpenConnection();
            t = this.conn.Query<t>(sql, param).ToList();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            logger.Debug(ex.Message, ex);
            LogSqlErrorException(this.conn.Database, sql, param);
            throw;
        }
        finally
        {
            CloseConnection();
        }

        return t;
    }

这是错误日志的一部分:-

System.InvalidOperationException: The current TransactionScope is already complete.
at System.Transactions.Transaction.get_Current()
at System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(Transaction& transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)<br> at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)<br> at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)<br> at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.Open()

这一行OpenConnection有点味道,我猜这就是你的问题所在

try
{
    OpenConnection();
    ...

这样更有意义

try
{
    //OpenConnection();
    conn.Open();
    queryrunner.QueryList(sql, param)
}
finally
{
    //CloseConnection();
    conn.Close();
}

通过这种方式,您可以强迫自己控制连接生命周期,而不是试图耍花招,然后在另一个上下文中进行第二次猜测

Its actually better if you put it a using statement, however i'm not sure of the framework you are using, and/or the syntax