From 3909afb810d20466e48e6da30e4a411dbec1dfaa Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 8 Apr 2019 19:49:16 -0700 Subject: [PATCH 1/3] Fix faulty netfx check --- .../Session/PowerShellContext.cs | 6 ++---- .../Utility/ExecutionTimer.cs | 5 +++++ .../Utility/Logging.cs | 5 +++++ .../Utility/PsesLogger.cs | 5 +++++ src/PowerShellEditorServices/Utility/Utils.cs | 20 +++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/PowerShellEditorServices/Utility/Utils.cs diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 2284a99ff..fbfe46cb2 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -34,14 +34,12 @@ namespace Microsoft.PowerShell.EditorServices /// public class PowerShellContext : IDisposable, IHostSupportsInteractiveSession { - private const string DotNetFrameworkDescription = ".NET Framework"; - private static readonly Action s_runspaceApartmentStateSetter; static PowerShellContext() { // PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection - if (RuntimeInformation.FrameworkDescription.Equals(DotNetFrameworkDescription)) + if (!Utils.IsNetCore) { MethodInfo setterInfo = typeof(Runspace).GetProperty("ApartmentState").GetSetMethod(); Delegate setter = Delegate.CreateDelegate(typeof(Action), firstArgument: null, method: setterInfo); @@ -195,7 +193,7 @@ public static Runspace CreateRunspace(PSHost psHost) // Windows PowerShell must be hosted in STA mode // This must be set on the runspace *before* it is opened - if (RuntimeInformation.FrameworkDescription.Equals(DotNetFrameworkDescription)) + if (s_runspaceApartmentStateSetter != null) { s_runspaceApartmentStateSetter(runspace, ApartmentState.STA); } diff --git a/src/PowerShellEditorServices/Utility/ExecutionTimer.cs b/src/PowerShellEditorServices/Utility/ExecutionTimer.cs index 994f83d27..2634eb6eb 100644 --- a/src/PowerShellEditorServices/Utility/ExecutionTimer.cs +++ b/src/PowerShellEditorServices/Utility/ExecutionTimer.cs @@ -1,3 +1,8 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + using System; using System.Diagnostics; using System.Runtime.CompilerServices; diff --git a/src/PowerShellEditorServices/Utility/Logging.cs b/src/PowerShellEditorServices/Utility/Logging.cs index 6813feacb..4813789d3 100644 --- a/src/PowerShellEditorServices/Utility/Logging.cs +++ b/src/PowerShellEditorServices/Utility/Logging.cs @@ -1,3 +1,8 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + using System; using System.Collections.Generic; using Serilog; diff --git a/src/PowerShellEditorServices/Utility/PsesLogger.cs b/src/PowerShellEditorServices/Utility/PsesLogger.cs index e7dd29e9e..de257f556 100644 --- a/src/PowerShellEditorServices/Utility/PsesLogger.cs +++ b/src/PowerShellEditorServices/Utility/PsesLogger.cs @@ -1,3 +1,8 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + using System; using System.Runtime.CompilerServices; using System.Text; diff --git a/src/PowerShellEditorServices/Utility/Utils.cs b/src/PowerShellEditorServices/Utility/Utils.cs new file mode 100644 index 000000000..6fec7007f --- /dev/null +++ b/src/PowerShellEditorServices/Utility/Utils.cs @@ -0,0 +1,20 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System.Runtime.InteropServices; + +namespace Microsoft.PowerShell.EditorServices +{ + /// + /// General purpose common utilities to prevent reimplementation. + /// + internal static class Utils + { + /// + /// True if we are running on .NET Core, false otherwise. + /// + public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"); + } +} From 4a3af4bf375c2b4549db32a5310002e8928093e8 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Tue, 9 Apr 2019 09:19:21 -0700 Subject: [PATCH 2/3] Update src/PowerShellEditorServices/Utility/Utils.cs Co-Authored-By: rjmholt --- src/PowerShellEditorServices/Utility/Utils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Utility/Utils.cs b/src/PowerShellEditorServices/Utility/Utils.cs index 6fec7007f..92739b293 100644 --- a/src/PowerShellEditorServices/Utility/Utils.cs +++ b/src/PowerShellEditorServices/Utility/Utils.cs @@ -15,6 +15,6 @@ internal static class Utils /// /// True if we are running on .NET Core, false otherwise. /// - public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"); + public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal); } } From 1dd99018c841c9c7555a8f56fd5e6f5984b2db9d Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Tue, 9 Apr 2019 09:19:29 -0700 Subject: [PATCH 3/3] Update src/PowerShellEditorServices/Utility/Utils.cs Co-Authored-By: rjmholt --- src/PowerShellEditorServices/Utility/Utils.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PowerShellEditorServices/Utility/Utils.cs b/src/PowerShellEditorServices/Utility/Utils.cs index 92739b293..05787be43 100644 --- a/src/PowerShellEditorServices/Utility/Utils.cs +++ b/src/PowerShellEditorServices/Utility/Utils.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // +using System; using System.Runtime.InteropServices; namespace Microsoft.PowerShell.EditorServices