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

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;net6.0-windows</TargetFrameworks>
<TargetFrameworks>net472;net6.0-windows7.0</TargetFrameworks>
<LangVersion>10</LangVersion>
<OutputType>Library</OutputType>
<RootNamespace>RhSolutions.AddIn</RootNamespace>
@ -13,13 +13,18 @@
<StartupObject />
</PropertyGroup>
<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.IntelliSense" Version="1.6.0" />
<PackageReference Include="ExcelDna.Interop" Version="15.0.1" />
<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="RhSolutions.Sku" Version="0.1.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using RhSolutions.AddIn;
using RhSolutions.Models;
using System;
@ -25,7 +26,8 @@ namespace RhSolutions.Services
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
{