aws / aws-sdk-net

The official AWS SDK for .NET. For more information on the AWS SDK for .NET, see our web site:
http://aws.amazon.com/sdkfornet/
Apache License 2.0
2.06k stars 854 forks source link

AWSPowerShell cmdlets error - Could not load type from assembly #689

Closed Jonatan-B closed 7 years ago

Jonatan-B commented 7 years ago

I was following the documentation to start using the AWSPowershell Module, and installed AWS SDK for .NET on the server but when I would open powershell and import the module I would receive an error.

Expected Behavior

The documentation on setting up my credentials into a profile, said to use the Set-AWSCredential and saved them as a profile, and then i'd be able to use my profile with other cmdlets.

Current Behavior

Then issue started when I entered the Set-AWSCredential and received following error:

Get-AWSCredential : Could not load type "Amazon.Runtime.CredentialManagement.CredentialProfile" from assembly "AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604".
At line:1 char:1
+ Get-AWSCredential
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-AWSCredential], TypeLoadException
    + FullyQualifiedErrorId : System.TypeLoadException,Amazon.PowerShell.Common.GetCredentialCmdlet

I received this error on a number of other cmdlets as well. (For example, Get-AWSCredential, Get-EC2Instance, etc)

Possible Solution

I loaded the assembly on PowerShell and I saw that the dll was being loaded from the GAC, which had an older version of the file. I removed the old dll and reloaded PowerShell and it started working.

Steps to Reproduce (for bugs)

I'm not sure how to reproduce the bug. These happened on Terminal servers that are shared between multiple people; however, I made sure that the SDK and the PowerShell module were not installed before performing the installation.

The old dll file was over a year old too, 6/6/2016, so maybe someone installed it the SDK at one point and since removed it; however, it's hard to tell.

Context

This issue prevented me from being able to use the AWSPowershell cmdlets and manage my VPC through code.

I couldn't find any help on google, or even from AWS support, about this issue, so I'm hoping to be able to provide someone with some insight in case they find themselves in this situation.

Your Environment

.NET Core Info

stevejroberts commented 7 years ago

Thanks for reporting the issue and figuring out what the problem was.

None of our installers have been placing SDK assemblies into the GAC for quite some time, and any behavior that did pre-dates the v3 SDK and PowerShell tools so it's a bit of a mystery as to how these older assemblies got there.

The AWSPowerShell module manifest lists the SDK assemblies that we depend on. I'm going to see if we can prefix these with a variable that might cause the shell to start looking at the install folder for the module first in the hopes that this might circumvent the issue should it occur again. Apart from that, there's little I can think of to do to 'fix' this so I'm going to resolve the issue.

MartinSGill commented 6 years ago

I think I've run into the same issue. I'm going to be a bit verbose here to make it easier for googlers to find it.

For me I was actually using AWSPowershell.NetCore (running on windows 10), and I get the following error:

 Get-AWSCredential -ListProfileDetail
Get-AWSCredential : The type initializer for 'Amazon.AWSConfigs' threw an exception.
At line:1 char:1
+ Get-AWSCredential -ListProfileDetail
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-AWSCredential], TypeInitializationException
+ FullyQualifiedErrorId : System.TypeInitializationException,Amazon.PowerShell.Common.GetCredentialCmdlet

After attaching the debugger and digging to the exception, I found that I also had a DLL coming from the GAC. Which I then confirmed with the gacutil.

 PS> & "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\gacutil.exe" /l | Select-String AWS
InputStream:457:  AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL
InputStream:458:  AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL

The actual version I found in the GAC was:

[assembly: AssemblyVersion("3.3.0.0")]
[assembly: AssemblyTitle("AWSSDK.Core")]
[assembly: AssemblyDescription("The Amazon Web Services SDK for .NET (4.5) - Core Runtime")]
[assembly: AssemblyProduct("Amazon Web Services SDK for .NET")]
[assembly: AssemblyCompany("Amazon.com, Inc")]
[assembly: AssemblyCopyright("Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.")]
[assembly: AssemblyFileVersion("3.3.21.6")]
[assembly: TargetFramework(".NETFramework,Version=v4.5", FrameworkDisplayName = ".NET Framework 4.5")]
[assembly: AssemblyInformationalVersion("1.0.0")]

Removing the DLLs from the GAC and restarting resolved the issue.