C# Bot仿真器框架无法解析服务

C# Bot仿真器框架无法解析服务,c#,azure,dependency-injection,botframework,azure-bot-service,C#,Azure,Dependency Injection,Botframework,Azure Bot Service,我遵循Azure EchoBot教程添加状态,在bot框架模拟器中运行bot时遇到了一个问题。 (机器人可以连接。EchoBot无需任何修改即可正常运行) 这是我在Startup.cs中添加到ConfigureServices函数的内容 public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVer

我遵循Azure EchoBot教程添加状态,在bot框架模拟器中运行bot时遇到了一个问题。 (机器人可以连接。EchoBot无需任何修改即可正常运行) 这是我在Startup.cs中添加到ConfigureServices函数的内容

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // Create the Bot Framework Adapter.
        services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

        //// Create the User state
        services.AddSingleton<UserState>();

        //// Create the Conversation State
        services.AddSingleton<ConversationState>();

        // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
        services.AddTransient<IBot, EchoBot>();
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//创建Bot框架适配器。
services.AddSingleton();
////创建用户状态
services.AddSingleton();
////创建对话状态
services.AddSingleton();
//将bot创建为瞬态。在这种情况下,ASP控制器需要一个IBot。
services.AddTransient();
}
这是我添加到EchoBot.cs的内容

private readonly BotState _userState;
    private readonly BotState _conversationState;

    public EchoBot (ConversationState conversationState, UserState userState)
    {
        _conversationState = conversationState;
        _userState = userState;
    }

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        var conversationStateAccessors = _conversationState.CreateProperty<ConversationFlow>(nameof(ConversationFlow));
        var flow = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationFlow());
        var userStateAccessors = _userState.CreateProperty<UserProfile>("User");
        var profile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());

        await _conversationState.SaveChangesAsync(turnContext);
        await _userState.SaveChangesAsync(turnContext);
        await turnContext.SendActivityAsync(MessageFactory.Text($"Echo: {turnContext.Activity.Text}"), cancellationToken);
    }
private readonly BotState\u userState;
私有只读BotState\u会话状态;
公共EchoBot(会话状态会话状态,用户状态用户状态)
{
_会话状态=会话状态;
_userState=userState;
}
受保护的重写异步任务OnMessageActivityAsync(ITurnContext turnContext,CancellationToken CancellationToken)
{
var conversationStateAccessors=_conversationState.CreateProperty(nameof(ConversationFlow));
var flow=await conversationStateAccessors.GetAsync(turnContext,()=>new ConversationFlow());
var userStateAccessors=\u userState.CreateProperty(“用户”);
var profile=await userStateAccessors.GetAsync(turnContext,()=>newuserprofile());
wait_conversationState.SaveChangesAsync(turnContext);
wait_userState.saveChangesSync(turnContext);
等待turnContext.SendActivityAsync(MessageFactory.Text($“Echo:{turnContext.Activity.Text}”)、cancellationToken;
}
我在emulator中遇到的错误是POST 500 directline.conversationUpdate,这是我从bot控制台收到的错误截图:

谢谢!如果需要任何澄清,我会尽力更新。

需要派生的,但根据显示的
ConfigureServices
显示,其中一个未注册到
IServiceCollection

这将在尝试解决异常消息所指示的
ConversationState
时导致异常

一个在线示例显示了以下内容

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    // Create the Bot Framework Adapter with error handling enabled.
    services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

    // Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
    services.AddSingleton<IStorage, MemoryStorage>();

    // Create the User state.
    services.AddSingleton<UserState>();

    // Create the Conversation state.
    services.AddSingleton<ConversationState>();

    // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
    services.AddTransient<IBot, StateManagementBot>();
}

参考

谢谢!我现在明白了:)@BrainMeme如果这解决了最初的问题
// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
services.AddSingleton<IStorage, MemoryStorage>();