0

I have an ASP.NET Core MVC application where I inject a DbContext into a controller. I want to know if there is a way to intercept the connection timeout exception while constructing the DbContext returning a specific error response code.

I noticed that the exception is thrown inside the method AddDbContext in the Startup class:

services.AddDbContext<MyDbContext>(options =>
        {
            options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
                ServerVersion.AutoDetect(Configuration.GetConnectionString("DefaultConnection")), o => 
                {
                    o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
                });
        });

EDIT: Could the exception be thrown from the AutoDetect option which opens a connection to the DB to retrieve its version? I'm using the Pomelo.EntityFrameworkCore.MySql NuGet package, version 5.0.0

Thanks

3
  • 1
    Why do you want that? A DbContext isn't a connection, it's a Unit-of-Work. It works disconnected and only opens a short-lived connection when loading data, or when persisting all cached changes when SaveChanges is called. If you want to catch a timeout exception use a normal exception handling block around the ToList or SaveChanges cals Commented Jun 17, 2021 at 14:42
  • Alternatively, you could implement a command interceptor and/or execution strategy. This specific link is for SQL Server, but the base infrastructure works for all database types: learn.microsoft.com/en-us/dotnet/architecture/microservices/… Commented Jun 17, 2021 at 14:56
  • @PanagiotisKanavos I think the problem here is the AutoDetect option used to detect the running version of MySql instance. Behind the scene, it opens a connection to the DB to retrieve its version. So, maybe the question is: How to intercept this timeout? Commented Jun 17, 2021 at 15:34

1 Answer 1

1

If you want to use the ServerVersion.AutoDetect() method to automatically get your database server version, then you usually want to query it only once on application startup and then reuse it in your UseMySql() calls:

var connectionString = Configuration.GetConnectionString("DefaultConnection");
var serverVersion = ServerVersion.AutoDetect(connectionString);

services.AddDbContext<MyDbContext>(options => options
    .UseMySql(connectionString, serverVersion, o => o
        .UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));

In addition, if you want some retry logic for ServerVersion.AutoDetect(), take a look at Missing retry policy for ServerVersion.AutoDetect() #1368.

You can also explicitly set your server version by using the MySqlServerVersion or MariaDbServerVersion classes.

Could the exception be thrown from the AutoDetect option which opens a connection to the DB to retrieve its version? I'm using the Pomelo.EntityFrameworkCore.MySql NuGet package, version 5.0.0

Yes, that is where the exception gets thrown in your case. Otherwise, the exception would be thrown when the DbContext first needs the database connection (usually when you execute your first query), which is sometime later after calling AddDbContext().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.