从配置文件中读取 Json 个对象的列表并 return 它在控制器中总是 return null
Reading a List of Json Objects from config file and return it in the controller always return null
我有这样的 GameSettingOptions class:
public class GameSettingOptions
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
我正在尝试从这样设置的服务中检索它:
public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("GameSettingOptions.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
});
services.Configure<GameSettingOptions>(Configuration.GetSection("GameSettings"));
}
这是我的 json 对象,其中游戏设置是一个数组:
{
"GameSettings": [
{
"gameId": 1,
"iconSize": "big",
"sortOrder": 6
},
{
"gameId": 2,
"iconSize": "small",
"sortOrder": 4
}
]
}
这就是我尝试使用此控制器和依赖项注入来检索它的方式
public class GameSettingsControllers : ControllerBase
{
private readonly GameSettingOptions Options;
public GameSettingsControllers(IOptionsSnapshot<GameSettingOptions> options)
{
Options = options.Value;
}
public object GetGameSettings()
{
return new JsonResult($"gameId: {Options.gameId}, " + $"iconSize: {Options.iconSize}, "
+ $"sortOrder: {Options.sortOrder}, " + $"chips: {Options.chips} ");
}
}
我试过的:
- 把服务和消费服务的属性都变成List,这个returns null
services.Configure<List<GameSettingOptions>>(Configuration.GetSection("GameSettings"));
IOptions<List<GameSettingOptions>>
- 创建另一个具有 GameSettingOptions 属性 集合的 class,然后我绑定并注入新的 class,这也是 returns null
public class GameSettings
{
public GameSettingsOptions[] AllGameSettings {get; set;}
}
public class GameSettingOptions
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
- 对于控制器,我尝试执行 IOptions 值的 foreach,但它说 GameSetingsOptions foes 不包含 public 实例或 'GetEnumerator'
的定义
请按如下所示定义GameSettingsOtions
public class GameSettingOptions: List<GameSetting>
{
}
public class GameSetting
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
现在配置它,使用下面的代码ConfigureServices
方法:
services.Configure<GameSettingOptions>(options => Configuration.GetSection("GameSettings").Bind(options));
在上面你可以看到定义的选项被注入了。这是我调试的截图:
这是我的appsettings.json文件供参考:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"MySettings": {
"SettingValue": 2
},
"GameSettings": [
{
"gameId": 1,
"iconSize": "big",
"sortOrder": 6
},
{
"gameId": 2,
"iconSize": "small",
"sortOrder": 4
},
{
"gameId": 3,
"iconSize": "medium",
"sortOrder": 2
},
{
"gameId": 4,
"iconSize": "small",
"sortOrder": 5
},
{
"gameId": 5,
"iconSize": "small",
"sortOrder": 8,
"chips": []
},
{
"gameId": 6,
"iconSize": "small",
"sortOrder": 7
},
{
"gameId": 7,
"iconSize": "big",
"sortOrder": 1,
"chips": []
}
]
}
我有这样的 GameSettingOptions class:
public class GameSettingOptions
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
我正在尝试从这样设置的服务中检索它:
public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("GameSettingOptions.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
});
services.Configure<GameSettingOptions>(Configuration.GetSection("GameSettings"));
}
这是我的 json 对象,其中游戏设置是一个数组:
{
"GameSettings": [
{
"gameId": 1,
"iconSize": "big",
"sortOrder": 6
},
{
"gameId": 2,
"iconSize": "small",
"sortOrder": 4
}
]
}
这就是我尝试使用此控制器和依赖项注入来检索它的方式
public class GameSettingsControllers : ControllerBase
{
private readonly GameSettingOptions Options;
public GameSettingsControllers(IOptionsSnapshot<GameSettingOptions> options)
{
Options = options.Value;
}
public object GetGameSettings()
{
return new JsonResult($"gameId: {Options.gameId}, " + $"iconSize: {Options.iconSize}, "
+ $"sortOrder: {Options.sortOrder}, " + $"chips: {Options.chips} ");
}
}
我试过的:
- 把服务和消费服务的属性都变成List,这个returns null
services.Configure<List<GameSettingOptions>>(Configuration.GetSection("GameSettings"));
IOptions<List<GameSettingOptions>>
- 创建另一个具有 GameSettingOptions 属性 集合的 class,然后我绑定并注入新的 class,这也是 returns null
public class GameSettings
{
public GameSettingsOptions[] AllGameSettings {get; set;}
}
public class GameSettingOptions
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
- 对于控制器,我尝试执行 IOptions 值的 foreach,但它说 GameSetingsOptions foes 不包含 public 实例或 'GetEnumerator' 的定义
请按如下所示定义GameSettingsOtions
public class GameSettingOptions: List<GameSetting>
{
}
public class GameSetting
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
现在配置它,使用下面的代码ConfigureServices
方法:
services.Configure<GameSettingOptions>(options => Configuration.GetSection("GameSettings").Bind(options));
在上面你可以看到定义的选项被注入了。这是我调试的截图:
这是我的appsettings.json文件供参考:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"MySettings": {
"SettingValue": 2
},
"GameSettings": [
{
"gameId": 1,
"iconSize": "big",
"sortOrder": 6
},
{
"gameId": 2,
"iconSize": "small",
"sortOrder": 4
},
{
"gameId": 3,
"iconSize": "medium",
"sortOrder": 2
},
{
"gameId": 4,
"iconSize": "small",
"sortOrder": 5
},
{
"gameId": 5,
"iconSize": "small",
"sortOrder": 8,
"chips": []
},
{
"gameId": 6,
"iconSize": "small",
"sortOrder": 7
},
{
"gameId": 7,
"iconSize": "big",
"sortOrder": 1,
"chips": []
}
]
}