示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Configure custom options classes for Bot and LUIS configuration sections.
            services.Configure <GuiOptions>(Configuration.GetSection("GUI"));
            services.Configure <LUISOptions>(Configuration.GetSection("LUIS"));

            var botConfigPath = Configuration.GetValue <string>("Bot:FilePath");
            var botConfigKey  = Configuration.GetValue <string>("Bot:FileSecret");
            // if (!File.Exists(botConfigPath))
            // {
            //     throw new FileNotFoundException($"The .bot configuration file was not found. botConfigPath: '{botConfigPath}'");
            // }

            // Loads .bot configuration file.
            var botConfig = File.Exists(botConfigPath)
                ? BotConfiguration.Load(botConfigPath, botConfigKey)
                : new BotConfiguration();

            // Add a singleton that the Bot can access through dependency injection.
            services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot configuration file could not be loaded. botFilePath: {botConfigPath}"));

            // Initialize Bot Connected Services clients.
            var connectedServices = new BotServices(botConfig);

            services.AddSingleton(sp => connectedServices);

            services.AddBot <GameBot>(options =>
            {
                // Retrieve current endpoint.
                var service = botConfig.Services.FirstOrDefault(s => s.Type == "endpoint");
                if (service is EndpointService endpointService)
                {
                    options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);
                }

                options.Middleware.Add(new BotFileAssistantMiddleware());

                var translatorOptions = new TranslatorOptions();
                Configuration.GetSection("Translator").Bind(translatorOptions);

                // TODO Trial 2: Register middleware here.

                IStorage dataStore = new MemoryStorage();
                options.State.Add(new ConversationState(dataStore));
            });

            // Create and register state accessors.
            // Accessors created here are passed into the IBot-derived class on every turn.
            services.AddSingleton <GameBotAccessors>(sp =>
            {
                var options = sp.GetRequiredService <IOptions <BotFrameworkOptions> >().Value;
                if (options == null)
                {
                    throw new InvalidOperationException("BotFrameworkOptions must be configured prior to setting up the state accessors");
                }

                var conversationState = options.State.OfType <ConversationState>().FirstOrDefault();
                if (conversationState == null)
                {
                    throw new InvalidOperationException("ConversationState must be defined and added before adding conversation-scoped state accessors.");
                }

                return(new GameBotAccessors(conversationState)
                {
                    DialogStateAccessor = conversationState.CreateProperty <DialogState>(GameBotAccessors.DialogStateAccessorName),
                    StateFlagsAccessor = conversationState.CreateProperty <List <string> >(GameBotAccessors.StateFlagsAccessorName),
                    InventoryItemsAccessor = conversationState.CreateProperty <List <string> >(GameBotAccessors.InventoryItemsAccessorName),
                    RoomStateAccessor = conversationState.CreateProperty <Dictionary <string, RoomState> >(GameBotAccessors.RoomStateAccessorName)
                });
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
示例#2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Register the game catalog.
            services.AddSingleton(sp => new GameCatalog("Games"));

            // Configure custom options classes for Bot and LUIS configuration sections.
            services.Configure <GuiOptions>(Configuration.GetSection("GUI"));
            services.Configure <LUISOptions>(Configuration.GetSection("LUIS"));

            var botConfigKey  = Configuration.GetValue <string>("Bot:FileSecret");
            var botConfigPath = Configuration.GetValue <string>("Bot:FilePath");

            if (!File.Exists(botConfigPath))
            {
                throw new FileNotFoundException($"The .bot configuration file was not found. botConfigPath: '{botConfigPath}'");
            }

            // Loads .bot configuration file and adds a singleton that the Bot can access through dependency injection.
            var botConfig = BotConfiguration.Load(botConfigPath, botConfigKey);

            services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot configuration file could not be loaded. botFilePath: {botConfigPath}"));

            // Initialize Bot Connected Services clients.
            var connectedServices = new BotServices(botConfig);

            services.AddSingleton(sp => connectedServices);

            services.AddBot <GameBot>(options =>
            {
                // Retrieve current endpoint.
                var service = botConfig.Services.FirstOrDefault(s => s.Type == "endpoint" && s.Name == _environmentName);
                if (!(service is EndpointService endpointService))
                {
                    throw new InvalidOperationException($"The .bot file does not contain an endpoint with name '{_environmentName}'.");
                }

                options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

                IStorage dataStore = new MemoryStorage();
                options.State.Add(new ConversationState(dataStore));
            });

            // Create and register state accessors.
            // Accessors created here are passed into the IBot-derived class on every turn.
            services.AddSingleton <GameBotAccessors>(sp =>
            {
                var options = sp.GetRequiredService <IOptions <BotFrameworkOptions> >().Value;
                if (options == null)
                {
                    throw new InvalidOperationException("BotFrameworkOptions must be configured prior to setting up the state accessors");
                }

                var conversationState = options.State.OfType <ConversationState>().FirstOrDefault();
                if (conversationState == null)
                {
                    throw new InvalidOperationException("ConversationState must be defined and added before adding conversation-scoped state accessors.");
                }

                return(new GameBotAccessors(conversationState)
                {
                    DialogStateAccessor = conversationState.CreateProperty <DialogState>(GameBotAccessors.DialogStateAccessorName),
                    GameStateAccessor = conversationState.CreateProperty <GameState>(GameBotAccessors.GameStateAccessorName),
                });
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }