Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5aef718

Browse files
committed
Add fallback for detecting UWP in .NET Native
1 parent 882a7c2 commit 5aef718

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/ServiceStack.Text/Env.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static Env()
5555
IsIOS = runtimeDir.StartsWith("/private/var") ||
5656
runtimeDir.Contains("/CoreSimulator/Devices/");
5757
}
58+
IsNetNative = fxDesc.Contains(".NET Native");
5859
IsNetCore = fxDesc.Contains(".NET Core");
5960
}
6061
catch (Exception) {} //throws PlatformNotSupportedException in AWS lambda
@@ -107,6 +108,8 @@ static Env()
107108

108109
public static bool IsAndroid { get; set; }
109110

111+
public static bool IsNetNative { get; set; }
112+
110113
public static bool IsUWP { get; set; }
111114

112115
public static bool IsNetStandard { get; set; }
@@ -183,17 +186,28 @@ public static string ReferenceAssembyPath
183186

184187
private static bool IsRunningAsUwp()
185188
{
186-
if (IsWindows7OrLower)
187-
return false;
189+
try
190+
{
191+
if (IsWindows7OrLower)
192+
return false;
188193

189-
int length = 0;
190-
var sb = new System.Text.StringBuilder(0);
191-
int result = GetCurrentPackageFullName(ref length, sb);
194+
int length = 0;
195+
var sb = new System.Text.StringBuilder(0);
196+
int result = GetCurrentPackageFullName(ref length, sb);
192197

193-
sb = new System.Text.StringBuilder(length);
194-
result = GetCurrentPackageFullName(ref length, sb);
198+
sb = new System.Text.StringBuilder(length);
199+
result = GetCurrentPackageFullName(ref length, sb);
195200

196-
return result != APPMODEL_ERROR_NO_PACKAGE;
201+
return result != APPMODEL_ERROR_NO_PACKAGE;
202+
}
203+
catch (TypeLoadException e) //of course the recommended code to detect UWP fails in .NET Native UWP
204+
{
205+
return IsWindows && IsNetNative;
206+
}
207+
catch (Exception e)
208+
{
209+
return false;
210+
}
197211
}
198212

199213
private static bool IsWindows7OrLower

0 commit comments

Comments
 (0)