سؤال

Can anyone shed any light on the use of Glimpse with Dapper, or is it purely just for Entity Framework?

EDIT

 public List<UserRole> GetUserRoles(string userName)
    {
        using (var block = new TransactionBlock())
        {
            const string sql = "SELECT AspNetRoles.Name FROM AspNetUsers INNER JOIN " +
                               "AspNetUserRoles ON AspNetUsers.Id = AspNetUserRoles.UserId INNER JOIN " +
                               "AspNetRoles ON AspNetUserRoles.RoleId = AspNetRoles.Id " +
                               "WHERE (AspNetUsers.UserName = @userName)";
            var results = TransactionBlock.Connection.Query<UserRole>(sql, new{userName}, transaction: TransactionBlock.Transaction).ToList();
            block.Commit();
            return results;
        }
    }

*EDIT *

public class TransactionBlock : IDisposable
{
    private bool m_FirstTransactionBlock = false;
    private bool m_Disposed = false;

    public TransactionBlock(string connectionStringConfigKey = null)
    {
        if (connectionStringConfigKey != null && CurrentTransaction != null)
            throw new Exception("Can't create a new transactionBlock with a specific connectionstring if you are already within a block.");

        // Open a connection if we haven't yet already          
        if (CurrentTransaction == null) // we don't have a transaction yet so create one:
        {
            var localConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            var conn = new SqlConnection(localConnectionString); // Create a new connection object for the connection string.
            try
            {
                conn.Open();
            }
            catch (SqlException ex)
            {
                // Severity 20 errors are returned when there are connection
                // problems, so we only retry if the severity is 20.
                // Don't worry about deadlock errors in here - we're just connecting
                // to the database, not running anything which could be deadlocked.
                if (ex.Class != 20) throw;
                // Clear connection pool of all connections related to the given connection string.
                SqlConnection.ClearPool(conn);
                // Try to open the connection once again, catching any error.
                try { conn.Open(); }
                catch { conn = null; }
                // If 2nd attempt failed, throw original exception.
                if (conn == null) throw;
            }
            CurrentTransaction = conn.BeginTransaction();
            m_FirstTransactionBlock = true;
        }
    }

    public void Commit()
    {
        if (m_FirstTransactionBlock)
        {
            Dispose(true);
        }
    }

    public void Dispose()
    {
        if (m_FirstTransactionBlock)
        {
            Dispose(false);
        }
    }

    public void Dispose(bool commit)
    {
        if (m_Disposed)
            return; // committed and cleaned up already.

        try
        {
            var transaction = CurrentTransaction;
            var connection = transaction.Connection; // taking a reference to the connection object as rollback will set it to null;

            if (commit)
            {
                transaction.Commit();
            }
            else // rollback the transaction if it hasn't been commited.
            {
                transaction.Rollback();
            }

            if (connection != null)
            {
                connection.Close();
                connection.Dispose();
            }

            transaction.Dispose();
        }
        finally // ensure that regardless of connection exceptions, the state is set to disposed.
        {
            // remove current transaction from context.
            CurrentTransaction = null;
            m_Disposed = true;
        }
    }

    private static SqlTransaction CurrentTransaction
    {
        get
        {
            return CurrentContext.GetItem<SqlTransaction>("__Transaction");
        }
        set
        {
            CurrentContext.SetItem("__Transaction", value);
        }
    }

    public static SqlConnection Connection
    {
        get { return CurrentTransaction.Connection; }
    }
    public static SqlTransaction Transaction
    {
        get { return CurrentTransaction; }
    }

    public static string ConnectionString
    {
        get
        {
            return System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
        }
    }
هل كانت مفيدة؟

المحلول

Ah, thanks for the TransactionBlock code, which shows immediately why Glimpse is not intercepting any DB calls.

Try making use of the DbProviderFactories class by changing the following lines :

var localConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
var conn = new SqlConnection(localConnectionString);       

with

var localConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"];
var factory = DbProviderFactories.GetFactory(localConnectionString.ProviderName); 
var conn = factory.CreateConnection();

نصائح أخرى

Have you tried using the Glimpse.ADO package as well?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top