Shivprasad Pai
Shivprasad Pai

Reputation: 1

PowerShell starts throwing intermittent CAS errors for an assembly (Class Library) loaded with Add-Type

I have created an Class Library Assembly in .NET Framework 4.8. The Class Library references AWSSDK nuget packages amongst others. The Class Library in turn is consumed by a PowerShell module. The module loads the Class Library using the Add-Type cmdlet with -Path parameter.

Add-Type -Path $assemblyFullName

The PowerShell module works perfectly fine in the Local environment (Windows 11). It also works fine the first time it is runs on the Server (EC2 Instance - Windows 2022 Data Center edition). However after that it starts throwing Loader Exceptions.

The specific message I receive is

Inheritance security rules violated by type: 'Amazon.AppConfigData.Model.StartConfigurationSessionResponse'. Derived types must either match the security accessibility of the base type or be less accessible. Inheritance security rules violated by type: 'Amazon.SimpleSystemsManagement.Model.GetParameterResponse'. Derived types must either match the security accessibility of the base type or be less accessible.

After fiddling around with CAS (Code Access Security), I have realized that the exception is misleading. There is no issue with the mentioned Classes. It has to do something with PowerShell and the Server but I am not able to figure out what it could be.

I say this because I created a mock Assembly similar to the original one with the AWSSDK dependencies that I get the message about. I created a mock PowerShell module and loaded the mock assembly. The mock PowerShell module ran without any issues. Then I ran the original PowerShell module, which too ran without any issues. Strangely, then when I ran the mock PowerShell module again, it started throwing the exceptions with the messages. The original PS module also started throwing the same exceptions.

The AWSSDK nuget packages I am referencing are

I don't know if this is important or not - I am running x86 instance of PowerShell and the assembly is compiled for "Any CPU". I am putting the Module in the x86 Module path of PowerShell (C:\Program Files (x86)\WindowsPowerShell\Modules)

Has anyone faced similar issues ? Any help would be appreciated.

Upvotes: 0

Views: 19

Answers (0)

Related Questions