Skip to content

Commit

Permalink
Fix faulty netfx check (#911)
Browse files Browse the repository at this point in the history
* Fix faulty netfx check
  • Loading branch information
rjmholt authored Apr 9, 2019
1 parent ffd0ef7 commit 02d21f7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ namespace Microsoft.PowerShell.EditorServices
/// </summary>
public class PowerShellContext : IDisposable, IHostSupportsInteractiveSession
{
private const string DotNetFrameworkDescription = ".NET Framework";

private static readonly Action<Runspace, ApartmentState> 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<Runspace, ApartmentState>), firstArgument: null, method: setterInfo);
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/PowerShellEditorServices/Utility/ExecutionTimer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/PowerShellEditorServices/Utility/Logging.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/PowerShellEditorServices/Utility/PsesLogger.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/PowerShellEditorServices/Utility/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// 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.InteropServices;

namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
/// General purpose common utilities to prevent reimplementation.
/// </summary>
internal static class Utils
{
/// <summary>
/// True if we are running on .NET Core, false otherwise.
/// </summary>
public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal);
}
}

0 comments on commit 02d21f7

Please sign in to comment.