System.ArgumentNullException:值不能为 null。(参数 'uriString') - 在程序中设置的值.cs在appsettings.json中加载参数

System.ArgumentNullException: Value cannot be null. (Parameter 'uriString') - in value set in program.cs loading parameter in appsettings.json

提问人:Gillian 提问时间:11/14/2023 最后编辑:Denis MichealGillian 更新时间:11/15/2023 访问量:37

问:

抱怨这里的空值

services.AddScoped(x => new GraphQLClient(hostContext.Configuration["myapp:GraphQL:InternalURL"]));

虽然我在我的 json 文件中设置了值,但我的文件:Program.cs

public class Program
{
     public static readonly string Namespace = typeof(Program).Namespace;
     public static readonly string AppName = Namespace;
     
     public static int Main(string[] args)
     {
         SelfLog.Enable(Console.Error);
         
         Log.Logger = new LoggerConfiguration()
             .MinimumLevel.Debug()
             .MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
             .Enrich.FromLogContext()
             .WriteTo.Console()
             .CreateLogger();

         var builder = new HostBuilder()
             .ConfigureAppConfiguration((hostingContext, config) =>
             {
                 config.SetBasePath(Directory.GetCurrentDirectory());
                 config.AddJsonFile("appsettings.json", optional:true);
                 config.AddEnvironmentVariables();

                 if (args != null)
                 {
                     config.AddCommandLine(args);
                 }
             })
             .ConfigureServices((hostContext, services) =>
             {
                 services.AddOptions();
                 services.Configure<FFFConfig>(hostContext.Configuration.GetSection("myapp:Device:Device" + Environment.GetEnvironmentVariable("myapp__Device__DeviceID")));
                 services.AddScoped(x => new GraphQLClient(hostContext.Configuration["myapp:GraphQL:InternalURL"]));
                 services.AddScoped<myappConsumer>();
                 services.AddHostedService<FFFFeedback>();
             })
             .UseSerilog();

         try
         {
             builder.Build().Run();
             return 0;
         }
         catch (Exception ex)
         {
             Log.Fatal(ex, "Host terminated unexpectedly");
             return 1;
         }
         finally
         {
             Log.CloseAndFlush();
         }
     }
 }


和我的,我正在传递参数的值appsettings.json

 "myapp": {
  "GraphQL": {
  "URL": "http://172.17.5.200:7000/v1/graphql",
  "InternalURL": "http://172.17.5.200:7000/v1/graphql"
}
}
C# asp.net

评论


答: 暂无答案