Miniprofiler profiled connection with ormlite fails on async methods

On .Net Framework 4.8, servicestack 6.2 and StackExchange.MiniProfiler 4.2.22, when an async call is made, it blows up with

Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbCommand' to type 'Npgsql.NpgsqlCommand'.

I’ve created a small gist with repro at https://gist.github.com/brunomlopes/0ab96c0b675eaaf46f98373c8136a499.
From https://github.com/MiniProfiler/dotnet/issues/519 it seems to be an assumption with SS.

The basic code is:

[Fact]
public async Task Test()
{
    var connectionFactory = CreateConnectionFactoryFactory();

    using (var db = connectionFactory.OpenDbConnection())
    {
        var query = "select @category as Category";

        var d = (await db.SelectAsync<Result>(query, new { category = "some text"}))
            .ToArray();

        Assert.NotEmpty(d);
        Assert.Equal("some text", d[0].Category);

    }
}

private static OrmLiteConnectionFactory CreateConnectionFactoryFactory()
{
    string sqlConnectionString =
        "Server=localhost;Port=5432;User Id=postgres;Database=postgres";
    return new OrmLiteConnectionFactory(sqlConnectionString, PostgreSqlDialect.Provider)
    {   
        ConnectionFilter = connection =>
        {
            // This forces a specific datestyle when communicating with PGSQL, and ensures that we
            // use our datetime format even if the database has a separate format.
            connection.ExecuteNonQuery(@"SET datestyle TO ""ISO, DMY""");
            return new ProfiledDbConnection((DbConnection) connection, MiniProfiler.Current);
        }
    };
}

Is this a bug?

We’ve only ever supported our customized Built-in Mini Profiler which still works for this example.

We’ve never attempted to support the unmodified MiniProfiler, you’re saying it blows up with the async API, does the sync version work?

Have you seen our new built-in Profiling UI in the latest release?

Thanks for the quick response.
The sync version has been working so far. We’re now spiking async on our codebase, and this error started appearing on all async queries using ormlite.

We’ll be moving to .net core, and since the miniprofiler there is the unmodified one, we were interested in keeping it, so that it’s easier to move when the time comes.

Ok if it works with async then it should also work for async APIs after removing the down casting to concrete ADO .NET provider classes, we can’t use interfaces here in order to be able to access the async APIs, but looks like we only need to cast to ADO .NET concrete classes to access them which MiniProfiler also inherits from so it should be ok now.

This change is available from v6.2.1+ that’s now available on MyGet.

Great, thanks, that seems to work!

1 Like