【问题标题】:System.InvalidOperationException error reason?System.InvalidOperationException 错误原因?
【发布时间】:2022-02-27 20:10:50
【问题描述】:

我在 .Net6 中使用 BigBlueButtonAPI.NET 包。

在我的 appsetting 中,我声明了这些变量:

\"BigBlueButtonAPISettings\": {
\"ServerAPIUrl\": \"https://myserver.com/bigbluebutton/api/\",
\"SharedSecret\": \"my shared secret here\"
}

在我的 program.cs 我写了这些代码:

builder.Services.Configure<BigBlueButtonAPISettings> 
(builder.Configuration.GetSection(\"BigBlueButtonAPISettings\"));
builder.Services.AddScoped<BigBlueButtonAPIClient>(provider =>
  {
    var settings = provider.GetRequiredService<IOptions<BigBlueButtonAPISettings>>().Value;
    var factory = provider.GetRequiredService<IHttpClientFactory>();
    return new BigBlueButtonAPIClient(settings, factory.CreateClient());
  });

当我想在我的应用程序中调用 API 时,在 program.cs 中我看到了这个错误:

  • 异常消息已经告诉您:没有注册类型IHttpClientFactory 的服务。你认为你在哪里注册?
  • (我怀疑你只需要打电话给builder.Services.AddHttpClient()。)
  • 对我来说,错误消息是自我解释的,您需要注册所需的服务
  • 请:read the manual。软件开发需要您自己的研究。
  • @JHBonarius 在提问之前我进行了搜索。

标签: c# asp.net .net-6.0 bigbluebutton


【解决方案1】:

这是答案。也许它对其他人也有帮助。我在代码前添加了builder.Services.AddHttpClient();

builder.Services.AddHttpClient();

builder.Services.Configure<BigBlueButtonAPISettings> 
(builder.Configuration.GetSection("BigBlueButtonAPISettings"));
builder.Services.AddScoped<BigBlueButtonAPIClient>(provider =>
{
var settings =provider.GetRequiredService<IOptions<BigBlueButtonAPISettings>>().Value;
var factory = provider.GetRequiredService<IHttpClientFactory>();
return new BigBlueButtonAPIClient(settings, 
factory.CreateClient());
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 2016-02-26
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多