无法解析死去的简单配置依赖关系
我正在尝试在一个基于大餐的小型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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,调用
services.configure
将只允许注入ioption&lt; bigquerysettings&gt;
中的消费者。If, however, you wish to inject
BigQuerySettings
directly into your consumer (which I would argue这允许将
bigquerysettings
注入bigqueryclient
。By default, a call to
services.Configure
will only allow injecting anIOption<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:This allows
BigQuerySettings
to be injected intoBigQueryClient
.