asp.net-core - 如何在单例服务中使用数据库上下文?

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

我想将已完成的国际象棋游戏存储在数据库中,以支持用户观看重播。

到目前为止,我有一个单例 GameManager存储所有正在进行的游戏。所以在 startup.cs 内我有以下代码行:

services.AddSingleton<IBattleManager, BattleManager>();

现在我想要 BattleManager访问 DbContext保存已完成的游戏。
public class BattleManager : IBattleManager
{
    //...
    private void EndGame(ulong gameId)
    {
        var DbContext = WhatDoIPutHere?
        lock(gameDictionary[gameId])
        {
            DbContext.Replays.Add(new ReplayModel(gameDictionary[gameId]));
            gameDictionary.Remove(gameId)
        }
    }
}

无论如何有可能实现这一目标吗?如何?

失败的尝试:
public class BattleManager : IBattleManager
{
    Data.ApplicationDbContext _context;
    public BattleManager(Data.ApplicationDbContext context)
    {
        _context = context;
    }
}

这显然会失败,因为无法注入(inject) EF Core DbContext进入这样的单例服务。

我有一种模糊的感觉,我应该做这样的事情:
using (var scope = WhatDoIPutHere.CreateScope())
{
    var DbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    DbContext.Replays.Add(new ReplayModel(...));
}

这是正确的方向吗?

最佳答案

你在正确的轨道上。 IServiceScopeFactory 可以做到这一点。

public class BattleManager : IBattleManager {

    private readonly IServiceScopeFactory scopeFactory;

    public BattleManager(IServiceScopeFactory scopeFactory)
    {
        this.scopeFactory = scopeFactory;
    }

    public void MyMethod() {
        using(var scope = scopeFactory.CreateScope()) 
        {
            var db = scope.ServiceProvider.GetRequiredService<DbContext>();

            // when we exit the using block,
            // the IServiceScope will dispose itself 
            // and dispose all of the services that it resolved.
        }
    }
}
DbContext会表现得像它有一个 Transient范围内using陈述。

关于asp.net-core - 如何在单例服务中使用数据库上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51939451/

相关文章:

c# - HttpClientFactory 任务取消异常 : Unable to read data from the transport connection

objective-c - 如何在类方法中访问 Objective-C 属性

ios - 拥有同步线程的目的

c# - 重构许多重复的 ProducesResponseType 以使其干燥

asp.net-mvc - 为特定 Controller ASP.Net Core MVC 设置请求超时

c++ - C++ 中的单例类

c# - Entity Framework 核心 - 不在

c# - 分离时删除 Entity Framework Core 中的一比零或一相关数据 (sqlite)

c# - .NET 核心 Entity Framework InvalidOperationException

rest - 使用 REST API 将消息发送到 .Net Core Core 中的 azure 服务总线队列