Botframework Microsoft.Bot.Connector错误:未注册服务提供程序实例
我按照上面的示例代码进行操作 当我向bot发送消息时,出现如下错误:Botframework Microsoft.Bot.Connector错误:未注册服务提供程序实例,botframework,asp.net-core-webapi,Botframework,Asp.net Core Webapi,我按照上面的示例代码进行操作 当我向bot发送消息时,出现如下错误: 未注册服务提供程序实例。请在使用ServiceProvider.Instance之前致电RegisterServiceProvider。 发生在 var-appCredentials=新的MicrosoftAppCredentials(此配置) csproj先生 Webapi-控制器 [HttpPost] [Route("Send")] public async Task<HttpResponseMessage>
未注册服务提供程序实例。请在使用ServiceProvider.Instance之前致电RegisterServiceProvider。
发生在
var-appCredentials=新的MicrosoftAppCredentials(此配置)代码>
csproj先生
Webapi-控制器
[HttpPost]
[Route("Send")]
public async Task<HttpResponseMessage> Send([FromBody]Activity activity)
{
//Error happens on this line!!!
var appCredentials = new MicrosoftAppCredentials(this._configuration);
var connector = new ConnectorClient(new Uri(activity.ServiceUrl), appCredentials);
var reply = activity.CreateReply();
if (activity.Type == ActivityTypes.Message)
{
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
}
else
{
//....
}
await connector.Conversations.ReplyToActivityAsync(reply);
var response = new HttpResponseMessage(HttpStatusCode.OK);
return response;
}
[HttpPost]
[路线(“发送”)]
公共异步任务发送([FromBody]活动)
{
//错误发生在这一行!!!
var appCredentials=新的MicrosoftAppCredentials(此配置);
var connector=new ConnectorClient(新Uri(activity.ServiceUrl)、appCredentials);
var reply=activity.CreateReply();
if(activity.Type==ActivityTypes.Message)
{
//算点东西让我们回去
int length=(activity.Text??string.Empty).length;
//将我们的回复返回给用户
reply=activity.CreateReply($“您发送的{activity.Text}为{length}个字符”);
}
其他的
{
//....
}
等待连接器.Conversations.ReplyToActivityAsync(reply);
var响应=新的HttpResponseMessage(HttpStatusCode.OK);
返回响应;
}
错误(使用模拟器测试)
信息
未注册服务提供程序实例。请在使用ServiceProvider.Instance之前调用RegisterServiceProvider
来源
Microsoft.Bot.Connector.Common
堆栈跟踪
位于Microsoft.Bot.Connector.ServiceProvider.ThrowOnNullInstance()处
在Microsoft.Bot.Connector.MicrosoftAppCredentials..ctor(IConfiguration configuration,ILogger logger)
在LotoBot.Webapi.Controllers.FbMsgController.d_u4.MoveNext()中
我被困了一晚,请帮帮我,谢谢你 只需在启动类的ConfigureServices方法上调用services.UseBotConnector()
。
此方法由Microsoft.Bot.Connector.BotServiceProviderExtensions扩展类提供。再尝试半天后,我发现这些代码可以使用。不知道这是否是错误。非常抱歉,这不是Microsoft.Bot.Connector.AspNetCore 3.6.0-alpha的错误,任何有此问题的人请查看@Luiz Carlos Faria和
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(_ => Configuration);
// Add framework services.
services.AddMvc(options =>
{
options.Filters.Add(typeof(TrustServiceUrlAttribute));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseStaticFiles();
app.UseBotAuthentication(new StaticCredentialProvider(
Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value));
app.UseMvc();
}
[HttpPost]
[Route("Send")]
public async Task<HttpResponseMessage> Send([FromBody]Activity activity)
{
//Error happens on this line!!!
var appCredentials = new MicrosoftAppCredentials(this._configuration);
var connector = new ConnectorClient(new Uri(activity.ServiceUrl), appCredentials);
var reply = activity.CreateReply();
if (activity.Type == ActivityTypes.Message)
{
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
}
else
{
//....
}
await connector.Conversations.ReplyToActivityAsync(reply);
var response = new HttpResponseMessage(HttpStatusCode.OK);
return response;
}