【问题标题】:Some services are not able to be constructed in .net core api某些服务无法在 .net core api 中构建
【发布时间】:2021-02-03 20:32:52
【问题描述】:

我正在使用 MongoDB 创建一个 .net 核心 API。

以下是我的 MongoDBSettings.cs

public class MongoDBSettings : IMongoDBSettings
    {
        public string DatabaseName { get; set; }
        public string CollectionName { get; set; }
        public string ConnectionString { get; set; }
        
    }

    public interface IMongoDBSettings
    {
        string DatabaseName { get; set; }
        string CollectionName { get; set; }
        string ConnectionString { get; set; }
        
    }

下面是appsettings.json文件

{
  "MongoDBSettings": {
    "CollectionName": Collname,
    "ConnectionString": connStr,
    "DatabaseName": Dbname
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

以下是我的服务类

public class InvoiceService
    {
        private readonly IMongoCollection<Invoice> _invoice;
        public InvoiceService(IMongoDBSettings settings)
        {
            var client = new MongoClient(settings.ConnectionString);
            var database = client.GetDatabase(settings.DatabaseName);

            _invoice = database.GetCollection<Invoice>(settings.CollectionName);
        }

        public List<Invoice> Get() =>
            _invoice.Find(invoice => true).ToList();
    }

以下是我的控制器类

[Route("api/[controller]")]
    [ApiController]
    public class InvoiceController : ControllerBase
    {
        private readonly InvoiceService _invoiceService;

        public InvoiceController(InvoiceService invoiceService)
        {
            _invoiceService = invoiceService;
        }

        [HttpGet]
        public ActionResult<List<Invoice>> Get() =>
            _invoiceService.Get();

    }

下面是我的startup.ConfigureServices

public void ConfigureServices(IServiceCollection services)
        {
            
            services.Configure<MongoDBSettings>(
                Configuration.GetSection(nameof(MongoDBSettings)));

            services.AddSingleton<MongoDBSettings>(sp =>
                sp.GetRequiredService<IOptions<MongoDBSettings>>().Value);
            services.AddSingleton<InvoiceService>();

            services.AddControllers();

        }

当我运行它时,我收到以下错误:

System.AggregateException: '验证服务时出错 描述符'ServiceType:IDP_API.Services.InvoiceService Lifetime: 单例实施类型:IDP_API.Services.InvoiceService' InvalidOperationException:无法解析类型的服务 尝试激活时出现“IDP_API.Models.IMongoDBSettings” 'IDP_API.Services.InvoiceService'。

我尝试改变

 services.AddSingleton<InvoiceService>();

services.AddTransient<InvoiceService>(); 

services.AddScoped<InvoiceService>();

但它们都不起作用。

有人可以帮忙吗?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-webapi


    【解决方案1】:

    IMongoDBSettings 未在服务集合中注册,因此当您尝试将 IMongoDBSettings 注入 InvoiceService

    public InvoiceService(IMongoDBSettings settings)
    

    你得到那个错误

    考虑改为执行以下操作

    services.AddSingleton<IMongoDBSettings>(sp =>
        sp.GetRequiredService<IOptions<MongoDBSettings>>().Value);
    

    注意在注册设置而不是实现时使用接口。

    还需要注意的是,"MongoDBSettings" 的部分名称在显示的appsettings.json 中没有出现,因此当前系统可能存在更多问题

    【讨论】:

    • 非常感谢你的工作就像一个魅力。是的,我在 appsettings.json 中添加了 MongoDBSettings
    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2021-12-12
    • 2021-07-07
    • 2023-02-03
    • 1970-01-01
    • 2018-11-20
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多