#null at SlashCommandsExtension.CreateInstance(Type t, IServiceProvider services)

1 messages · Page 1 of 1 (latest)

lapis osprey
#

Hello.

I had a working bot, with working slash commands.
I just switched my test database from a simple json to a proper database in postgresql with EF Core.
That was (I think) the only thing I changed, and it was only a bit structural in some functions.
This is working like it should. No issue.

Now, when I try to invoke a slash command, I end up with this error :

   at DSharpPlus.SlashCommands.SlashCommandsExtension.CreateInstance(Type t, IServiceProvider services)
at DSharpPlus.SlashCommands.SlashCommandsExtension.RunCommandAsync(BaseContext context, MethodInfo method, IEnumerable`1 args)```

But nothing changed on this side, and it was working like a charm before.
I still have the same registration method, simple and straightforward.
 ```cs
var slash = discord.UseSlashCommands(); // (discord being my DiscordClient)
slash.RegisterCommands<UserCommands>();
slash.RegisterCommands<TimerCommands>();```

Now, no slash commands are working anymore.

Where could this error come from ? I'm searching since hours, up to no avail.
velvet thunder
#

How do you inject your databasecontext?

lapis osprey
#

        services.AddScoped(_ => _interactivity);
        services.AddScoped(_ => database);```
velvet thunder
#

theres the problem

lapis osprey
#

Oh ? Please tell me more. I'm willing to learn how it is a problem and how to fix it.

velvet thunder
#

you cant inject a scoped Service. try using a DbContextFactory which is a singlton

candid lodge
#

(or make your command scoped too)

lapis osprey
#

I was already injecting the scoped interactivity since a long time, tho

#

It never errored anything

#

Ok, now that I have a base, I'll try to work with that and find a way 🙂
Thx !

#

Ok. Just for reference, I changed my injection (and removed the OnConfiguring in the actual dbContext) to this :

services.AddDbContextFactory<GridContext>(options =>
    options.UseSqlServer(Environment.GetEnvironmentVariable("sqltoken")), ServiceLifetime.Singleton);```
And I still get the same error, no matter if I specify a lifetime or not (tried just to be sure).

Continuing to dive in. I find that interesting 🙂
velvet thunder
#

Can you show us the command source code?

lapis osprey
#

The command one ? yes

#
public partial class UserCommands : ApplicationCommandModule
{
        public GridContext database;

    [SlashCommand("scanproduit", "Fournit des informations nutritionnelles d'un produit grâce à son code barre.")]
    public async Task ProductInrfoTask(InteractionContext ctx, [Option("codebarre", "codebarre")] long codebarre)
    {
        var flag = new UserFlag();
        await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);

...```

That's the relevant part you want to see, I guess.
#

I can remove UserFlag, it doesn't matter here.

#

So I'm basically just injecting the database as a service, and calling it in the module, to have it available all along the code.

#

As long as I call a prefix command, I don't have any issue, and never had.

Next to the slash commands, I still kept two commands as normal prefix ones

var commands = discord.UseCommandsNext(new CommandsNextConfiguration()

commands.RegisterCommands<AdminCommands>();```

And this one works fine with the dbContext injected as a scoped like I was doing.
#

Only slash commands don't register anymore.

#

And actually, even when I don't inject any DbContext into my services, it still doesn't work, so I guess it's not directly linked in the end.

#

SlashCommands just don't register anymore.

#

Ok.
Now I changed

var slash = discord.UseSlashCommands();```
into
```cs
var slash = discord.UseSlashCommands(new SlashCommandsConfiguration() { });```
And now it does register them again.
But now, it bugs at the first line of the command, throwing a not found 404 error because database is null in the command.
```cs
 await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);```

And it actually works if I just define 
database = new();
Just before where it bugs.
Which is not what I want, but...

But I'm going forward 🙂
velvet thunder
#

you have to pass your ServiceProvider in the SlashConfig