RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs
2023-04-17 08:49:26 +03:00

50 lines
1.6 KiB
C#

using System.Net;
#if !NET472
using System.Runtime.Versioning;
#endif
namespace RhSolutions.AddIn;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
public sealed class RhSolutionsAddIn : IExcelAddIn
{
public static Application Excel { get; private set; }
public static ServiceProvider ServiceProvider { get; private set; }
public static IAddInConfiguration Configuration { get; private set; }
public void AutoOpen()
{
IServiceCollection Services = new ServiceCollection();
Services.AddHttpClient()
.AddSingleton((Application)ExcelDnaUtil.Application)
.AddSingleton<IAddInConfiguration, RhAddInConfiguration>()
.AddSingleton<IDatabaseClient, RhDatabaseClient>()
.AddTransient<IFileDialog, ExcelFileDialog>()
.AddTransient<IExcelReader, RhExcelReader>();
Services.AddSingleton<WriterFactory>();
Services.AddTransient<RhExcelWriter>()
.AddTransient<IExcelWriter, RhExcelWriter>(s => s.GetService<RhExcelWriter>());
Services.AddTransient<RhDxfWriter>()
.AddTransient<IExcelWriter, RhDxfWriter>(s => s.GetService<RhDxfWriter>());
Services.AddSingleton<ToolFactory>();
ServiceProvider = Services.BuildServiceProvider();
Configuration = ServiceProvider.GetService<IAddInConfiguration>();
Excel = ServiceProvider.GetService<Application>();
EventsUtil.Initialize();
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12;
}
public void AutoClose()
{
EventsUtil.Uninitialize();
}
}