Skip to content

Commit c6337a5

Browse files
Ensure batch context is used only for batch processes (#1120)
* Ensure static _currentGxContext used for batch process is not accessed from a web app or http service. * Keep compatibility in .net framework, when there is no current context in web application it must return null. An error NullReferenceException was thrown by GxWebServicesConfig because GxContext.Current.SoapContext was null. public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext) { GxHelpers.GetSoapContext().AppendXml(request.ToString()); GxHelpers.GetSoapContext().EndMessage(); return null; } * Initialize HttpContext on GxContext. * Ensure consistent behavior with .NET Framework when retrieving the current GxContext.
1 parent 88ce097 commit c6337a5

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,12 @@ private void ConfigureSessionService(IServiceCollection services, ISessionServic
431431
});
432432
}
433433
}
434-
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, ILoggerFactory loggerFactory)
434+
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, ILoggerFactory loggerFactory, IHttpContextAccessor contextAccessor)
435435
{
436436
string baseVirtualPath = string.IsNullOrEmpty(VirtualPath) ? VirtualPath : $"/{VirtualPath}";
437437
LogConfiguration.SetupLog4Net();
438-
438+
AppContext.Configure(contextAccessor);
439+
439440
var provider = new FileExtensionContentTypeProvider();
440441
//mappings
441442
provider.Mappings[".json"] = "application/json";

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ public interface IGxContext
284284
string GetURLBuildNumber(string resourcePath, string urlBuildNumber);
285285
}
286286
#if NETCORE
287+
internal static class AppContext
288+
{
289+
static IHttpContextAccessor _httpContextAccessor { get; set; }
290+
internal static HttpContext Current => _httpContextAccessor != null ? new GxHttpContextAccesor(_httpContextAccessor) : null;
291+
internal static void Configure(IHttpContextAccessor accessor)
292+
{
293+
_httpContextAccessor = accessor;
294+
}
295+
296+
}
287297
public class GxHttpContextAccesor : HttpContext
288298
{
289299
IHttpContextAccessor ctxAccessor;
@@ -603,11 +613,12 @@ public CookieContainer GetCookieContainer(string url, bool includeCookies = true
603613
}
604614

605615
[NonSerialized]
606-
static GxContext _currentGxContext;
616+
static GxContext _currentBatchGxContext;
607617
static public GxContext Current
608618
{
609619
get
610620
{
621+
611622
#if !NETCORE
612623
if (HttpContext.Current != null)
613624
{
@@ -619,11 +630,21 @@ static public GxContext Current
619630
else
620631
{
621632

622-
return _currentGxContext;
633+
return _currentBatchGxContext;
623634
}
624635
return null;
625636
#else
626-
return _currentGxContext;
637+
if (AppContext.Current != null)
638+
{
639+
GxContext currCtx = (GxContext)AppContext.Current.Items["CURRENT_GX_CONTEXT"];
640+
if (currCtx != null)
641+
return currCtx;
642+
}
643+
else
644+
{
645+
return _currentBatchGxContext;
646+
}
647+
return null;
627648
#endif
628649
}
629650
}
@@ -632,9 +653,14 @@ static void setContext(GxContext ctx)
632653
#if !NETCORE
633654
if (HttpContext.Current != null)
634655
HttpContext.Current.Items["CURRENT_GX_CONTEXT"] = ctx;
635-
else
656+
657+
#else
658+
if (AppContext.Current != null)
659+
AppContext.Current.Items["CURRENT_GX_CONTEXT"] = ctx;
636660
#endif
637-
_currentGxContext = ctx;
661+
662+
else if (!IsHttpContext)
663+
_currentBatchGxContext = ctx;
638664
}
639665

640666
public LocalUtil localUtil
@@ -1175,7 +1201,12 @@ public HttpContext HttpContext
11751201
{
11761202
get
11771203
{
1178-
#if !NETCORE
1204+
#if NETCORE
1205+
if (_HttpContext == null && AppContext.Current != null)
1206+
{
1207+
HttpContext = AppContext.Current;
1208+
}
1209+
#else
11791210
if (_HttpContext == null && HttpContext.Current != null)
11801211
{
11811212
HttpContext = HttpContext.Current;

0 commit comments

Comments
 (0)