C
C#2mo ago
hutonahill

✅ BuildServiceProvider does not exist

private static IServiceProvider _services;
...
IServiceCollection temp = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
_services = temp.BuildServiceProvider();
private static IServiceProvider _services;
...
IServiceCollection temp = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
_services = temp.BuildServiceProvider();
everyone i can find online says this is how to get an instance of IServiceProvider, but it does not work. i found one person with my issue, but they had the wrong useing declaration.
7 Replies
Angius
Angius2mo ago
IServiceCollection is a... collection of services What would "building" it entail?
hutonahill
hutonahill2mo ago
i am not realy sure. i dont actualy know what this does, i am trying to hack together a discord bot and this is part of what chatGPT spit out. uses include:
private static async Task RegisterCommandsAsync() {
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly assembly in assemblies) {
await _commands.AddModulesAsync(assembly, _services);
}
}
private static async Task RegisterCommandsAsync() {
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly assembly in assemblies) {
await _commands.AddModulesAsync(assembly, _services);
}
}
and
var result = await _commands.ExecuteAsync(context, argPos, _services);
var result = await _commands.ExecuteAsync(context, argPos, _services);
looking into alternitive data types for these params... it doesent like ServiceCollection... and i am lost after that
Angius
Angius2mo ago
I recommend checking the documentation of whatever bot library it is you're using, rather than relying on an LLM to provide accurate information
MutableString
MutableString2mo ago
generally you would inject IServiceProvider from a constructor don't call BuildServiceProvider, there should be an instance already (at least that you know what you are doing) ideally you would want to inject the service you need instead of the IServiceProvider itself, anyway
hutonahill
hutonahill2mo ago
i did eventualy figure it out. still not sure what i did, but i rebuilt it like 3 times based on a few articles and the docs and the last one worked
MutableString
MutableString2mo ago
that's a, uhm, empirical approach, maybe? but ok
hutonahill
hutonahill2mo ago
yea everyone sets it up differently and no one comments their code properly >:( or even explanes why they do what they do very annoying no one writes code for people who like learning by building a project. but i guess thats a me problem
Want results from more Discord servers?
Add your server