Microsoft.Bot.Connector error : service provider instance was not register
Microsoft.Bot.Connector error : service provider instance was not register
我遵循了 Microsoft/BotBuilder
上的示例代码
当我向机器人发送消息时,出现了这样的错误消息:
The service provider instance was not register. Please call RegisterServiceProvider before using ServiceProvider.Instance.
发生在
var appCredentials = new MicrosoftAppCredentials(this._configuration);
.csproj
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Bot.Connector.AspNetCore" Version="3.6.0-alpha" />
<PackageReference Include="Microsoft.Bot.Connector.AspNetCore.Mvc" Version="3.6.0-alpha" />
<PackageReference Include="Microsoft.Bot.Connector.Common" Version="3.6.0-alpha" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
</ItemGroup>
代码
appsettings.json
{ "MicrosoftAppId": "xxxxxxxxx", "MicrosoftAppPassword": "xxxxxxxxxxxxxxxxxx" }
Startup.cs
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();
}
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;
}
错误(使用模拟器测试)
- 留言
The service provider instance was not register. Please call RegisterServiceProvider before using ServiceProvider.Instance.
- 来源
Microsoft.Bot.Connector.Common
- 堆栈跟踪
at Microsoft.Bot.Connector.ServiceProvider.ThrowOnNullInstance()
at Microsoft.Bot.Connector.MicrosoftAppCredentials..ctor(IConfiguration configuration, ILogger logger)
at LotoBot.Webapi.Controllers.FbMsgController.d__4.MoveNext()
卡了一晚上,求大神指教,万分感谢!
只需在 Startup class 的 ConfigureServices 方法上调用 services.UseBotConnector()
。
此方法由 Microsoft.Bot.Connector.BotServiceProviderExtensions 扩展 class.
提供
我遵循了 Microsoft/BotBuilder
上的示例代码当我向机器人发送消息时,出现了这样的错误消息:
The service provider instance was not register. Please call RegisterServiceProvider before using ServiceProvider.Instance.
发生在
var appCredentials = new MicrosoftAppCredentials(this._configuration);
.csproj
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Bot.Connector.AspNetCore" Version="3.6.0-alpha" />
<PackageReference Include="Microsoft.Bot.Connector.AspNetCore.Mvc" Version="3.6.0-alpha" />
<PackageReference Include="Microsoft.Bot.Connector.Common" Version="3.6.0-alpha" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
</ItemGroup>
代码
appsettings.json
{ "MicrosoftAppId": "xxxxxxxxx", "MicrosoftAppPassword": "xxxxxxxxxxxxxxxxxx" }
Startup.cs
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(); }
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; }
错误(使用模拟器测试)
- 留言
The service provider instance was not register. Please call RegisterServiceProvider before using ServiceProvider.Instance.
- 来源
Microsoft.Bot.Connector.Common
- 堆栈跟踪
at Microsoft.Bot.Connector.ServiceProvider.ThrowOnNullInstance() at Microsoft.Bot.Connector.MicrosoftAppCredentials..ctor(IConfiguration configuration, ILogger logger) at LotoBot.Webapi.Controllers.FbMsgController.d__4.MoveNext()
卡了一晚上,求大神指教,万分感谢!
只需在 Startup class 的 ConfigureServices 方法上调用 services.UseBotConnector()
。
此方法由 Microsoft.Bot.Connector.BotServiceProviderExtensions 扩展 class.