Async/await ready solution init commit

This commit is contained in:
Sergey Chebotar 2021-11-29 15:50:24 +03:00
parent 0fe8e038af
commit 3eb118d6db
7 changed files with 91 additions and 109 deletions

24
AddIn.cs Normal file
View File

@ -0,0 +1,24 @@
using ExcelDna.Integration;
using ExcelDna.Registration;
namespace Rehau.Sku.Assist
{
public class AddIn : IExcelAddIn
{
public void AutoOpen()
{
RegisterFunctions();
}
public void AutoClose()
{
}
void RegisterFunctions()
{
ExcelRegistration.GetExcelFunctions()
.ProcessAsyncRegistrations(nativeAsyncIfAvailable: false)
.RegisterFunctions();
}
}
}

View File

@ -1,38 +1,41 @@
using AngleSharp;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
//using System;
//using AngleSharp;
//using AngleSharp.Dom;
//using System.Linq;
//using System.Net;
//using System.Net.Http;
//using System.Threading.Tasks;
namespace Rehau.Sku.Assist
{
static class SkuAssist
{
public async static Task<AngleSharp.Dom.IDocument> GetDocumentAsync(string request, HttpClient httpClient)
{
string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request;
//namespace Rehau.Sku.Assist
//{
// static class SkuAssist
// {
// public async static Task<string> GetContent(string request, HttpClient httpClient)
// {
// string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request;
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpResponseMessage response = await httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
// return await httpClient.GetStringAsync(uri);
// }
IConfiguration config = Configuration.Default;
IBrowsingContext context = BrowsingContext.New(config);
// public async static Task<AngleSharp.Dom.IDocument> GetDocument(string source)
// {
// IConfiguration config = Configuration.Default;
// IBrowsingContext context = BrowsingContext.New(config);
string source = await response.Content.ReadAsStringAsync();
return await context.OpenAsync(req => req.Content(source));
}
// return await context.OpenAsync(req => req.Content(source));
// }
public static IProduct GetProductFromDocument(AngleSharp.Dom.IDocument document)
{
return document
.All
.Where(e => e.ClassName == "product-item__desc-top")
.Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
// .Where(product => !product.Sku.Any(c => char.IsLetter(c)))
.FirstOrDefault();
}
}
}
// public static IProduct GetProductFromDocument(IDocument document)
// {
// return document
// .All
// .Where(e => e.ClassName == "product-item__desc-top")
// .Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
// // .Where(product => !product.Sku.Any(c => char.IsLetter(c)))
// .FirstOrDefault();
// }
// }
//}

View File

@ -1,53 +1,18 @@
using ExcelDna.Integration;
using System.Runtime.Caching;
using System;
using ExcelDna.Integration;
using System.Net.Http;
using System.Threading.Tasks;
namespace Rehau.Sku.Assist
{
public class Functions : IExcelAddIn
public class Functions
{
private static HttpClient _httpClient;
private static ObjectCache _resultCache = MemoryCache.Default;
public void AutoClose()
{
}
public void AutoOpen()
{
_httpClient = new HttpClient();
}
private static HttpClient httpClient = new HttpClient();
[ExcelFunction]
public static object RAUNAME(string request)
public static async Task<string> RAUNAME(string request)
{
string cachedResult = _resultCache[request] as string;
if (cachedResult != null)
{
return cachedResult;
}
else
{
object result = ExcelAsyncUtil.Run("RAUNAME", null,
delegate
{
var document = SkuAssist.GetDocumentAsync(request, _httpClient).Result;
var product = SkuAssist.GetProductFromDocument(document);
return product.ToString();
});
if (result.Equals(ExcelError.ExcelErrorNA))
{
return "Загрузка...";
}
else
{
_resultCache.Add(request, result, System.DateTime.Now.AddMinutes(20));
return result.ToString();
}
}
throw new NotImplementedException();
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<DnaLibrary Name="Rehau.Sku.Assist Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2020/07/dnalibrary">
<ExternalLibrary Path="Rehau.Sku.Assist.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" IncludePdb="false" />
<ExternalLibrary Path="Rehau.Sku.Assist.dll" ExplicitRegistration="true" LoadFromBytes="true" Pack="true" IncludePdb="false" />
<!--
The RuntimeVersion attribute above allows only the following setting:

View File

@ -14,6 +14,7 @@
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -33,42 +34,22 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AngleSharp, Version=0.16.1.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea, processorArchitecture=MSIL">
<HintPath>packages\AngleSharp.0.16.1\lib\net472\AngleSharp.dll</HintPath>
</Reference>
<Reference Include="ExcelDna.Integration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL">
<HintPath>packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ExcelDna.Registration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL">
<HintPath>packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.ConfigurationManager, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Configuration.ConfigurationManager.6.0.0\lib\net461\System.Configuration.ConfigurationManager.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Data.OracleClient" />
<Reference Include="System.Net" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Security" />
<Reference Include="System.Security.AccessControl, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
</Reference>
<Reference Include="System.Security.Permissions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Permissions.6.0.0\lib\net461\System.Security.Permissions.dll</HintPath>
</Reference>
<Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
<Reference Include="System.Transactions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@ -79,6 +60,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AddIn.cs" />
<Compile Include="Assistant\IProduct.cs" />
<Compile Include="Assistant\Product.cs" />
<Compile Include="Functions.cs" />
@ -86,6 +68,7 @@
<Compile Include="Assistant\SkuAssist.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Rehau.Sku.Assist-AddIn.dna" />
<None Include="packages.config" />
<None Include="Properties\ExcelDna.Build.props" />

15
app.config Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>

View File

@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AngleSharp" version="0.16.1" targetFramework="net48" />
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net48" />
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Configuration.ConfigurationManager" version="6.0.0" targetFramework="net48" />
<package id="System.Runtime.Caching" version="6.0.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net48" />
<package id="System.Security.AccessControl" version="6.0.0" targetFramework="net48" />
<package id="System.Security.Permissions" version="6.0.0" targetFramework="net48" />
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
<package id="System.Text.Encoding.CodePages" version="5.0.0" targetFramework="net48" />
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net480" />
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net480" />
<package id="ExcelDna.Registration" version="1.5.0" targetFramework="net480" />
</packages>