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

Commit f13c48f

Browse files
committed
Add Env ConfigAwait
1 parent cff66e9 commit f13c48f

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/ServiceStack.Text/Env.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using System;
55
using System.Globalization;
66
using System.IO;
7+
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
9+
using System.Threading.Tasks;
710

811
namespace ServiceStack.Text
912
{
@@ -18,12 +21,12 @@ static Env()
1821
IsNetStandard = true;
1922
try
2023
{
21-
IsLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
22-
IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
23-
IsOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
24-
IsNetCore3 = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 3");
24+
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
25+
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
26+
IsOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
27+
IsNetCore3 = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 3");
2528

26-
var fxDesc = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
29+
var fxDesc = RuntimeInformation.FrameworkDescription;
2730
IsMono = fxDesc.Contains("Mono");
2831
IsNetCore = fxDesc.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);
2932
}
@@ -79,7 +82,7 @@ static Env()
7982
IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;
8083
if (IsOSX && IsMono)
8184
{
82-
var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
85+
var runtimeDir = RuntimeEnvironment.GetRuntimeDirectory();
8386
//iOS detection no longer trustworthy so assuming iOS based on some current heuristics. TODO: improve iOS detection
8487
IsIOS = runtimeDir.StartsWith("/private/var") ||
8588
runtimeDir.Contains("/CoreSimulator/Devices/");
@@ -99,11 +102,11 @@ static Env()
99102
VersionString = ServiceStackVersion.ToString(CultureInfo.InvariantCulture);
100103

101104
ServerUserAgent = "ServiceStack/"
102-
+ VersionString + " "
103-
+ PclExport.Instance.PlatformName
104-
+ (IsMono ? "/Mono" : "")
105-
+ (IsLinux ? "/Linux" : IsOSX ? "/OSX" : IsUnix ? "/Unix" : IsWindows ? "/Windows" : "/UnknownOS")
106-
+ (IsIOS ? "/iOS" : IsAndroid ? "/Android" : IsUWP ? "/UWP" : "");
105+
+ VersionString + " "
106+
+ PclExport.Instance.PlatformName
107+
+ (IsMono ? "/Mono" : "")
108+
+ (IsLinux ? "/Linux" : IsOSX ? "/OSX" : IsUnix ? "/Unix" : IsWindows ? "/Windows" : "/UnknownOS")
109+
+ (IsIOS ? "/iOS" : IsAndroid ? "/Android" : IsUWP ? "/UWP" : "");
107110

108111
__releaseDate = new DateTime(2001,01,01);
109112
}
@@ -216,7 +219,7 @@ private static bool IsRunningAsUwp()
216219
{
217220
try
218221
{
219-
IsNetNative = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);
222+
IsNetNative = RuntimeInformation.FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);
220223
return IsInAppContainer || IsNetNative;
221224
}
222225
catch (Exception) {}
@@ -294,9 +297,18 @@ private static bool IsInAppContainer
294297
}
295298
}
296299

297-
[System.Runtime.InteropServices.DllImport("kernel32.dll", ExactSpelling = true)]
300+
[DllImport("kernel32.dll", ExactSpelling = true)]
298301
private static extern int GetCurrentApplicationUserModelId(ref uint applicationUserModelIdLength, byte[] applicationUserModelId);
299302
#endif
300-
303+
304+
public static bool ContinueOnCapturedContext = false;
305+
306+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
307+
public static ConfiguredTaskAwaitable ConfigAwait(this Task task) =>
308+
task.ConfigureAwait(ContinueOnCapturedContext);
309+
310+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
311+
public static ConfiguredTaskAwaitable<object> ConfigAwait(this Task<object> task) =>
312+
task.ConfigureAwait(ContinueOnCapturedContext);
301313
}
302314
}

0 commit comments

Comments
 (0)