Skip to main content

Fixed: "CS0120: An object reference is required" for ClearAllPoolsAsync in MySql.Data

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

MySqlConnection.ClearAllPools() is static, so I wrote await MySqlConnection.ClearAllPoolsAsync(), and the compiler said CS0120. The async versions in MySql.Data aren't static, unlike the methods they mirror.

What I Was Doing​

I was writing a small program to show how to clear the connection pool, on MySql.Data 26.10.0 and .NET 10:

clear-pool.cs
await MySqlConnection.ClearAllPoolsAsync();
Terminal
dotnet run clear-pool.cs

What I Expected​

It to compile, as the method it mirrors does.

What I Got​

Output
/Users/you/projects/mysql-connection-test/clear-pool.cs(5,7): error CS0120: An object reference is required for the non-static field, method, or property 'MySqlConnection.ClearAllPoolsAsync(CancellationToken)'

The build failed. Fix the build errors and run again.

dotnet run failing with error CS0120: An object reference is required for the non-static field, method, or property MySqlConnection.ClearAllPoolsAsync(CancellationToken).

What I Checked​

The class's members, in MySql.Data 26.10.0:

MySqlConnection
public static void ClearPool(MySqlConnection connection)
public Task ClearPoolAsync(MySqlConnection connection, CancellationToken cancellationToken = default)
public static void ClearAllPools()
public Task ClearAllPoolsAsync(CancellationToken cancellationToken = default)

The sync ones are static. The async ones aren't.

Why It Happens​

CS0120 means you've called an instance method as though it were static. Here, the two pairs of methods just differ.

The Fix​

Use the static methods. They're simple calls, and there's no reason to wait on them in most code:

clear-pool.cs
MySqlConnection.ClearAllPools();

To clear one pool, pass a connection that has that connection string. It doesn't need to be open:

clear-pool.cs
await using (var connection = new MySqlConnection(connectionString))
{
MySqlConnection.ClearPool(connection);
}

Or, if you do want the async versions, call them on an instance:

clear-pool.cs
await using var connection = new MySqlConnection(connectionString);
await connection.ClearAllPoolsAsync();

After the Fix​

Terminal
dotnet run clear-pool.cs
Output
First open: connection id 33
Second open: connection id 33
After ClearAllPools: connection id 34
After ClearPool: connection id 35

The id changes because each clear closed the pooled connection, so the next open needed a new one.

The clear-pool program printing the same connection id for two opens, then a new id after ClearAllPools, and another after ClearPool.

Common Mistakes​

MistakeWhat happensDo this instead
Adding new MySqlConnection().ClearAllPoolsAsync() everywhereIt works, and reads oddlyUse the static method
Clearing pools routinelyEvery request then pays for a new connectionOnly clear after a real change, such as a failover

Troubleshooting​

SymptomLikely causeFix
CS0176 on the sync method through an instanceThe reverse mistakeCall it on the class
Connections still there after clearingConnections in use close when they're returnedCheck again once they've finished

Conclusion​

ClearPool and ClearAllPools are static, and their async versions aren't. Call the static ones on the class.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.