【问题标题】:Does Entity Framework Core have connection resiliency configured by default?Entity Framework Core 是否默认配置了连接弹性?
【发布时间】:2020-04-27 01:20:09
【问题描述】:

我正在阅读through the documentation on connection resiliency in Entity Framework Core 并了解到可以在配置服务时在 ASP.NET Core 中配置连接弹性:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<PicnicContext>(
        options => options.UseSqlServer(
            "<connection string>",
            providerOptions => providerOptions.EnableRetryOnFailure()));
}

我注意到对于其他服务,例如 Azure 搜索客户端,默认启用重试机制。 Entity Framework Core (3.1.3) 也是这种情况,还是我需要显式调用providerOptions =&gt; providerOptions.EnableRetryOnFailure()

SqlServerDbContextOptionsBuilder documentation 没有提供有关默认值的详细信息。

【问题讨论】:

    标签: asp.net-core entity-framework-core


    【解决方案1】:

    重试失败功能通过SqlServerRetryingExecutionStrategy 实现。调用EnableRetryOnFailure,基本会把这个策略配置为执行策略。

    默认情况下,Entity Framework Core 不会使用执行策略。或者更确切地说,默认执行策略是 NoopExecutionStrategy(在 EF Core 5 中将重命名为 NonRetryingExecutionStrategy),它什么都不做。

    所以不,没有默认重试。不进行自动重试通常是经过深思熟虑的选择。默认情况下,查询会失败,错误会冒出来,以便开发人员处理它。如果一个应用程序是为重试而编写的,那么他们可以通过最适合的配置启用该功能。

    【讨论】:

    • 感谢您非常明确的回答!验证:在使用 Azure 托管的 SQL 服务器时是否建议启用重试策略?那至少是how I understood it from the documentation
    • 我无法对此发表评论,因为我没有足够的经验,但我不希望得到一般性的推荐。这可能取决于。例如,对于高性能用例,最好立即失败而不是由于重试而延迟执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多