Move geofeed web logic to a seperate project as core lib as asp.net core project is causing weird issues
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
70
AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs
Normal file
70
AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Text;
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.WebLogic
|
||||
{
|
||||
public class GeoFeedReturn
|
||||
{
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
private readonly IGeoFeedProvider provider;
|
||||
private readonly ILogger<GeoFeedReturn> logger;
|
||||
private readonly IGeoFeedPersistentCacheProvider cacheProvider;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
|
||||
public GeoFeedReturn(IGeoFeedProvider provider,
|
||||
ILogger<GeoFeedReturn> logger,
|
||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment)
|
||||
{
|
||||
this.provider = provider;
|
||||
this.logger = logger;
|
||||
this.cacheProvider = cacheProvider;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public async Task<IResult> GetGeoFeed()
|
||||
{
|
||||
bool isCached = true;
|
||||
try
|
||||
{
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||
{
|
||||
isCached = false;
|
||||
feed = await provider.GetGeoFeedDataAsync();
|
||||
if (environment.IsProduction())
|
||||
{
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
}
|
||||
}
|
||||
|
||||
return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)),
|
||||
"text/csv",
|
||||
"geofeed.csv");
|
||||
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||
string geoFeedData = cacheProvider.GetGeoFeed();
|
||||
|
||||
return Results.File(Encoding.UTF8.GetBytes(geoFeedData),
|
||||
"text/csv",
|
||||
"geofeed.csv");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError($"Error: {ex}");
|
||||
}
|
||||
|
||||
return Results.NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json
Normal file
12
AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"AS1024.GeoFeed.Core.WebLogic": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:54455;http://localhost:54456"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user