Add Application Singleton

This commit is contained in:
Sergey Chebotar 2023-03-28 07:33:13 +03:00
parent 15dd27fb68
commit 68512dba2b

View File

@ -8,7 +8,7 @@ using System.Net;
namespace RhSolutions.AddIn namespace RhSolutions.AddIn
{ {
public class RhSolutionsAddIn : IExcelAddIn public sealed class RhSolutionsAddIn : IExcelAddIn
{ {
public static Application Excel { get; private set; } public static Application Excel { get; private set; }
public static ServiceProvider ServiceProvider { get; set; } public static ServiceProvider ServiceProvider { get; set; }
@ -17,15 +17,16 @@ namespace RhSolutions.AddIn
public void AutoOpen() public void AutoOpen()
{ {
IServiceCollection Services = new ServiceCollection(); IServiceCollection Services = new ServiceCollection();
Excel = (Application)ExcelDnaUtil.Application;
Services.AddHttpClient() Services.AddHttpClient()
.AddSingleton<IDatabaseClient, RhDatabaseClient>() .AddSingleton<IDatabaseClient, RhDatabaseClient>()
.AddSingleton<IAddInConfiguration, AddInConfiguration>(); .AddSingleton<IAddInConfiguration, AddInConfiguration>()
.AddSingleton(ExcelDnaUtil.Application);
ServiceProvider = Services.BuildServiceProvider(); ServiceProvider = Services.BuildServiceProvider();
Configuration = ServiceProvider.GetService<IAddInConfiguration>(); Configuration = ServiceProvider.GetService<IAddInConfiguration>();
Excel = ServiceProvider.GetService<Application>();
IntelliSenseServer.Install(); IntelliSenseServer.Install();
EventsUtil.Initialize(); EventsUtil.Initialize();