某些服务无法构建.net 8工人服务

问题描述 投票:0回答:1

这是我的程序.cs

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSingleton<cMain>();


// Ensure cMain is constructed before using it to get the connection string
var serviceProvider = builder.Services.BuildServiceProvider();
var cMainInstance = serviceProvider.GetRequiredService<cMain>();
var connectionString = cMainInstance.ConnectionString;

// Register the DbContext with the correct connection string
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddHostedService<Worker>();


var host = builder.Build();
host.Run();

这是我的 UnitOfWork 和 IUnitOfWork

public class UnitOfWork : IUnitOfWork
{
    private readonly ApplicationDbContext _db;


    public UnitOfWork(ApplicationDbContext db)
    {
        _db = db;
       BreadCrumbs = new BreadCrumbRepository(_db);

    }
    
    public IBreadCrumbRepository BreadCrumbs { get; private set; }

    public void Save()
    {
        _db.SaveChanges();
    }
}
public interface IUnitOfWork
{
    IBreadCrumbRepository BreadCrumbs { get; }
    void Save();
}

我正在尝试将 IUnitOfWork 注入 Worker 中,但出现以下错误

 public class Worker : BackgroundService
 {
     private readonly ILogger<Worker> _logger;
     private readonly cMain _cmain;
     private readonly IConfiguration _configuration;
     private readonly IUnitOfWork _uow;


     public Worker()
     {
         
     }
     public Worker(ILogger<Worker> logger, IConfiguration configuration, IUnitOfWork uow)
     {
         _logger = logger ?? throw new ArgumentNullException(nameof(logger));
         _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
         _uow = uow ?? throw new ArgumentNullException(nameof(uow));
         _cmain = new cMain(_configuration);



     }

     protected override async Task ExecuteAsync(CancellationToken stoppingToken)
     {
         while (!stoppingToken.IsCancellationRequested)
         {
             var breadCrumb = new BreadCrumb
             {
                 Application = Assembly.GetExecutingAssembly().GetName().Name,
                 DateTime = DateTime.Now,
                 Description = "Test",
                 EnquiryHeader_ID = 1,
                 EventName = "Test",
                 ReferenceID = 1,
                 Result = "Test"
             };

             //await _uow.BreadCrumbs.AddAsync(breadCrumb);
           

             if (_logger.IsEnabled(LogLevel.Information))
             {
                 _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
             }
             await Task.Delay(1000, stoppingToken);
         }
     }
 }

我得到的错误如下

抛出异常:Microsoft.Extensions.DependencyInjection.dll 中的“System.AggregateException”

部分服务无法构建

如果有此异常的处理程序,则程序可以安全地继续。

我真的很感激你指出我哪里出了问题或我遗漏了什么

我期望应用程序运行并使用工作类中的 IUnitOfWork。 当我删除 DI 时,它会运行

c# .net-8.0
1个回答
0
投票

问题在于该服务已注册为范围

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

它需要是后台工作者的单例。

© www.soinside.com 2019 - 2024. All rights reserved.