Skip to content

Commit

Permalink
Refactoring for Http sources
Browse files Browse the repository at this point in the history
(cherry picked from commit 18312f7)
  • Loading branch information
Sabrina Juarez Garcia committed Jan 25, 2025
1 parent 2bb0b4b commit 6377765
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 199 deletions.
10 changes: 9 additions & 1 deletion dotnet/DotNetStandardClasses.sln
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ai", "ai", "{F45B7F31-6F9B-
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GxAI", "src\dotnetcore\Providers\AI\GxAI.csproj", "{AA9CD80B-5FDC-4194-BA55-D77FC82915C3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AzureFunctionsAbstractions", "src\extensions\Azure\Abstractions\AzureFunctionsAbstractions.csproj", "{B3DC39F8-39F0-4200-A971-77E26FFDB2CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -681,6 +683,7 @@ Global
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Release|Any CPU.Build.0 = Release|Any CPU
<<<<<<< HEAD
{463C9F3B-ED87-43B0-A09A-E46B50BBEE6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{463C9F3B-ED87-43B0-A09A-E46B50BBEE6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{463C9F3B-ED87-43B0-A09A-E46B50BBEE6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -716,7 +719,11 @@ Global
{AA9CD80B-5FDC-4194-BA55-D77FC82915C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA9CD80B-5FDC-4194-BA55-D77FC82915C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA9CD80B-5FDC-4194-BA55-D77FC82915C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA9CD80B-5FDC-4194-BA55-D77FC82915C3}.Release|Any CPU.Build.0 = Release|Any CPU
{AA9CD80B-5FDC-4194-BA55-D77FC82915C3}.Release|Any CPU.Build.0 = Release|Any CPU
{B3DC39F8-39F0-4200-A971-77E26FFDB2CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3DC39F8-39F0-4200-A971-77E26FFDB2CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3DC39F8-39F0-4200-A971-77E26FFDB2CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3DC39F8-39F0-4200-A971-77E26FFDB2CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -863,6 +870,7 @@ Global
{E59B3248-4C26-4DB0-96CB-67437319E22B} = {41E1D031-799F-484F-85DE-7A30AF1A6FBA}
{F45B7F31-6F9B-4A8E-8B82-34DBDF375000} = {2261B65E-3757-4E5B-9DCD-EAE8D1E236A3}
{AA9CD80B-5FDC-4194-BA55-D77FC82915C3} = {F45B7F31-6F9B-4A8E-8B82-34DBDF375000}
{B3DC39F8-39F0-4200-A971-77E26FFDB2CA} = {BD804A75-9F3F-416C-BF6B-D3DF6C4A8DC0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E18684C9-7D76-45CD-BF24-E3944B7F174C}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.8.24" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\dotnetcore\GxClasses.Web\GxClasses.Web.csproj" />
<ProjectReference Include="..\..\..\dotnetcore\GxClasses\GxClasses.csproj" />
<ProjectReference Include="..\..\..\dotnetcore\Providers\Cache\GxRedis\GxRedis.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using GeneXus;
using GeneXus.Application;
using GeneXus.Cache;
using GeneXus.Deploy.AzureFunctions.HttpHandler;
using GeneXus.Utils;
using Microsoft.AspNetCore.Http;

namespace GxClasses.Web.Middleware
{
public class GXAzureRestService : GxRestService
{
static readonly IGXLogger log = GXLoggerFactory.GetLogger<GXAzureRestService>();
private readonly ICacheService2 _cacheService;
public GXAzureRestService(ICacheService2 redis) : base()
{
if (GxContext.IsAzureContext)
{
if (redis != null && redis.GetType() == typeof(Redis))
{
_cacheService = redis;
}
else
_cacheService = new InProcessCache();
}
else
{
GXLogging.Debug(log, "Error: Not an Azure context.");
throw new Exception("Operation Cancelled. Not an Azure context.");
}
}
public void SetServiceSession(HttpRequest request, HttpResponse response, HttpContext httpContext)
{
if (GxContext.IsAzureContext)
{
if ((context != null && context.HttpContext != null) && (Request != null && Response != null))
{
GXHttpAzureContext httpAzureContext = new GXHttpAzureContext(Request, Response, _cacheService);
if (httpAzureContext != null && httpAzureContext.Session != null && context != null && context.HttpContext != null)
context.HttpContext.Session = httpAzureContext.Session;
else
GXLogging.Debug(log, $"Error : Azure Serverless session could not be created.");
}
else
{
if (context != null)
{
context.HttpContext = httpContext;
GXHttpAzureContext httpAzureContext = new GXHttpAzureContext(request, response, _cacheService);
if (httpAzureContext != null && httpAzureContext.Session != null && context != null && context.HttpContext != null)
context.HttpContext.Session = httpAzureContext.Session;
else
GXLogging.Debug(log, $"Error : Azure Serverless session could not be created.");
}
}
}
else
{
GXLogging.Debug(log, "Error: Not an Azure context.");
throw new Exception("Operation Cancelled. Not an Azure context.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
Expand All @@ -23,15 +24,17 @@ public class GXHttpAzureContext
public GXHttpAzureContext( HttpRequest request, HttpResponse response, ICacheService2 redis)
{
bool isSecure = IsSecureConnection(request);
sessionId = request.Cookies[AzureSessionId];

if (request != null && request.Cookies != null && request.Cookies[AzureSessionId] != null)
sessionId = request.Cookies[AzureSessionId];

if (redis != null && redis.GetType() == typeof(Redis))
_redis = redis;

if (string.IsNullOrEmpty(sessionId))
if (string.IsNullOrEmpty(sessionId) && request != null)
CreateSessionId(isSecure, response, request);

if ((_redis != null) & (sessionId != null))
if ((_redis != null) && (sessionId != null))
session = new RedisHttpSession(_redis, sessionId);
else
session = new MockHttpSession();
Expand Down Expand Up @@ -93,7 +96,7 @@ private void CreateSessionId(bool isSecure, HttpResponse response, HttpRequest r
public class MockHttpSession : ISession
{
string _sessionId = Guid.NewGuid().ToString();
readonly Dictionary<string, object> _sessionStorage = new Dictionary<string, object>();
readonly ConcurrentDictionary<string, object> _sessionStorage = new ConcurrentDictionary<string, object>();
string ISession.Id => _sessionId;
bool ISession.IsAvailable => throw new NotImplementedException();
IEnumerable<string> ISession.Keys => _sessionStorage.Keys;
Expand All @@ -111,22 +114,32 @@ Task ISession.LoadAsync(CancellationToken cancellationToken)
}
void ISession.Remove(string key)
{
_sessionStorage.Remove(key);
_sessionStorage.TryRemove(key, out Object value);
}
void ISession.Set(string key, byte[] value)
{
_sessionStorage[key] = Encoding.UTF8.GetString(value);
}
bool ISession.TryGetValue(string key, out byte[] value)
{

if (_sessionStorage.ContainsKey(key) && _sessionStorage[key] != null)
{
value = Encoding.ASCII.GetBytes(_sessionStorage[key].ToString());
return true;
value = Array.Empty<byte>();
try
{
if (_sessionStorage != null && _sessionStorage.ContainsKey(key) && _sessionStorage[key] != null)
{
value = Encoding.ASCII.GetBytes(_sessionStorage[key].ToString());
return true;
}
else
{
value = Array.Empty<byte>();
return false;
}
}
value = null;
return false;
catch (Exception ex)
{
throw ex;
}
}
}
public class RedisHttpSession : ISession
Expand Down Expand Up @@ -211,7 +224,7 @@ public bool TryGetValue(string key, out byte[] value)
}
}
}
value = null;
value = Array.Empty<byte>();
return false;
}
public bool SessionKeyExists(string sessionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@

<ItemGroup Condition="'$(HttpSupport)' == 'true'">
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<Compile Include="HttpHandler\*" />
</ItemGroup>

<ItemGroup Condition="'$(EventGridSupport)' == 'true'">
Expand All @@ -72,8 +71,8 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.8.24" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup Condition="$(GenStdVersion)==''">
Expand Down

This file was deleted.

Loading

0 comments on commit 6377765

Please sign in to comment.