Reputation: 39
I have a GameSettingOptions class like this:
public class GameSettingOptions
{
public int gameId { get; set; }
public string iconSize { get; set; }
public int sortOrder { get; set; }
public string[] chips { get; set; }
}
And I am trying to retrieve it from a service being set up like this:
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"));
}
This is my json object where game settings is an array:
{
"GameSettings": [
{
"gameId": 1,
"iconSize": "big",
"sortOrder": 6
},
{
"gameId": 2,
"iconSize": "small",
"sortOrder": 4
}
]
}
And this is how I am trying to retrieve it using this controller and dependency injection
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} ");
}
}
What I have tried:
services.Configure<List<GameSettingOptions>>(Configuration.GetSection("GameSettings"));
IOptions<List<GameSettingOptions>>
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; }
}
Upvotes: 0
Views: 1555
Reputation: 10849
Please define the GameSettingsOtions
as shown below
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; }
}
Now to configure it, use the below code ConfigureServices
method:
services.Configure<GameSettingOptions>(options => Configuration.GetSection("GameSettings").Bind(options));
with above you can see that defined options are injected. Here is the screenshot from my debugging:
This is my appsettings.json file for reference:
{
"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": []
}
]
}
Upvotes: 1