无法解析死去的简单配置依赖关系

发布于 2025-01-29 06:20:43 字数 1674 浏览 2 评论 0原文

我正在尝试在一个基于大餐的小型Web应用程序中设置依赖注入。

我当前的代码是:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

AddDeliveryStats(builder.Services, builder.Configuration);

// ...blazor template things...

void AddDeliveryStats(IServiceCollection services, ConfigurationManager config)
{
    services.Configure<BigQuerySettings>(config.GetSection("BigQuery"));
    services.AddTransient<IBigQueryClient, BigQueryClient>();
    // ...other stuff not pertinent to the error...
}

其中bigquerysettings作为

public class BigQuerySettings
{
    public string ProjectId { get; set; }
    public string DataSetId { get; set; }
    public string AuthFilePath { get; set; }
}

bigqueryclient具有以下构造函数:

public BigQueryClient(
    BigQuerySettings bigQuerySettings,
    ILogger<BigQueryClient> logger) { /* ... */ }

和我的appSettings.json.json包含以下内容:

{
  // ...
  "BigQuery": {
    "ProjectId": "<project-identifier>",
    "DataSetId": "",
    "AuthFilePath": "BigQueryAuthProd.json"
  }
}

如果此 :看起来很像一个教程示例,这是因为它基本上就是。它不起作用,这并不明显。我会收到以下错误:

无法构建某些服务 (在验证服务描述符时错误“ ServiceType:ibigqueryClient lifetime:Transient ImparinationType:BigQueryClient':在尝试激活'BigQueryClient'的同时,无法解析type'bigquerySettings'的服务。)

(在验证服务描述符时错误“ ServiceType:ibigqueryClient Lifetime:TramSient impasionationType : 适合自己的课程,我已经阅读了我能找到的每片文档(我无法理解的许多文档),并在错误消息中搜索了至少十个不同关键字的不同排列。没有什么真正指出我做错了什么。

I am trying to set up dependency injection in a small blazor-based web app.

My current code is:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

AddDeliveryStats(builder.Services, builder.Configuration);

// ...blazor template things...

void AddDeliveryStats(IServiceCollection services, ConfigurationManager config)
{
    services.Configure<BigQuerySettings>(config.GetSection("BigQuery"));
    services.AddTransient<IBigQueryClient, BigQueryClient>();
    // ...other stuff not pertinent to the error...
}

where BigQuerySettings is given as

public class BigQuerySettings
{
    public string ProjectId { get; set; }
    public string DataSetId { get; set; }
    public string AuthFilePath { get; set; }
}

and BigQueryClient has the following constructor:

public BigQueryClient(
    BigQuerySettings bigQuerySettings,
    ILogger<BigQueryClient> logger) { /* ... */ }

and my appsettings.json contains the following:

{
  // ...
  "BigQuery": {
    "ProjectId": "<project-identifier>",
    "DataSetId": "",
    "AuthFilePath": "BigQueryAuthProd.json"
  }
}

and if this looks pretty much like a tutorial example, that's because it basically is. It does not work and it is not obvious why. I get the following error:

Some services are not able to be constructed
(Error while validating the service descriptor 'ServiceType: IBigQueryClient Lifetime: Transient ImplementationType: BigQueryClient': Unable to resolve service for type 'BigQuerySettings' while attempting to activate 'BigQueryClient'.)

I have copied this code from online tutorial examples and adapted it as appropriate to my own classes, and I have read every piece of documentation I have been able to find (much of which I can't understand) and googled at least ten different permutations of the keywords in the error message. Nothing really points to what I am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-02-05 06:20:43

默认情况下,调用services.configure将只允许注入ioption&lt; bigquerysettings&gt;中的消费者。

If, however, you wish to inject BigQuerySettings directly into your consumer (which I would argue

BigQuerySettings settings =
    Configuration.GetSection("BigQuery").Get<BigQuerySettings>();

// TODO: Verify settings here (if required)

// Register BigQuerySettings as singleton in the container.
services.AddSingleton<BigQuerySettings>(settings);

这允许将bigquerysettings注入bigqueryclient

By default, a call to services.Configure will only allow injecting an IOption<BigQuerySettings> into your consumers.

If, however, you wish to inject BigQuerySettings directly into your consumer (which I would argue you should), you should do the following:

BigQuerySettings settings =
    Configuration.GetSection("BigQuery").Get<BigQuerySettings>();

// TODO: Verify settings here (if required)

// Register BigQuerySettings as singleton in the container.
services.AddSingleton<BigQuerySettings>(settings);

This allows BigQuerySettings to be injected into BigQueryClient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文