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

Commit c0bcb0a

Browse files
committed
Replace with better way for determining if running in UWP
1 parent 5aef718 commit c0bcb0a

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/ServiceStack.Text/Env.cs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static Env()
7575
SupportsExpressions = !IsIOS;
7676
SupportsEmit = !IsIOS && !IsUWP;
7777

78-
if (IsUWP || IsIOS)
78+
if (IsNetNative || IsIOS)
7979
{
8080
ReflectionOptimizer.Instance = ExpressionReflectionOptimizer.Provider;
8181
}
@@ -176,12 +176,45 @@ public static string ReferenceAssembyPath
176176
}
177177
set => referenceAssembyPath = value;
178178
}
179-
179+
180+
private static bool IsRunningAsUwp()
181+
{
182+
//TODO: find a more reliable way to detect UWP https://github.com/dotnet/corefx/issues/31599
183+
#if NETSTANDARD2_0
184+
try
185+
{
186+
if (IsWindows7OrLower)
187+
return false;
188+
189+
var isWin = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
190+
var fxDesc = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
191+
var isNetNative = fxDesc.Contains(".NET Native");
192+
var onlyInDebugType = Type.GetType("System.Runtime.InteropServices.WindowsRuntime.IActivationFactory,System.Runtime.InteropServices.WindowsRuntime");
193+
return isWin && (isNetNative || onlyInDebugType != null);
194+
}
195+
catch (Exception) {} //throws PlatformNotSupportedException in AWS lambda
196+
#endif
197+
return false;
198+
}
199+
200+
private static bool IsWindows7OrLower
201+
{
202+
get
203+
{
204+
int versionMajor = Environment.OSVersion.Version.Major;
205+
int versionMinor = Environment.OSVersion.Version.Minor;
206+
double version = versionMajor + (double)versionMinor / 10;
207+
return version <= 6.1;
208+
}
209+
}
210+
211+
/* Only way to determine if .NET Standard 2.0 .dll is running in UWP is now a build error from VS.NET 15.8 Preview
212+
180213
//https://blogs.msdn.microsoft.com/appconsult/2016/11/03/desktop-bridge-identify-the-applications-context/
181214
//https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
182215
const long APPMODEL_ERROR_NO_PACKAGE = 15700L;
183216
184-
[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
217+
[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
185218
static extern int GetCurrentPackageFullName(ref int packageFullNameLength, System.Text.StringBuilder packageFullName);
186219
187220
private static bool IsRunningAsUwp()
@@ -209,17 +242,7 @@ private static bool IsRunningAsUwp()
209242
return false;
210243
}
211244
}
212-
213-
private static bool IsWindows7OrLower
214-
{
215-
get
216-
{
217-
int versionMajor = Environment.OSVersion.Version.Major;
218-
int versionMinor = Environment.OSVersion.Version.Minor;
219-
double version = versionMajor + (double)versionMinor / 10;
220-
return version <= 6.1;
221-
}
222-
}
245+
*/
223246

224247
}
225248
}

0 commit comments

Comments
 (0)