Created
May 19, 2022 03:23
-
-
Save hugodahl/7471b761a804dd6120f371d7d75e316f to your computer and use it in GitHub Desktop.
Getting a list of registered services from DI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the thing that does the work, or the "!command" command | |
public class GetAllCommandsCommand : ICommandMbhToTwitch | |
{ | |
public GetAllCommandsCommand(IOptionsSnapshot<List<string>> commandNames) | |
{ | |
var commands = commandNames.Get("CommandNames"); | |
this.CommandNames = commands; | |
} | |
public IEnumerable<string> CommandNames { get; set; } | |
public string Name => "command"; | |
public void Handle(OnChatCommandReceivedArgs args) | |
{ | |
Console.WriteLine(this.CommandNames); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IHostBuilder CreateHostBuilder(string[] args) => { | |
// Some clever bits happen here.... | |
services.AddOptions<List<string>>("CommandNames").Configure<IServiceProvider>((str, svc) => | |
{ | |
var commandNames = svc.GetServices<ICommandMbhToTwitch>().Select(cmd => cmd.Name); | |
str.AddRange(commandNames); | |
}); | |
// Do some more clever stuff with services | |
// Register the command that gets all commands | |
services.AddScoped<GetAllCommandsCommand>(); | |
// Finish setup, registration, etc. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment