-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathProgram.cs
77 lines (60 loc) · 2.41 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using NorthwindInventory;
using NorthwindInventory.Bots;
using NorthwindInventory.DbSetup;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
var builder = WebApplication.CreateBuilder(args);
// Load configuration from appsettings.json
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
// Register configuration in the service collection
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
// Retrieve the storage connection string from the configuration
var storageConnectionString = builder.Configuration["StorageConnectionString"];
if (string.IsNullOrEmpty(storageConnectionString))
{
throw new ArgumentNullException(nameof(storageConnectionString), "Storage connection string cannot be null or empty.");
}
// Set up services
builder.Services.AddControllers();
builder.Services.AddHttpClient("WebClient", client => client.Timeout = TimeSpan.FromSeconds(600));
builder.Services.AddHttpContextAccessor();
// Configure Bot Framework Authentication
builder.Services.AddSingleton(new ConfigurationBotFrameworkAuthentication(builder.Configuration));
builder.Services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>();
// Create the Bot Framework Adapter with error handling enabled.
builder.Services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
builder.Services.AddTransient<IBot, SearchBot>();
// Register the AzureTableSetup service
builder.Services.AddSingleton(sp => new AzureTableSetup(storageConnectionString));
// Build the application
var app = builder.Build();
// Run the SetupTablesAndDataAsync method on startup
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var azureTableSetup = services.GetRequiredService<AzureTableSetup>();
await azureTableSetup.SetupTablesAndDataAsync();
}
catch (Exception ex)
{
// Handle exceptions
Console.WriteLine($"An error occurred during table setup: {ex.Message}");
}
}
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();