Advance - koreanpanda345/Discord_Net_Example GitHub Wiki

OH, your back? or are you just too cool for the basics. I would never get my answer cause I am just a guy who hides behind a computer, and write boring wiki tutorials.

anyways lets start then

Advance

Services

What are services? Services are kind of like javascript export function. Services are globally across all of your project, and described as a background process. This can benefit you later on when you start making a command manager which has its own page. so how do we make a service. First thing we need is we need the Dependency of Microsoft.Extensions.DependencyInjectionmake sure to use the using keyword.

next we need to a variable that is going to be a IServiceProvider, lets call it service. should look like this

private IServiceProvider service;

next we need to make a method that will build the services for us:

private ServiceProvider SetupService()
{
    return new ServiceCollection()
        .AddSingleton(client)
        .BuildServiceProvider();
}

now we have the method, lets break it down to understand it better.

What is a Singleton? a singleton is a class that only allows one instance of itself. in our case its only allowed to create a single instance of client. this client will be shared throughout the program.

next lets make the service variable to equal our new method. in the MainAsync method type in:

service = SetupService();

this will be handy later on, especially when you make a command manager and dealing with a ton of dependencies.