Search code examples
botframework

ITurnContext Service collection missing in Microsoft bot builder 4.2.2


I'm trying to add some middleware code to a bot I'm creating with microsoft bot framework v 4.2.2.

I can see in previous versions of builder, you're able to add services to the service collection within the 'Services' property of ITurnContext, however it no longer is present, and I can't find any documentation to detail the removal.

public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next)
{
    // some middleware method in bot builder 4.0
    context.Services.Add(SOME_KEY, someTService);
    await next();
}

I'd like to be doing the same thing with my bot, so if anyone knows where this might have got to I'd much appreciate the info!

Thanks

Nick


Solution

  • It has been moved to a property called TurnState;

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.turncontext.turnstate

    So the code would look like the following;

    public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next,  CancellationToken cancellationToken = new CancellationToken())
    {
        // some middleware method in bot builder 4.2.2
        context.TurnState.Add(SOME_KEY, someTService);
        await next();
    }