Skip to content

Conversation

@SteveL-MSFT
Copy link
Member

WSManConfigProvider doesn't work once this is enabled if starting powershell with -sta, it does work if using -mta. The same/similar WSManConfigProvider code works fine under Windows PowerShell 5.1 with sta or mta. We shouldn't have to change the WSManConfigProvider code which relies on underlying non-thread safe COM objects, but need some help debugging this.

Fix #4350

add support for -sta/-mta in powershell.exe
enable ApartmentState support overall
some FullCLR code cleanup
fix WSMan provider
@SteveL-MSFT SteveL-MSFT force-pushed the apartmentstate-support branch from 4d1e4f8 to 9a7782d Compare September 7, 2017 22:51
// So just return the CurrentTimeZone.

#if !CORECLR // TimeZone Not In CoreCLR
#if !CORECLR // TimeZone deprecated, need to change to TimeZoneInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add that this change affects serialization, remoting and backward compatibility. We can go back to that when we'll have tests for the areas.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, will add

// So just return the CurrentTimeZone.

#if !CORECLR // TimeZone Not In CoreCLR
#if !CORECLR // TODO: TimeZone deprecated, need to change to TimeZoneInfo. Affects serialization, remoting, and backwards compatibility.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to documentation here: https://docs.microsoft.com/en-us/dotnet/api/system.timezone?view=netcore-2.0
TimeZone class can still be used to get CurrentTimeZone. A comment about deprecation might not be accurate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At compilation time, dotnet reported it as deprecated

Copy link
Collaborator

@iSazonov iSazonov Sep 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SteveL-MSFT
What do you mean when you say the WSManConfigProvider doesn't work? Do you have repro steps and errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PaulHigin using sta, you would get this error:

dir : COM object that has been separated from its underlying RCW cannot be used.
At line:1 char:1
+ dir
+ ~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], InvalidComObjectException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.InvalidComObjectException,Microsoft.PowerShell.Commands.GetChildItemCommand

@TravisEz13 TravisEz13 removed their request for review September 14, 2017 19:34
#if !CORECLR
runspace.ApartmentState = ApartmentState.STA;
#endif
runspace.ThreadOptions = PSThreadOptions.ReuseThread;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line supposed to be removed?

if (Credential_Delegation_Key == null)
Credential_Delegation_Key = rootKey.CreateSubKey(Registry_Path_Credentials_Delegation
#if !CORECLR
, RegistryKeyPermissionCheck.ReadWriteSubTree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Microsoft.Win32.RegistryKeyPermissionCheck is available in .NET Core. Maybe we should keep it?

if (Allow_Fresh_Credential_Key == null)
Allow_Fresh_Credential_Key = rootKey.CreateSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials
#if !CORECLR
, RegistryKeyPermissionCheck.ReadWriteSubTree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

if (Environment.OSVersion.Version.Major < 6)
{
//OS is XP/Win2k3. Throw error.
string message = helper.FormatResourceMsgFromResourcetext("CmdletNotAvailable");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the resource string CmdletNotAvailable can be removed?

<ItemGroup>
<ProjectReference Include="..\System.Management.Automation\System.Management.Automation.csproj" />
<ProjectReference Include="..\Microsoft.WSMan.Runtime\Microsoft.WSMan.Runtime.csproj" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="4.4.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you rebase? Some changes in this PR is also in #4756

RegistryKey rGPOLocalMachineKey = Registry.LocalMachine.OpenSubKey(
Registry_Path_Credentials_Delegation + @"\CredentialsDelegation",
#if !CORECLR
RegistryKeyPermissionCheck.ReadWriteSubTree,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

{
rGPOLocalMachineKey = rGPOLocalMachineKey.OpenSubKey(Key_Allow_Fresh_Credentials,
#if !CORECLR
RegistryKeyPermissionCheck.ReadWriteSubTree,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

#if CORECLR
"0409" /* TODO: don't assume it is always English on CSS? */
#else
String.Concat("0", String.Format(CultureInfo.CurrentCulture, "{0:x2}", checked((uint)CultureInfo.CurrentUICulture.LCID)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we use the #else block instead?

else if (i > 100)
i = 100; // maximum 100MB
return i * 1000000;
// TODO: move this to startupconfig is needed. This number is in bytes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add one more comment indicating we use 10MB by default?

_closed = false;
}
#else
internal PipelineThread(ApartmentState apartmentState)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that this method can be called on Unix plats? If so, _worker.SetApartmentState(apartmentState) might throw.

@SteveL-MSFT
Copy link
Member Author

@daxian-dbw I've been thinking that I've been looking at this problem the wrong way. ApartmentState is only used for COM interop and dotnet will marshal between STA and MTA so the only loss is some perf impact. I'm thinking that the right thing to do here is to remove support for ApartmentState. Thoughts?

@daxian-dbw
Copy link
Member

and dotnet will marshal between STA and MTA

@SteveL-MSFT I'm not very clear about the quoted sentence. Can you please elaborate a bit on it?

My understanding is that STA is for proper calls to COM object with the single-thread apartment (mainly about COM objects related to the user interface, for example, windows forms). Windows form may not be a problem as it's not supported in .NET Core. However, it may still be possible that user gets a STA COM object by New-Object and operate on it, so it might be affected when removing the Apartment support from powershell core.

A question, if we remove Apartment support, what would be the default thread apartment for powershell?

I want to point out that on Windows PowerShell, users are able to specify the ApartmentState in PSInvocationSettings and pass the setting to powershell.Invoke(...). Removing ApartmentState support in PS Core is technically a breaking change, so it probably needs to be reviewed by the Committee.

@SteveL-MSFT
Copy link
Member Author

@daxian-dbw see Interop Marshaling

I'll close this for now and open a new issue regarding removing ApartmentState support and will discuss with Committee

@SteveL-MSFT SteveL-MSFT deleted the apartmentstate-support branch October 16, 2017 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants