Transient Failure handling in .net core 2.1 MVC for MySQL Database

六眼飞鱼酱① 提交于 2020-06-27 17:17:25

问题


services.AddDbContext<MyContext>(options =>
{
    options.UseSqlServer(mysqlConnection,
    sqlServerOptionsAction: sqlOptions =>
    {
        sqlOptions.EnableRetryOnFailure(
        maxRetryCount: 10,
        maxRetryDelay: TimeSpan.FromSeconds(30),
        errorNumbersToAdd: null);
    });
});

I found this code snippet at:

https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/implement-resilient-entity-framework-core-sql-connections

My DB is MySQL 5.7

I changed the above code to :

That means EnableRetryOnFailure is not available for MySQL DB. How do i set the retry, delay etc.. policies now?

Also if i try to set the ExecutionStrategy function i get this:

Then i tried Create my own Strategy using:

public class MyStrategy: ExecutionStrategy
{
   ......
}

but how to use this class now?


回答1:


There is a library for this: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Setup steps:

  1. Donwload Pomelo.EntityFrameworkCore.MySql from NuGet.

  2. Add this using to your class:

    using Pomelo.EntityFrameworkCore.MySql.Infrastructure;

  3. Add this to your ConfigureServices method:

    services.AddDbContextPool<ApplicationDbContext>( 
    options => options.UseMySql("Server=localhost;Database=ef;User=root;Password=123456;",
    
        mySqlOptions =>
        {
            mySqlOptions.ServerVersion(new Version(5, 7, 17), ServerType.MySql)
            .EnableRetryOnFailure(
            maxRetryCount: 10,
            maxRetryDelay: TimeSpan.FromSeconds(30),
            errorNumbersToAdd: null); 
        }
    ));
    


来源:https://stackoverflow.com/questions/55432473/transient-failure-handling-in-net-core-2-1-mvc-for-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!