Search code examples
c#asp.net-web-apibotframeworkautofacmicrosoft-teams

How to inject 'TeamSpecificConversationState', 'DropNonTeamsActivitiesMiddleware' and 'TeamsMiddleware' into Bot by autofac under .NET Framework


I'm a newbie about MS Bot development. I try to develop a MS Teams bot by Web API and autofac under .NET Framework. As MS sample code, the bot can work as a common bot. Now I want to only use it in Teams activity and conversations but person stuation. As Preview Release of .Net Teams Bot Builder v4 SDK, TeamSpecificConversationState, DropNonTeamsActivitiesMiddleware, DropChatActivitiesMiddleware and TeamsMiddleware could be used in my development case. But I cannot use autofac and Bot Frameworkv4 well. Below is my code:

    //setting Bot data store policy to use last write win
    //example if bot service got restarted, existing conversation would just overwrite data to store
    public static class BotConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var builder = new ContainerBuilder();
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            // The ConfigurationCredentialProvider will retrieve the MicrosoftAppId and
            // MicrosoftAppPassword from Web.config
            builder.RegisterType<ConfigurationCredentialProvider>().As<ICredentialProvider>().SingleInstance();

            // Create the Bot Framework Adapter with error handling enabled.
            builder.RegisterType<AdapterWithErrorHandler>().As<IBotFrameworkHttpAdapter>().SingleInstance();


            // The Memory Storage used here is for local bot debugging only. When the bot
            // is restarted, everything stored in memory will be gone.
            IStorage dataStore = new MemoryStorage();

            //// Create Conversation State object.
            //// The Conversation State object is where we persist anything at the conversation-scope.
            //TeamSpecificConversationState conversationState = new TeamSpecificConversationState(dataStore);
            //builder.RegisterInstance(conversationState).As<TeamSpecificConversationState>().SingleInstance();
            var conversationState = new ConversationState(dataStore);

            //// Drop all activites not received from Microsoft Teams channel.
            //builder.RegisterType<DropNonTeamsActivitiesMiddleware>().As<IMiddleware>().SingleInstance();

            //// For TeamContext object, Add Teams Middleware
            //builder.Register(c => new TeamsMiddleware(
            //   new ConfigurationCredentialProvider())).As<IMiddleware>().SingleInstance();

            //// Automatically drop all non Team messages.
            //builder.RegisterType<DropChatActivitiesMiddleware>().As<IMiddleware>().SingleInstance();

            builder.RegisterInstance(conversationState).As<ConversationState>().SingleInstance();


            // Register the NextLabsBot as the IBot interface
            builder.RegisterType<myBot>().As<IBot>();

            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            config.DependencyResolver = resolver;
        }
    }

the code uncommented can work as the common bot, it comes from MS.

the code commented by '//' is my attempt but it cannot work.

the code commented by '////' is SDK usages and some notes on my thought.

What can be done to complete it. Thanks!


Solution

  • Don't register bot and inject middleware via AddBot. You should inject your own IBot (which you are already doing) and add middleware through the adapter.

    Here is a correct example.

    It's dotnet core however as long as you are using the V4 SDK the pattern should be the same. The example from the official github seems outdated.

    For more info, see my discussion with them on github.