gpt4 book ai didi

c# - 命令已在进行中

转载 作者:行者123 更新时间:2023-12-03 22:47:45 25 4
gpt4 key购买 nike

我正在尝试为我正在开发的 Web 应用程序运行后台工作程序。我使用 Npgsql 作为我的 EF Core 提供程序。

为了澄清,我注入(inject)了我的DbContext具有 transient 生命周期,并且允许在我的连接字符串中进行池化,但是,每当我尝试对其进行测试时,都会出现以下错误:

Npgsql.NpgsqlOperationInProgressException: A command is already in progress: [My Query Here]



我有我的 Program类设置如下:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
// Get the configuration
IConfiguration config = hostContext.Configuration;

// DbContext
services.AddDbContext<MyDbContext>(options => options.UseNpgsql(config.GetConnectionString("PostgreSQLString")), ServiceLifetime.Transient);

services.AddHostedService<Worker>();
services.AddScoped<IDTOService, BackgroundDTOService>();
});
}

然后导致我的 Worker类(class)
public class Worker : BackgroundService
{
private Logger logger;

public Worker(IServiceProvider services, IConfiguration configuration)
{
this.Services = services;

var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("PostgreSQLString"));
var context = new StatPeekContext(optionsBuilder.Options);

this.logger = new Logger(new LogWriter(context));
}

public IServiceProvider Services { get; }

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
this.logger.LogInformation("ExecuteAsync in Worker Service is running.");
await this.DoWork(stoppingToken);
}

private async Task DoWork(CancellationToken stoppingToken)
{
using (var scope = Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<MyDbContext>();
var dtoService = scope.ServiceProvider.GetRequiredService<IDTOService>();
await dtoService.ProcessJSON(stoppingToken);
}
}

public override async Task StopAsync(CancellationToken cancellationToken)
{
this.logger.LogInformation("Worker service is stopping.");
await Task.CompletedTask;
}
}

这导致我的 BackGroundDTOService
public class BackgroundDTOService : IDTOService
{
private int executionCount = 0;
private Logger logger;
private MyDbContext context;
private DbContextOptionsBuilder<MyDbContext> optionsBuilder;

public BackgroundDTOService(IConfiguration configuration, MyDbContext context)
{
this.optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
this.optionsBuilder.UseNpgsql(configuration.GetConnectionString("PostgreSQLString"));

this.logger = new Logger(new LogWriter(new MyDbContext(this.optionsBuilder.Options)));

this.context = context;
}

public async Task ProcessJSON(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
this.executionCount++;

this.logger.LogInformation($"DTO Service is working. Count: {this.executionCount}");

this.ProcessTeams();

await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
}
}

public void ProcessTeams()
{
// Add Any Franchises that don't exist
var franchiseDumps = this.context.RequestDumps.Where(rd => rd.Processed == false && rd.DumpType == "leagueteams");
foreach (RequestDump teamDump in franchiseDumps)
{
var league = this.context.Leagues.Include(l => l.Franchises).FirstOrDefault(l => l.Id == teamDump.LeagueID);
var teams = Jeeves.GetJSONFromKey<List<DTOTeam>>(teamDump.JsonDump, "leagueTeamInfoList");

foreach (DTOTeam team in teams)
{
this.UpdateFranchise(team, league);
}

this.logger.LogInformation($"DTO Service Processed League Teams on count {this.executionCount}");
}

this.context.SaveChanges();
}

该错误似乎在捕获 franchiseDumps 后立即发生当它试图获取 league

最佳答案

您能否尝试具体化查询:

 var franchiseDumps = this.context.RequestDumps.Where(rd => rd.Processed == false && rd.DumpType == "leagueteams").ToList();

关于c# - 命令已在进行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61052687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com