Skip to content

Do Command modules support Scoped services? #2174

Answered by BobVul
defunky asked this question in Q&A
Discussion options

You must be logged in to vote

You need to:

  1. Create a service scope when handling a command
  2. Pass in the new scope when executing the command
  3. Dispose the scope after command execution is complete

In the following example I assume you're using the CommandHandler from the documentation.

  1. You should first make sure you have correct post-execution handling for RunMode.Async commands, as described in this documentation.

  2. Create a new SocketCommandContext subclass that implements IDisposable, like so:

    public class ScopedSocketCommandContext : SocketCommandContext, IDisposable
    {
        private bool disposedValue;
        public IServiceScope ServiceScope { get; }
    
        public ScopedSocketCommandContext(DiscordSocketClient client, S…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by csmir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants