예제 #1
0
        // 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_2);

            Settings.AuthorizationKey = Configuration.GetSection("ApplicationSettings:AuthorizationKey")?.Value;

            // Add Application Insights services into service collection
            services.AddApplicationInsightsTelemetry();

            // Add the standard telemetry client
            services.AddSingleton <IBotTelemetryClient, BotTelemetryClient>();

            // Add ASP middleware to store the HTTP body, mapped with bot activity key, in the httpcontext.items
            // This will be picked by the TelemetryBotIdInitializer
            services.AddTransient <TelemetrySaveBodyASPMiddleware>();

            // Add telemetry initializer that will set the correlation context for all telemetry items
            services.AddSingleton <ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();

            // Add telemetry initializer that sets the user ID and session ID (in addition to other
            // bot-specific properties, such as activity ID)
            services.AddSingleton <ITelemetryInitializer, TelemetryBotIdInitializer>();

            // Create the telemetry middleware to track conversation events
            services.AddSingleton <IMiddleware, TelemetryLoggerMiddleware>();

            var apps = new List <LuisAppRegistration>();

            Configuration.GetSection("LuisAppRegistrations").Bind(apps);
            Settings.LuisAppRegistrations = apps;

            // Adding Consul hosted service
            using (ConsulService consulService = new ConsulService(EnvironmentName, ContentRootPath))
            {
                consulService.Initialize(services, Configuration);
            }

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,

                    ValidIssuer      = "https://BotApp.Luis.Router.Identity",
                    ValidAudience    = "https://BotApp.Luis.Router.Identity",
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Settings.AuthorizationKey))
                };
            });

            services.AddCors(o => o.AddPolicy("AllowAllPolicy", options =>
            {
                options.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));
        }
예제 #2
0
        // 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_2);

            Settings.AuthorizationKey        = Configuration.GetSection("ApplicationSettings:AuthorizationKey")?.Value;
            Settings.KeyVaultEncryptionKey   = Configuration.GetSection("ApplicationSettings:KeyVaultEncryptionKey")?.Value;
            Settings.KeyVaultApplicationCode = Configuration.GetSection("ApplicationSettings:KeyVaultApplicationCode")?.Value;

            // Add Application Insights services into service collection
            services.AddApplicationInsightsTelemetry();

            // Add the standard telemetry client
            services.AddSingleton <TelemetryClient, TelemetryClient>();

            // Adding KeyVault service
            services.AddSingleton <IKeyVaultService>(sp => { return(new KeyVaultService(EnvironmentName, ContentRootPath)); });

            KeyVaultService keyVaultService = new KeyVaultService(EnvironmentName, ContentRootPath);

            EncryptionKey   = keyVaultService.GetVaultKeyAsync(Settings.KeyVaultEncryptionKey).Result;
            ApplicationCode = keyVaultService.GetVaultKeyAsync(Settings.KeyVaultApplicationCode).Result;

            // Adding Consul hosted service
            using (ConsulService consulService = new ConsulService(EnvironmentName, ContentRootPath))
            {
                consulService.Initialize(services, Configuration);
            }

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,

                    ValidIssuer      = "https://BotApp.Luis.Router.Identity",
                    ValidAudience    = "https://BotApp.Luis.Router.Identity",
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Settings.AuthorizationKey))
                };
            });

            services.AddCors(o => o.AddPolicy("AllowAllPolicy", options =>
            {
                options.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));
        }
예제 #3
0
        public void InitializeTest()
        {
            // arrage
            var builder = new ConfigurationBuilder()
                          .SetBasePath(ContentRootPath)
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{EnvironmentName}.json", optional: true)
                          .AddEnvironmentVariables();

            var configuration = builder.Build();

            IServiceCollection services = new ServiceCollection();

            // act
            ConsulService consulService = new ConsulService(EnvironmentName, ContentRootPath);

            consulService.Initialize(services, configuration);

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            IConsulClient    service         = serviceProvider.GetRequiredService <IConsulClient>();

            // assert
            Assert.NotNull(service);
        }
예제 #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Reading settings
            Settings.MicrosoftAppId       = Configuration.GetSection("ApplicationSettings:MicrosoftAppId")?.Value;
            Settings.MicrosoftAppPassword = Configuration.GetSection("ApplicationSettings:MicrosoftAppPassword")?.Value;
            Settings.BotConversationStorageConnectionString = Configuration.GetSection("ApplicationSettings:BotConversationStorageConnectionString")?.Value;
            Settings.BotConversationStorageKey                    = Configuration.GetSection("ApplicationSettings:BotConversationStorageKey")?.Value;
            Settings.BotConversationStorageDatabaseId             = Configuration.GetSection("ApplicationSettings:BotConversationStorageDatabaseId")?.Value;
            Settings.BotConversationStorageUserCollection         = Configuration.GetSection("ApplicationSettings:BotConversationStorageUserCollection")?.Value;
            Settings.BotConversationStorageConversationCollection = Configuration.GetSection("ApplicationSettings:BotConversationStorageConversationCollection")?.Value;
            Settings.KeyVaultEncryptionKey   = Configuration.GetSection("ApplicationSettings:KeyVaultEncryptionKey")?.Value;
            Settings.KeyVaultApplicationCode = Configuration.GetSection("ApplicationSettings:KeyVaultApplicationCode")?.Value;

            // Adding CosmosDB user storage
            CosmosDbStorage userstorage = new CosmosDbStorage(new CosmosDbStorageOptions
            {
                AuthKey          = Settings.BotConversationStorageKey,
                CollectionId     = Settings.BotConversationStorageUserCollection,
                CosmosDBEndpoint = new Uri(Settings.BotConversationStorageConnectionString),
                DatabaseId       = Settings.BotConversationStorageDatabaseId,
            });

            var userState = new UserState(userstorage);

            services.AddSingleton(userState);

            // Adding CosmosDB conversation storage
            CosmosDbStorage conversationstorage = new CosmosDbStorage(new CosmosDbStorageOptions
            {
                AuthKey          = Settings.BotConversationStorageKey,
                CollectionId     = Settings.BotConversationStorageConversationCollection,
                CosmosDBEndpoint = new Uri(Settings.BotConversationStorageConnectionString),
                DatabaseId       = Settings.BotConversationStorageDatabaseId,
            });

            var conversationState = new ConversationState(conversationstorage);

            services.AddSingleton(conversationState);

            // Adding Consul hosted service
            using (ConsulService consulService = new ConsulService(EnvironmentName, ContentRootPath))
            {
                consulService.Initialize(services, Configuration);
            }

            // Adding MVC compatibility
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // Adding the credential provider to be used with the Bot Framework Adapter
            services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>();

            // Adding the channel provider to be used with the Bot Framework Adapter
            services.AddSingleton <IChannelProvider, ConfigurationChannelProvider>();

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

            // Adding middlewares
            services.AddSingleton(new AutoSaveStateMiddleware(userState, conversationState));
            services.AddSingleton(new ShowTypingMiddleware());

            // Adding telemetry
            ConfigureTelemetry(services);

            // Adding HttpClient and HttpClientHandler
            var handler = new HttpClientHandler();

            handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
            var httpClient = new HttpClient(handler);

            // Adding LUIS Router service
            services.AddSingleton <ILuisRouterService>(sp => { return(new LuisRouterService(httpClient, EnvironmentName, ContentRootPath, userState, sp.GetRequiredService <IBotTelemetryClient>())); });

            // Adding QnAMaker Router service
            services.AddSingleton <IQnAMakerService>(sp => { return(new QnAMakerService(httpClient, EnvironmentName, ContentRootPath, sp.GetRequiredService <IBotTelemetryClient>())); });

            // Adding WebChat service
            services.AddSingleton <IWebChatService>(sp => { return(new WebChatService(httpClient, EnvironmentName, ContentRootPath)); });

            // Adding KeyVault service
            services.AddSingleton <IKeyVaultService>(sp => { return(new KeyVaultService(EnvironmentName, ContentRootPath)); });

            KeyVaultService keyVaultService = new KeyVaultService(EnvironmentName, ContentRootPath);

            EncryptionKey   = keyVaultService.GetVaultKeyAsync(Settings.KeyVaultEncryptionKey).Result;
            ApplicationCode = keyVaultService.GetVaultKeyAsync(Settings.KeyVaultApplicationCode).Result;

            // Adding Active Directory service
            services.AddSingleton <IActiveDirectoryService>(sp => { return(new ActiveDirectoryService(EnvironmentName, ContentRootPath)); });

            // Adding accessor
            services.AddSingleton(sp =>
            {
                // We need to grab the conversationState we added on the options in the previous step
                var options = sp.GetRequiredService <IOptions <BotFrameworkOptions> >().Value;
                if (options == null)
                {
                    throw new InvalidOperationException("BotFrameworkOptions must be configured prior to setting up the State Accessors");
                }

                // Create the custom state accessor.
                // State accessor enable other components to read and write individual properties of state.
                var accessor = new BotAccessor(loggerFactory, conversationState, userState)
                {
                    ConversationDialogState   = conversationState.CreateProperty <DialogState>("DialogState"),
                    AskForExamplePreference   = conversationState.CreateProperty <bool>("AskForExamplePreference"),
                    IsAuthenticatedPreference = userState.CreateProperty <bool>("IsAuthenticatedPreference")
                };

                return(accessor);
            });

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