将数组作为命令行参数传递给Asp.Net Core

需要将数组作为命令行参数传递给Asp.Net Core托管服务。

添加的提供商

config
  .AddJsonFile("appsettings.json")
  .AddCommandLine(args);

我在应用程序中的某个地方

var actions = _configuration.GetSection("actions").Get<List<string>>();
foreach (var action in actions)
{
   Console.WriteLine(action);
}

尝试运行类似的应用

dotnet MyService.dll --actions action1,action2
dotnet MyService.dll --actions [action1,action2]
dotnet MyService.dll --actions ["action1","action2"]

但没有结果,actionsnull

当我将"actions": ["action1","action2"]添加到a​​ppsettings.json时,绑定工作良好。

如何将数组作为命令行参数传递?

我可以通过_configuration.Getvalue<string>("actions").Split(",");的方式获得它,但是如何将其绑定到列表?

ergasfgsafg 回答:将数组作为命令行参数传递给Asp.Net Core

ASP.NET Core配置是一组键值对。您显示的appsettings.json属性将转换为以下内容:

  • actions:0 = Action1
  • actions:1 = Action2

为多个命令行参数提供相同的一组键值对,以获得所需的结果:

dotnet MyService.dll --actions:0 Action1 --actions:1 Action2
本文链接:https://www.f2er.com/3049731.html

大家都在问