Skip to content

Commit 96717c3

Browse files
Conversion functions involving time must include the context parameter (#911)
* Load service files during startup to better handle a request storm at the startup of kestrel. Conversion functions involving time must now include the context parameter. The use of GxContext.Current is deprecated and should be avoided. * Minor typo in message.
1 parent 9039c5f commit 96717c3

File tree

3 files changed

+85
-25
lines changed

3 files changed

+85
-25
lines changed

dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRouting.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public GXRouting(string baseURL)
5858
ServicesGroupSetting();
5959
ServicesFunctionsMetadata();
6060
GetAzureDeploy();
61+
SvcFiles();
6162
}
6263

6364
static public List<ControllerInfo> GetRouteController(Dictionary<string, string> apiPaths,
@@ -396,7 +397,7 @@ public GxRestWrapper GetController(HttpContext context, ControllerInfo controlle
396397
bool privateDirExists = Directory.Exists(privateDir);
397398

398399
GXLogging.Debug(log, $"PrivateDir:{privateDir} asssemblycontroller:{asssemblycontroller}");
399-
400+
string svcFile=null;
400401
if (privateDirExists && File.Exists(Path.Combine(privateDir, $"{asssemblycontroller.ToLower()}.grp.json")))
401402
{
402403
controller = tmpController;
@@ -411,9 +412,7 @@ public GxRestWrapper GetController(HttpContext context, ControllerInfo controlle
411412
else
412413
{
413414
string controllerLower = controller.ToLower();
414-
string svcFile = SvcFile($"{controller}{SvcExtension}");
415-
if (svcFile==null)
416-
svcFile = SvcFile($"{controllerLower}{SvcExtension}");
415+
svcFile = SvcFile($"{controller}{SvcExtension}");
417416
if (File.Exists(svcFile))
418417
{
419418
string[] controllerAssemblyQualifiedName = new string(File.ReadLines(svcFile).First().SkipWhile(c => c != '"')
@@ -445,22 +444,26 @@ public GxRestWrapper GetController(HttpContext context, ControllerInfo controlle
445444
GXLogging.Warn(log, $"Controller was not found");
446445
return null;
447446
}
448-
string SvcFile(string controller)
447+
448+
private void SvcFiles()
449449
{
450-
if (svcFiles == null)
450+
svcFiles = new HashSet<string>(new CaseInsensitiveStringEqualityComparer());
451+
foreach (string file in Directory.GetFiles(ContentRootPath, SvcExtensionPattern, SearchOption.AllDirectories))
451452
{
452-
svcFiles = new HashSet<string>();
453-
foreach (string file in Directory.GetFiles(ContentRootPath, SvcExtensionPattern, SearchOption.AllDirectories))
454-
{
455-
svcFiles.Add(file);
456-
}
453+
svcFiles.Add(file);
457454
}
455+
}
456+
string SvcFile(string controller)
457+
{
458458
string fileName;
459-
string controllerFullName = Path.Combine(ContentRootPath, controller);
460-
if (svcFiles.TryGetValue(new FileInfo(controllerFullName).FullName, out fileName))
459+
string controllerFullName = new FileInfo(Path.Combine(ContentRootPath, controller)).FullName;
460+
if (svcFiles.TryGetValue(controllerFullName, out fileName))
461461
return fileName;
462462
else
463+
{
464+
GXLogging.Warn(log, "Service file not found:" + controllerFullName);
463465
return null;
466+
}
464467

465468
}
466469
public void ServicesGroupSetting()
@@ -600,7 +603,18 @@ public string GAM
600603

601604

602605
}
606+
internal class CaseInsensitiveStringEqualityComparer : IEqualityComparer<string>
607+
{
608+
public bool Equals(string x, string y)
609+
{
610+
return string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
611+
}
603612

613+
public int GetHashCode(string obj)
614+
{
615+
return obj.ToLower().GetHashCode();
616+
}
617+
}
604618
public static class AzureFeature
605619
{
606620
public const string AzureServerless = "AzureServerless";

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

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,18 +2656,18 @@ public static DateTime CToD2(string value)
26562656
}
26572657
return nullDate;
26582658
}
2659-
internal static DateTime CToDT2(string jsonDate) {
2659+
internal static DateTime CToDT2(string jsonDate, IGxContext context) {
26602660

26612661
if (!string.IsNullOrEmpty(jsonDate) && (jsonDate.Contains(ISO_8601_TIME_SEPARATOR) || jsonDate.Contains(ISO_8601_TIME_SEPARATOR_1)))
26622662
{
2663-
return CToT2(jsonDate);
2663+
return CToT2(jsonDate, context);
26642664
}
26652665
else
26662666
{
26672667
return CToD2(jsonDate);
26682668
}
26692669
}
2670-
public static DateTime CToT2(string value)
2670+
public static DateTime CToT2(string value, IGxContext context)
26712671
{
26722672
if (isNullJsonDate(value))
26732673
return nullDate;
@@ -2684,38 +2684,76 @@ public static DateTime CToT2(string value)
26842684
}
26852685
}
26862686
if (Preferences.useTimezoneFix())
2687-
ret = fromUniversalTime(ret);
2687+
{
2688+
if (context==null)
2689+
ret = fromUniversalTime(ret);
2690+
else
2691+
ret = fromUniversalTime(ret, context);
2692+
}
26882693
return ret;
26892694
}
26902695
}
2696+
//[Obsolete("CToT2 is deprecated, use CToT2(string, IGxContext) instead", false)]
2697+
public static DateTime CToT2(string value)
2698+
{
2699+
return CToT2(value, null);
2700+
}
2701+
//[Obsolete("TToC2 is deprecated, use TToC2(DateTime, IGxContext) instead", false)]
26912702
public static string TToC2(DateTime dt)
26922703
{
26932704
return TToC2(dt, true);
26942705
}
2706+
public static string TToC2(DateTime dt, IGxContext context)
2707+
{
2708+
return TToC2(dt, true, context);
2709+
}
2710+
//[Obsolete("TToC2 is deprecated, use TToC2(DateTime, bool, IGxContext) instead", false)]
26952711
public static string TToC2(DateTime dt, bool toUTC)
26962712
{
2697-
return TToCRest(dt, "0000-00-00T00:00:00", JsonDateFormat, toUTC);
2713+
return TToCRest(dt, "0000-00-00T00:00:00", JsonDateFormat, null, toUTC);
2714+
}
2715+
internal static string TToC2(DateTime dt, bool toUTC, IGxContext context)
2716+
{
2717+
return TToCRest(dt, "0000-00-00T00:00:00", JsonDateFormat, context, toUTC);
26982718
}
2719+
//[Obsolete("TToC3 is deprecated, use TToC3(DateTime, IGxContext) instead", false)]
26992720

27002721
public static string TToC3(DateTime dt)
27012722
{
27022723
return TToC3(dt, true);
27032724
}
2725+
public static string TToC3(DateTime dt, IGxContext context)
2726+
{
2727+
return TToC3(dt, true, context);
2728+
}
27042729
internal const string JsonDateFormatMillis = "yyyy-MM-ddTHH:mm:ss.fff";
27052730
internal const string JsonDateFormat = "yyyy-MM-ddTHH:mm:ss";
2706-
2731+
2732+
//[Obsolete("TToC3 is deprecated, use TToC3(DateTime, bool, IGxContext) instead", false)]
27072733
public static string TToC3(DateTime dt, bool toUTC)
27082734
{
2709-
return TToCRest(dt, "0000-00-00T00:00:00.000", JsonDateFormatMillis, toUTC);
2735+
return TToCRest(dt, "0000-00-00T00:00:00.000", JsonDateFormatMillis, null, toUTC);
2736+
}
2737+
internal static string TToC3(DateTime dt, bool toUTC, IGxContext context)
2738+
{
2739+
return TToCRest(dt, "0000-00-00T00:00:00.000", JsonDateFormatMillis, context, toUTC);
27102740
}
27112741

2712-
static string TToCRest(DateTime dt, String nullString, String formatStr, bool toUTC=true)
2742+
static string TToCRest(DateTime dt, String nullString, String formatStr, IGxContext context, bool toUTC=true)
27132743
{
27142744
if (dt == nullDate)
27152745
return FormatEmptyDate(nullString);
27162746
else
27172747
{
2718-
DateTime ret = Preferences.useTimezoneFix() ? (toUTC ? toUniversalTime(dt) : dt) : dt;
2748+
DateTime ret;
2749+
if (context != null)
2750+
{
2751+
ret = Preferences.useTimezoneFix() ? (toUTC ? toUniversalTime(dt, context) : dt) : dt;
2752+
}
2753+
else
2754+
{
2755+
ret = Preferences.useTimezoneFix() ? (toUTC ? toUniversalTime(dt) : dt) : dt;
2756+
}
27192757
return ret.ToString(formatStr, CultureInfo.InvariantCulture);
27202758
}
27212759
}
@@ -3536,7 +3574,7 @@ static private bool isNullDateCompatible(DateTime dt)
35363574
else
35373575
return isNullDate(dt);
35383576
}
3539-
3577+
//[Obsolete("fromUniversalTime is deprecated, use fromUniversalTime(DateTime, IGxContext) instead", false)]
35403578
static public DateTime fromUniversalTime(DateTime dt)
35413579
{
35423580
#if NODATIME
@@ -3545,7 +3583,15 @@ static public DateTime fromUniversalTime(DateTime dt)
35453583
return isNullDateCompatible(dt) ? dt : fromUniversalTime(dt, GxContext.Current.GetOlsonTimeZone());
35463584
#endif
35473585
}
3548-
3586+
static public DateTime fromUniversalTime(DateTime dt, IGxContext context)
3587+
{
3588+
#if NODATIME
3589+
return isNullDateCompatible(dt) ? dt : fromUniversalTime(dt, context.GetTimeZone());
3590+
#else
3591+
return isNullDateCompatible(dt) ? dt : fromUniversalTime(dt, context.GetOlsonTimeZone());
3592+
#endif
3593+
}
3594+
//[Obsolete("toUniversalTime is deprecated, use toUniversalTime(DateTime, IGxContext) instead", false)]
35493595
static public DateTime toUniversalTime(DateTime dt)
35503596
{
35513597
#if NODATIME

dotnet/src/dotnetframework/GxClasses/Services/ReflectionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private static object ConvertSingleJsonItem(object value, Type newType, IGxConte
150150
else if (newType == typeof(DateTime))
151151
{
152152
string jsonDate = value as string;
153-
return DateTimeUtil.CToDT2(jsonDate);
153+
return DateTimeUtil.CToDT2(jsonDate, context);
154154
}
155155
else if (newType == typeof(Geospatial))
156156
{

0 commit comments

Comments
 (0)