-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (50 loc) · 1.86 KB
/
Program.cs
File metadata and controls
54 lines (50 loc) · 1.86 KB
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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OfficeOpenXml;
using System.Windows.Forms;
using YandexConfertor_5;
internal static class Program
{
[STAThread]
private static void Main()
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
ApplicationConfiguration.Initialize();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(defaultValue: false);
Form1 mainForm = new Form1();
mainForm.ButtonClick += async delegate
{
await HandleButtonClick(mainForm);
};
Application.Run(mainForm);
}
private static async Task HandleButtonClick(Form1 mainForm)
{
try
{
string inputExcelPath = mainForm.InputExcelPath;
string outputExcelPath = mainForm.OutputExcelPath;
string yandexApiKey = mainForm.YandexApiKey;
using IHost host = CreateHostBuilder().Build();
using IServiceScope serviceScope = host.Services.CreateScope();
IServiceProvider serviceProvider = serviceScope.ServiceProvider;
ExcelService requiredService = serviceProvider.GetRequiredService<ExcelService>();
serviceProvider.GetRequiredService<YandexApiService>();
await requiredService.WriteExcel(inputExcelPath, outputExcelPath, yandexApiKey);
mainForm.AppendToRichTextBox("Program executed successfully.");
}
catch (Exception ex)
{
mainForm.AppendToRichTextBox("! " + ex.Message);
}
}
private static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder().ConfigureServices(delegate (IServiceCollection services)
{
services.AddScoped<ExcelService>();
services.AddScoped<YandexApiService>();
});
}
}