Add HttpClient DI Injection

This commit is contained in:
Sergey Chebotar 2023-03-27 07:12:04 +03:00
parent 2cb7cd0377
commit 6291d30027
3 changed files with 20 additions and 10 deletions

View File

@ -1,21 +1,24 @@
using ExcelDna.Integration; using ExcelDna.Integration;
using ExcelDna.IntelliSense; using ExcelDna.IntelliSense;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using RhSolutions.Services; using RhSolutions.Services;
using System.Net; using System.Net;
using System.Net.Http;
namespace RhSolutions.AddIn namespace RhSolutions.AddIn
{ {
class RhSolutionsAddIn : IExcelAddIn public class RhSolutionsAddIn : IExcelAddIn
{ {
public static Application Excel { get; private set; } public static Application Excel { get; private set; }
public static HttpClient HttpClient { get; private set; } public static ServiceProvider ServiceProvider { get; set; }
public void AutoOpen() public void AutoOpen()
{ {
IServiceCollection Services = new ServiceCollection();
Services.AddHttpClient();
ServiceProvider = Services.BuildServiceProvider();
Excel = (Application)ExcelDnaUtil.Application; Excel = (Application)ExcelDnaUtil.Application;
HttpClient = new HttpClient();
IntelliSenseServer.Install(); IntelliSenseServer.Install();
RegistryUtil.Initialize(); RegistryUtil.Initialize();
EventsUtil.Initialize(); EventsUtil.Initialize();
@ -29,7 +32,7 @@ namespace RhSolutions.AddIn
IntelliSenseServer.Uninstall(); IntelliSenseServer.Uninstall();
RegistryUtil.Uninitialize(); RegistryUtil.Uninitialize();
EventsUtil.Uninitialize(); EventsUtil.Uninitialize();
HttpClient.Dispose(); //HttpClient.Dispose();
} }
} }
} }

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net472;net6.0-windows</TargetFrameworks> <TargetFrameworks>net472;net6.0-windows7.0</TargetFrameworks>
<LangVersion>10</LangVersion> <LangVersion>10</LangVersion>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<RootNamespace>RhSolutions.AddIn</RootNamespace> <RootNamespace>RhSolutions.AddIn</RootNamespace>
@ -13,13 +13,18 @@
<StartupObject /> <StartupObject />
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ExcelDna.AddIn" Version="1.6.0" /> <PackageReference Include="ExcelDna.AddIn" Version="1.6.0">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="ExcelDna.Integration" Version="1.6.0" /> <PackageReference Include="ExcelDna.Integration" Version="1.6.0" />
<PackageReference Include="ExcelDna.IntelliSense" Version="1.6.0" /> <PackageReference Include="ExcelDna.IntelliSense" Version="1.6.0" />
<PackageReference Include="ExcelDna.Interop" Version="15.0.1" /> <PackageReference Include="ExcelDna.Interop" Version="15.0.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RhSolutions.Sku" Version="0.1.1" /> <PackageReference Include="RhSolutions.Sku" Version="0.1.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json; using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using RhSolutions.AddIn; using RhSolutions.AddIn;
using RhSolutions.Models; using RhSolutions.Models;
using System; using System;
@ -25,7 +26,8 @@ namespace RhSolutions.Services
request = @"https://rh.cebotari.ru/api/search?query=" + line; request = @"https://rh.cebotari.ru/api/search?query=" + line;
} }
var response = await RhSolutionsAddIn.HttpClient.GetAsync(request); var client = RhSolutionsAddIn.ServiceProvider.GetRequiredService<HttpClient>();
var response = await client.GetAsync(request);
try try
{ {