From 5511d9b12a445ff452352fb5a7af61840fe76a66 Mon Sep 17 00:00:00 2001 From: sjuarezgx Date: Fri, 9 Sep 2022 09:53:50 -0300 Subject: [PATCH 1/5] Physical app Path for Azure serverless --- .../src/dotnetframework/GxClasses/Helpers/HttpHelper.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs index 109c871e6..3c76897e0 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Primitives; using System.Net.Http; using Microsoft.AspNetCore.Mvc.Formatters; +using System.Reflection; #else using System.ServiceModel.Web; using System.ServiceModel; @@ -80,6 +81,7 @@ public class HttpHelper const string GAM_CODE_OTP_USER_ACCESS_CODE_SENT = "400"; const string GAM_CODE_TFA_USER_MUST_VALIDATE = "410"; const string GAM_CODE_TOKEN_EXPIRED = "103"; + const string AZUREFLAGFILE = "azureflag.json"; static Regex CapitalsToTitle = new Regex(@"(?<=[A-Z])(?=[A-Z][a-z]) | (?<=[^A-Z])(?=[A-Z]) | (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); public static void SetResponseStatus(HttpContext httpContext, string statusCode, string statusDescription) @@ -270,6 +272,13 @@ public static bool GetHttpRequestPostedFile(IGxContext gxContext, string varName public static string RequestPhysicalApplicationPath(HttpContext context = null) { #if NETCORE + string contentRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + string azureDeployFlagFile = Path.Combine(contentRootPath, AZUREFLAGFILE); + + if (File.Exists(azureDeployFlagFile)) + { + return (contentRootPath); + } return Directory.GetParent(FileUtil.GetStartupDirectory()).FullName; #else if (context==null) From f2dffe4e721c3505a06f5a7815aaf1c548f4b90e Mon Sep 17 00:00:00 2001 From: sjuarezgx Date: Fri, 9 Sep 2022 10:03:28 -0300 Subject: [PATCH 2/5] minor change in const variable --- dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs index 3c76897e0..661044934 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs @@ -81,7 +81,6 @@ public class HttpHelper const string GAM_CODE_OTP_USER_ACCESS_CODE_SENT = "400"; const string GAM_CODE_TFA_USER_MUST_VALIDATE = "410"; const string GAM_CODE_TOKEN_EXPIRED = "103"; - const string AZUREFLAGFILE = "azureflag.json"; static Regex CapitalsToTitle = new Regex(@"(?<=[A-Z])(?=[A-Z][a-z]) | (?<=[^A-Z])(?=[A-Z]) | (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); public static void SetResponseStatus(HttpContext httpContext, string statusCode, string statusDescription) @@ -273,7 +272,7 @@ public static string RequestPhysicalApplicationPath(HttpContext context = null) { #if NETCORE string contentRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - string azureDeployFlagFile = Path.Combine(contentRootPath, AZUREFLAGFILE); + string azureDeployFlagFile = Path.Combine(contentRootPath, "azureflag.json"); if (File.Exists(azureDeployFlagFile)) { From 32545df352931a9fe6338f9cd188c89bf749619e Mon Sep 17 00:00:00 2001 From: sjuarezgx Date: Sun, 27 Nov 2022 13:38:12 -0300 Subject: [PATCH 3/5] Do some changes to avoid reading from file system --- .../GxClasses.Web/Middleware/GXRouting.cs | 15 +-------------- .../GxClasses.Web/Middleware/IGXRouting.cs | 1 - .../GxClasses/Core/GXApplication.cs | 8 +++++++- .../GxClasses/Helpers/HttpHelper.cs | 8 ++------ dotnet/src/extensions/Azure/Handlers/Program.cs | 14 ++++++++------ 5 files changed, 18 insertions(+), 28 deletions(-) diff --git a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs index ccaf7d06f..6518c2ad5 100644 --- a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs +++ b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs @@ -54,7 +54,7 @@ public GXRouting(string baseURL) { restBaseURL = baseURL; ServicesGroupSetting(); - ServicesFunctionsMetadata(); + AzureRuntime = GxContext.IsAzureContext; GetAzureDeploy(); } @@ -522,19 +522,6 @@ public void ServicesGroupSetting() throw; } } - - public void ServicesFunctionsMetadata() - { - //Used for Azure functions - - string functionMetadataFile = "functions.metadata"; - string metadataFilePath = Path.Combine(ContentRootPath, functionMetadataFile); - - if (File.Exists(metadataFilePath)) - { - AzureRuntime = true; - } - } public void GetAzureDeploy() { string azureDeployFlagFile = Path.Combine(ContentRootPath, "azureflag.json"); diff --git a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs index 667cc0b5f..98101f652 100644 --- a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs +++ b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs @@ -13,7 +13,6 @@ public interface IGXRouting public GxRestWrapper GetController(HttpContext context, ControllerInfo controllerInfo); public GxRestWrapper GetController(HttpContext context, string controller, string methodName, Dictionary variableAlias); public void ServicesGroupSetting(); - public void ServicesFunctionsMetadata(); } } diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs index f5ab562f3..797251861 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs @@ -2923,6 +2923,8 @@ public static bool IsRestService } #if NETCORE static bool _isHttpContext; + internal static bool IsAzureContext + { get; set; } #endif public static bool IsHttpContext { @@ -3581,7 +3583,7 @@ Hashtable Images string imgDir = ""; if (String.IsNullOrEmpty(dir) && _HttpContext == null) { - + GXLogging.Debug(log, "Searching for txt files .."); int srchIx = 0; string[] paths = new string[] { ".\\", "..\\" }; bool found = false; @@ -3597,9 +3599,13 @@ Hashtable Images } } imgDir = dir; + GXLogging.Debug(log, $"{imgDir} txt file found"); } else + { imageFiles = Directory.GetFiles(dir, "*.txt"); + GXLogging.Debug(log, $"{imageFiles} imageFiles found"); + } string KBPrefix = String.Empty; char[] densitySeparator = new char[] { '|' }; diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs index 661044934..f03671765 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs @@ -271,13 +271,9 @@ public static bool GetHttpRequestPostedFile(IGxContext gxContext, string varName public static string RequestPhysicalApplicationPath(HttpContext context = null) { #if NETCORE - string contentRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - string azureDeployFlagFile = Path.Combine(contentRootPath, "azureflag.json"); + if (GxContext.IsAzureContext) + return FileUtil.GetStartupDirectory(); - if (File.Exists(azureDeployFlagFile)) - { - return (contentRootPath); - } return Directory.GetParent(FileUtil.GetStartupDirectory()).FullName; #else if (context==null) diff --git a/dotnet/src/extensions/Azure/Handlers/Program.cs b/dotnet/src/extensions/Azure/Handlers/Program.cs index cbb058672..c0d2bf5e8 100644 --- a/dotnet/src/extensions/Azure/Handlers/Program.cs +++ b/dotnet/src/extensions/Azure/Handlers/Program.cs @@ -2,6 +2,7 @@ using System.Reflection; using System.Text.Json; using System.Threading.Tasks; +using GeneXus.Application; using GeneXus.Cache; using GeneXus.Deploy.AzureFunctions.Handlers.Helpers; using GeneXus.Services; @@ -18,9 +19,11 @@ public class Program static async Task Main() { + GxContext.IsAzureContext = true; + string roothPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - string routePrefix = GetRoutePrefix(); - GXRouting.ContentRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + string routePrefix = GetRoutePrefix(roothPath); + GXRouting.ContentRootPath = roothPath; var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() @@ -43,13 +46,12 @@ static async Task Main() services.AddSingleton(x => new InProcessCache()); }) .Build(); - - await host.RunAsync(); + + await host.RunAsync(); } - private static string GetRoutePrefix() + private static string GetRoutePrefix(string ContentRootPath) { //Read host.json file to get Route prefix - string ContentRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string hostFile = "host.json"; string hostFilePath = Path.Combine(ContentRootPath, hostFile); From 83d272634d1b56e3133d2a599f18719d4108f488 Mon Sep 17 00:00:00 2001 From: sjuarezgx Date: Sun, 27 Nov 2022 14:04:44 -0300 Subject: [PATCH 4/5] revert some changes --- .../GxClasses.Web/Middleware/GXRouting.cs | 14 +++++++++++++- .../GxClasses.Web/Middleware/IGXRouting.cs | 1 + dotnet/src/extensions/Azure/Handlers/Program.cs | 4 +--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs index 6518c2ad5..50438ef13 100644 --- a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs +++ b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs @@ -54,7 +54,7 @@ public GXRouting(string baseURL) { restBaseURL = baseURL; ServicesGroupSetting(); - AzureRuntime = GxContext.IsAzureContext; + ServicesFunctionsMetadata(); GetAzureDeploy(); } @@ -522,6 +522,18 @@ public void ServicesGroupSetting() throw; } } + public void ServicesFunctionsMetadata() + { + //Used for Azure functions + + string functionMetadataFile = "functions.metadata"; + string metadataFilePath = Path.Combine(ContentRootPath, functionMetadataFile); + + if (File.Exists(metadataFilePath)) + { + AzureRuntime = true; + } + } public void GetAzureDeploy() { string azureDeployFlagFile = Path.Combine(ContentRootPath, "azureflag.json"); diff --git a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs index 98101f652..667cc0b5f 100644 --- a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs +++ b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/IGXRouting.cs @@ -13,6 +13,7 @@ public interface IGXRouting public GxRestWrapper GetController(HttpContext context, ControllerInfo controllerInfo); public GxRestWrapper GetController(HttpContext context, string controller, string methodName, Dictionary variableAlias); public void ServicesGroupSetting(); + public void ServicesFunctionsMetadata(); } } diff --git a/dotnet/src/extensions/Azure/Handlers/Program.cs b/dotnet/src/extensions/Azure/Handlers/Program.cs index c0d2bf5e8..2a5079852 100644 --- a/dotnet/src/extensions/Azure/Handlers/Program.cs +++ b/dotnet/src/extensions/Azure/Handlers/Program.cs @@ -19,8 +19,6 @@ public class Program static async Task Main() { - GxContext.IsAzureContext = true; - string roothPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string routePrefix = GetRoutePrefix(roothPath); GXRouting.ContentRootPath = roothPath; @@ -46,7 +44,7 @@ static async Task Main() services.AddSingleton(x => new InProcessCache()); }) .Build(); - + GxContext.IsAzureContext = true; await host.RunAsync(); } private static string GetRoutePrefix(string ContentRootPath) From 6de4276ccd9547dc6795995039ea9e8df892ef07 Mon Sep 17 00:00:00 2001 From: sjuarezgx Date: Mon, 28 Nov 2022 11:51:48 -0300 Subject: [PATCH 5/5] fix log message --- dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs index 797251861..6b3221adf 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs @@ -3604,7 +3604,7 @@ Hashtable Images else { imageFiles = Directory.GetFiles(dir, "*.txt"); - GXLogging.Debug(log, $"{imageFiles} imageFiles found"); + GXLogging.Debug(log, "imageFiles found"); } string KBPrefix = String.Empty;