I have built a Discord bot app with C# and I'm trying to run it using dotnet app.dll
, but I instantly encounter an error:
Microsoft.Extensions.Options.OptionsValidationException: A validation error has occurred.
This issue does not happen if I use the dotnet run
command from the project directory. The bot itself is running on a Ubuntu VPS.
Here is how I register the options to services:
IHost host = Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureDiscordHost((context, config) => {
// ...
})
.UseInteractionService((context, config) =>
{
// ...
})
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(context.HostingEnvironment.ContentRootPath);
config.AddJsonFile("words_list.json");
})
.ConfigureServices((context, services) =>
{
// ...
services.Configure<BotOptions>(options => context.Configuration.GetSection("BotOptions").Bind(options));
services.AddOptions<WordSearchSettings>().Bind(context.Configuration);
services.Configure<HostOptions>(
opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(15));
})
.Build();
await host.RunAsync(); // Error occurs here.
And the WordSearchSettings class itself:
public struct WordSearchDifficulty
{
public int StartTimestamp { get; set; }
public int EndTimestamp { get; set; }
public int WordsInGrid { get; set; }
public int GridSize { get; set; }
public string Message { get; set; }
public List<string> Words { get; set; }
}
public class WordSearchSettings
{
public Dictionary<string, WordSearchDifficulty> Difficulties { get; set; }
}
I think the issue lies with WordSearchSettings, since I have created a very similar bot before without adding that class.
I have tried using ConfigureAndValidate<TOptions>()
method from this package, but it did not fix the issue. There is also almost no information on this error on Google.