RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs

85 lines
2.7 KiB
C#
Raw Normal View History

2023-04-06 21:38:01 +03:00
using System.Net;
2023-05-17 07:40:26 +03:00
using ExcelDna.IntelliSense;
using Microsoft.Office.Interop.Excel;
2023-04-06 08:29:39 +03:00
#if !NET472
using System.Runtime.Versioning;
#endif
2021-12-24 16:22:03 +03:00
2023-03-28 10:03:19 +03:00
namespace RhSolutions.AddIn;
2023-04-06 08:29:39 +03:00
#if !NET472
[SupportedOSPlatform("windows")]
#endif
2023-03-28 10:03:19 +03:00
public sealed class RhSolutionsAddIn : IExcelAddIn
2021-11-29 15:50:24 +03:00
{
2023-03-28 10:03:19 +03:00
public static Application Excel { get; private set; }
public static ServiceProvider ServiceProvider { get; private set; }
public static IAddInConfiguration Configuration { get; private set; }
2023-03-28 10:03:19 +03:00
public void AutoOpen()
{
IServiceCollection Services = new ServiceCollection();
Services.AddHttpClient()
2023-05-16 07:43:57 +03:00
.AddMemoryCache()
2023-04-06 08:29:39 +03:00
.AddSingleton((Application)ExcelDnaUtil.Application)
2023-04-20 09:37:07 +03:00
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
.AddSingleton<IDatabaseClient, DatabaseClient>()
.AddSingleton<ICurrencyClient, CurrencyClient>()
2023-06-21 07:11:31 +03:00
.AddSingleton<ISleevesCalculator, SleevesCalculator>()
2023-05-22 07:39:48 +03:00
.AddTransient<IFileDialog, FileDialog>();
2023-04-17 08:49:26 +03:00
Services.AddSingleton<WriterFactory>();
2023-06-20 07:25:44 +03:00
Services.AddTransient<NewPriceWriter>()
.AddTransient<IWriter, NewPriceWriter>(s => s.GetService<NewPriceWriter>());
2023-04-20 09:37:07 +03:00
Services.AddTransient<DxfWriter>()
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
2023-06-20 09:46:52 +03:00
Services.AddTransient<CurrentPriceWriter>()
.AddTransient<IWriter, CurrentPriceWriter>(s => s.GetService<CurrentPriceWriter>());
2023-03-28 10:03:19 +03:00
2023-05-22 07:39:48 +03:00
Services.AddSingleton<ReaderFactory>();
Services.AddTransient<ExcelReader>()
.AddTransient<IReader, ExcelReader>(s => s.GetService<ExcelReader>());
2023-05-23 07:07:16 +03:00
Services.AddTransient<GuessReader>()
.AddTransient<IReader, GuessReader>(s => s.GetService<GuessReader>());
2023-05-22 07:39:48 +03:00
2023-04-14 08:08:46 +03:00
Services.AddSingleton<ToolFactory>();
2023-03-28 10:03:19 +03:00
ServiceProvider = Services.BuildServiceProvider();
Configuration = ServiceProvider.GetService<IAddInConfiguration>();
Excel = ServiceProvider.GetService<Application>();
EventsUtil.Initialize();
2023-05-17 08:03:55 +03:00
bool isTesting = Environment.GetEnvironmentVariable("ISTESTING") switch
2023-05-17 07:40:26 +03:00
{
"true" => true,
"false" => false,
_ => false
};
if (!isTesting)
{
IntelliSenseServer.Install();
}
2023-03-28 10:03:19 +03:00
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12;
}
public void AutoClose()
2021-11-29 15:50:24 +03:00
{
2023-05-17 08:03:55 +03:00
EventsUtil.Uninitialize();
bool isTesting = Environment.GetEnvironmentVariable("ISTESTING") switch
2023-05-17 07:40:26 +03:00
{
"true" => true,
"false" => false,
_ => false
};
if (!isTesting)
{
IntelliSenseServer.Uninstall();
}
2021-11-29 15:50:24 +03:00
}
}