异常:尝试激活时无法解析服务类型

问题描述 投票:0回答:0

 'Unable to resolve service for type 'Victoria.LavaNode
1[TheSwarmManager.Modules.XLavaPlayer]',同时尝试激活'TheSwarmManager.Services.AudioHandler'。'`

所以,我正在使用 C#、Discord.NET 和 Victoria 库创建一个 discord 机器人来播放音乐。我的服务正在这里配置:

private ServiceProvider ConfigureServices()
{
    return new ServiceCollection()
        .AddSingleton<DiscordSocketClient>()
        .AddSingleton(x => new DiscordSocketClient(new DiscordSocketConfig
        {
            GatewayIntents = Discord.GatewayIntents.All,
            AlwaysDownloadUsers = true,
        }))

        .AddSingleton<InteractionService>()
        .AddSingleton<InteractionHandler>()
        .AddSingleton<CommandService>()
        .AddSingleton<PrefixHandler>()

        .AddLavaNode()
        .AddSingleton(new LavaConfig())
        .AddSingleton<AudioHandler>()

        .AddSingleton(_config)

        .BuildServiceProvider();
}

导致异常的行是这样的:

_services = ConfigureServices();
_audioService = _services.GetRequiredService<AudioHandler>(); // <- this one

这是 XLavaPlayer.cs 文件:

using Discord;
using Victoria;

namespace TheSwarmManager.Modules {
    public class XLavaPlayer : LavaPlayer {
        public string ChannelName { get; }
        public XLavaPlayer(LavaSocket lavaSocket, IVoiceChannel voiceChannel, ITextChannel textChannel)
            : base(lavaSocket, voiceChannel, textChannel) {
            ChannelName = textChannel.Name;
        }
    }
}

我试着用 AddScope 做点什么,但我有点新,所以我没有再进一步。

c# service discord.net
© www.soinside.com 2019 - 2024. All rights reserved.