using System.Net; using ExcelDna.IntelliSense; using Grpc.Core; #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 static ProductSearch.ProductSearchClient GrpcClient { get; private set; } public void AutoOpen() { IServiceCollection Services = new ServiceCollection(); var grpcChannel = new Channel(@"rh2.cebotari.ru", ChannelCredentials.SecureSsl); GrpcClient = new ProductSearch.ProductSearchClient(grpcChannel); Services.AddHttpClient() .AddMemoryCache() .AddSingleton((Application)ExcelDnaUtil.Application) .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddTransient(); Services.AddSingleton(); Services.AddTransient() .AddTransient(s => s.GetService()); Services.AddTransient() .AddTransient(s => s.GetService()); Services.AddTransient() .AddTransient(s => s.GetService()); Services.AddSingleton(); Services.AddTransient() .AddTransient(s => s.GetService()); Services.AddTransient() .AddTransient(s => s.GetService()); Services.AddSingleton(); ServiceProvider = Services.BuildServiceProvider(); Configuration = ServiceProvider.GetService(); Excel = ServiceProvider.GetService(); EventsUtil.Initialize(); bool isTesting = Environment.GetEnvironmentVariable("ISTESTING") switch { "true" => true, "false" => false, _ => false }; if (!isTesting) { IntelliSenseServer.Install(); } ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; } public void AutoClose() { EventsUtil.Uninitialize(); bool isTesting = Environment.GetEnvironmentVariable("ISTESTING") switch { "true" => true, "false" => false, _ => false }; if (!isTesting) { IntelliSenseServer.Uninstall(); } } }