Skip to content

Commit 6b21ea4

Browse files
claudiamurialdoclaudiamurialdo
authored andcommitted
Prevent KeyNotFoundException by safely retrieving 'CURRENT_GX_CONTEXT' from AppContext.Items (#1197)
Co-authored-by: claudiamurialdo <c.murialdo@globant.com> (cherry picked from commit a779773) # Conflicts: # dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs
1 parent 33fd815 commit 6b21ea4

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ public class GxContext : IGxContext
366366
internal const string GXLanguage = "GXLanguage";
367367
internal const string GXTheme = "GXTheme";
368368
internal const string SERVER_VAR_HTTP_HOST = "HTTP_HOST";
369+
internal const string CURRENT_GX_CONTEXT = "CURRENT_GX_CONTEXT";
369370
[NonSerialized]
370371
HttpContext _HttpContext;
371372
[NonSerialized]
@@ -651,7 +652,7 @@ static public GxContext Current
651652
if (HttpContext.Current != null)
652653
{
653654

654-
GxContext currCtx = (GxContext)HttpContext.Current.Items["CURRENT_GX_CONTEXT"];
655+
GxContext currCtx = (GxContext)HttpContext.Current.Items[CURRENT_GX_CONTEXT];
655656
if (currCtx != null)
656657
return currCtx;
657658
}
@@ -662,29 +663,28 @@ static public GxContext Current
662663
}
663664
return null;
664665
#else
665-
if (AppContext.Current != null)
666-
{
667-
GxContext currCtx = (GxContext)AppContext.Current.Items["CURRENT_GX_CONTEXT"];
668-
if (currCtx != null)
669-
return currCtx;
670-
}
671-
else
672-
{
673-
return _currentBatchGxContext;
674-
}
675-
return null;
666+
if (AppContext.Current != null)
667+
{
668+
if (AppContext.Current.Items.TryGetValue(CURRENT_GX_CONTEXT, out object ctxObj) && ctxObj is GxContext currCtx)
669+
return currCtx;
670+
}
671+
else
672+
{
673+
return _currentBatchGxContext;
674+
}
675+
return null;
676676
#endif
677677
}
678678
}
679679
static void setContext(GxContext ctx)
680680
{
681681
#if !NETCORE
682682
if (HttpContext.Current != null)
683-
HttpContext.Current.Items["CURRENT_GX_CONTEXT"] = ctx;
683+
HttpContext.Current.Items[CURRENT_GX_CONTEXT] = ctx;
684684

685685
#else
686-
if (AppContext.Current != null)
687-
AppContext.Current.Items["CURRENT_GX_CONTEXT"] = ctx;
686+
if (AppContext.Current != null)
687+
AppContext.Current.Items[CURRENT_GX_CONTEXT] = ctx;
688688
#endif
689689

690690
else if (!IsHttpContext)
@@ -4316,4 +4316,4 @@ public override string GetScriptPath()
43164316
}
43174317
}
43184318

4319-
}
4319+
}

0 commit comments

Comments
 (0)