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

View File

@ -1,53 +1,18 @@
using ExcelDna.Integration; using System;
using System.Runtime.Caching; using ExcelDna.Integration;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
namespace Rehau.Sku.Assist namespace Rehau.Sku.Assist
{ {
public class Functions : IExcelAddIn public class Functions
{ {
private static HttpClient _httpClient; private static HttpClient httpClient = new HttpClient();
private static ObjectCache _resultCache = MemoryCache.Default;
public void AutoClose()
{
}
public void AutoOpen()
{
_httpClient = new HttpClient();
}
[ExcelFunction] [ExcelFunction]
public static object RAUNAME(string request) public static async Task<string> RAUNAME(string request)
{ {
string cachedResult = _resultCache[request] as string; throw new NotImplementedException();
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();
}
}
} }
} }
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?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"> <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: The RuntimeVersion attribute above allows only the following setting:

View File

@ -14,6 +14,7 @@
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<NuGetPackageImportStamp> <NuGetPackageImportStamp>
</NuGetPackageImportStamp> </NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@ -33,42 +34,22 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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"> <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> <HintPath>packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </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" />
<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" />
<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.Core" />
<Reference Include="System.Data.OracleClient" /> <Reference Include="System.Data.OracleClient" />
<Reference Include="System.Net" /> <Reference Include="System.Net" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Caching" /> <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" />
<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.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.Transactions" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
@ -79,6 +60,7 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AddIn.cs" />
<Compile Include="Assistant\IProduct.cs" /> <Compile Include="Assistant\IProduct.cs" />
<Compile Include="Assistant\Product.cs" /> <Compile Include="Assistant\Product.cs" />
<Compile Include="Functions.cs" /> <Compile Include="Functions.cs" />
@ -86,6 +68,7 @@
<Compile Include="Assistant\SkuAssist.cs" /> <Compile Include="Assistant\SkuAssist.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" />
<None Include="Rehau.Sku.Assist-AddIn.dna" /> <None Include="Rehau.Sku.Assist-AddIn.dna" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Properties\ExcelDna.Build.props" /> <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"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AngleSharp" version="0.16.1" targetFramework="net48" /> <package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net480" />
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net48" /> <package id="ExcelDna.Integration" version="1.5.0" targetFramework="net480" />
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net48" /> <package id="ExcelDna.Registration" version="1.5.0" targetFramework="net480" />
<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" />
</packages> </packages>