我正在尝试遵循Microsofts documentation如何将LUIS添加到我的机器人中
因此我创建了一个BotServices
类,并在Startup.cs
中将其添加到我的服务集合中
var connectedServices = new BotServices(botConfig);
services.AddSingleton<BotServices>(sp => connectedServices);
然后将其注入我的MyBot
类
public MyBot(BotServices botServices)
但是我得到这个错误
System.InvalidOperationException: Unable to resolve service for type 'BotServices' while attempting to activate 'MyBot'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
答案 0 :(得分:0)
在实例化对象时,您尝试执行以下操作:
var connectedServices = new BotServices(botConfig);
services.AddSingleton<BotServices>(connectedServices);
即不需要您现在拥有的工厂lambda(它正在获取指向您实例的本地变量...可能会导致问题...
答案 1 :(得分:0)
我的代码存在问题,就是我在BotServices
调用中注册了services.AddBot<IBot>
无效:
services.AddBot<MyBot>(options =>
{
var botConfig = BotConfiguration.Load(botFilePath ?? @".\MyBot.bot", secretKey);
services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})"));
var connectedServices = new BotServices(botConfig);
services.AddSingleton<BotServices>(sp => connectedServices);
}
之所以这样,是因为在使用空的bot模板创建时默认在其中定义了'botConfig'变量,因此我将其移出并可以使用! (但我仍然无法解释为什么它不起作用)
工程
var botConfig = BotConfiguration.Load(botFilePath ?? @".\MyBot.bot", secretKey);
services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})"));
var connectedServices = new BotServices(botConfig);
services.AddSingleton<BotServices>(sp => connectedServices);
services.AddBot<MyBot>(options =>
{
//all the bot configuration code
}