Add IDatabaseClient DI

This commit is contained in:
Sergey Chebotar 2023-03-27 13:55:58 +03:00
parent a3094fe275
commit 19007ff43e
6 changed files with 42 additions and 10 deletions

View File

@ -1,5 +1,6 @@
using ExcelDna.Integration;
using RhSolutions.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel;
using RhSolutions.Services;
using System.Linq;
@ -11,10 +12,11 @@ namespace RhSolutions.AddIn
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{
object result;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
result = ExcelAsyncUtil.Run("Database request", line, delegate
{
return RhDatabaseClient.GetProduct(line).GetAwaiter().GetResult();
return databaseClient.GetProduct(line).GetAwaiter().GetResult();
});
string parsedSku = Sku.TryParse(line, out var skus)
@ -49,7 +51,7 @@ namespace RhSolutions.AddIn
[ExcelFunction]
public static void _ResetStatusBar()
{
{
RhSolutionsAddIn.Excel.StatusBar = false;
}
}

View File

@ -15,10 +15,13 @@ namespace RhSolutions.AddIn
public void AutoOpen()
{
IServiceCollection Services = new ServiceCollection();
Services.AddHttpClient();
Excel = (Application)ExcelDnaUtil.Application;
Services.AddHttpClient()
.AddSingleton<IDatabaseClient, RhDatabaseClient>();
ServiceProvider = Services.BuildServiceProvider();
Excel = (Application)ExcelDnaUtil.Application;
IntelliSenseServer.Install();
RegistryUtil.Initialize();
EventsUtil.Initialize();
@ -32,7 +35,6 @@ namespace RhSolutions.AddIn
IntelliSenseServer.Uninstall();
RegistryUtil.Uninitialize();
EventsUtil.Uninitialize();
//HttpClient.Dispose();
}
}
}

View File

@ -3,7 +3,6 @@ using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn;
using RhSolutions.Services;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

View File

@ -12,6 +12,18 @@
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net472|AnyCPU'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net472|AnyCPU'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-windows7.0|AnyCPU'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-windows7.0|AnyCPU'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExcelDna.AddIn" Version="1.6.0">
<TreatAsUsed>true</TreatAsUsed>

View File

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace RhSolutions.Services
{
public interface IDatabaseClient
{
public Task<object> GetProduct(string line);
}
}

View File

@ -10,9 +10,16 @@ using System.Threading.Tasks;
namespace RhSolutions.Services
{
public static class RhDatabaseClient
public class RhDatabaseClient : IDatabaseClient
{
public static async Task<object> GetProduct(string line)
private IServiceProvider provider;
public RhDatabaseClient(IServiceProvider provider)
{
this.provider = provider;
}
public async Task<object> GetProduct(string line)
{
string request;
@ -26,7 +33,7 @@ namespace RhSolutions.Services
request = @"https://rh.cebotari.ru/api/search?query=" + line;
}
var client = RhSolutionsAddIn.ServiceProvider.GetRequiredService<HttpClient>();
var client = provider.GetRequiredService<HttpClient>();
var response = await client.GetAsync(request);
try
@ -45,6 +52,7 @@ namespace RhSolutions.Services
return $"{product.ProductSku} {product.Name}";
}
}
catch
{
return $"Ошибка сервера {response.StatusCode}";