gpt4 book ai didi

c# - System.InvalidOperationException : 'Unable to resolve service for type ' Microsoft. AspNetCore.Hosting.IHostingEnvironment'

转载 作者:行者123 更新时间:2023-12-04 14:13:55 25 4
gpt4 key购买 nike

我有以下代码,它没有任何编译器警告或错误:(它是一个网络作业)

但是在这一行:builder.Build 我得到了这个异常(exception):

System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' while attempting to activate 'Microsoft.AspNetCore.Hosting.DefaultApplicationInsightsServiceConfigureOptions

代码如下:
 public class Program
{
private static IConfiguration Configuration { get; set; }
static async Task Main(string[] args)
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
b.AddServiceBus(sbOptions =>
{
sbOptions.ConnectionString = Configuration["AzureWebJobsServiceBus"];
});
});

builder.ConfigureServices((context, s) => { ConfigureServices(s); s.BuildServiceProvider(); });
builder.ConfigureLogging(logging =>
{
string appInsightsKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(appInsightsKey))
{
// This uses the options callback to explicitly set the instrumentation key.
logging.AddApplicationInsights(appInsightsKey)
.SetMinimumLevel(LogLevel.Information);
logging.AddApplicationInsightsWebJobs(o => { o.InstrumentationKey = appInsightsKey; });
}

});
var tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;
var host = builder.Build();
using (host)
{
await host.RunAsync(ct);
tokenSource.Dispose();
}
}

private static void ConfigureServices(IServiceCollection services)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables() //this doesnt do anything useful notice im setting some env variables explicitly.
.Build(); //build it so you can use those config variables down below.

Environment.SetEnvironmentVariable("QueueToIndexDocumentsFrom", Configuration["QueueToIndexDocumentsFrom"]);
Environment.SetEnvironmentVariable("SearchServiceName", Configuration["SearchServiceName"]);
Environment.SetEnvironmentVariable("SearchServideAdminApiKey", Configuration["SearchServideAdminApiKey"]);
Environment.SetEnvironmentVariable("SearchServiceIndexClientDocuments", Configuration["SearchServiceIndexClientDocuments"]);
Environment.SetEnvironmentVariable("SearchServiceIndexJobDocuments", Configuration["SearchServiceIndexJobDocuments"]);
Environment.SetEnvironmentVariable("SearchServiceIndexBillCycleDocuments", Configuration["SearchServiceIndexBillCycleDocuments"]);

#region RegisterServiceProviders
services.AddSingleton(Configuration);
services.AddScoped<Functions, Functions>();
services.AddScoped<IIndexer, Indexer>();
services.AddApplicationInsightsTelemetry();
#endregion

}
}

最佳答案

确保您的 configuration settings 中有一个用于检测 key 的 json 条目。 .

{
"AzureWebJobsStorage": "{storage connection string}",
"APPINSIGHTS_INSTRUMENTATIONKEY": "{instrumentation key}"
}

关于c# - System.InvalidOperationException : 'Unable to resolve service for type ' Microsoft. AspNetCore.Hosting.IHostingEnvironment',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62170166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com