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.03k stars 849 forks source link

AmazonCognitoIdentityConfig throws exception in release build #2535

Closed bryceburrows closed 1 year ago

bryceburrows commented 1 year ago

Describe the bug

This code works in debug mode on my iMac when running my Xamarin forms app.


var   credentials = new CognitoAWSCredentials(
                "ap-southeast-2:etcetcblahdeblah", // Identity pool ID
                RegionEndpoint.APSoutheast2 // Region

But throws a very poor exception when run in release build

"The type initializer for 'Amazon.CognitoIdentity.AmazonCognitoIdentityConfig' threw an exception."

var config = new AmazonCognitoIdentityConfig(); config.RegionEndpoint = RegionEndpoint.APSoutheast2;

throws the same exception.

Expected Behavior

I expect the release build version to work like the Debug version does.

Current Behavior

this exception is thrown

The type initializer for 'Amazon.CognitoIdentity.AmazonCognitoIdentityConfig' threw an exception.

Reproduction Steps

//        var config = new AmazonCognitoIdentityConfig();
  //      config.RegionEndpoint = RegionEndpoint.APSoutheast2;

        System.Diagnostics.Debug.WriteLine("ready to try");
            try
        {
credentials = new CognitoAWSCredentials(
            "ap-southeast-2:extrabigthatisntgoingtobeshared", // Identity pool ID
            RegionEndpoint.APSoutheast2 // Region
);
        }
        catch (Exception ex)
        {

            System.Diagnostics.Debug.WriteLine(ex.Message);

        }

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWSSDK.CognitoIdentity 3.7.100.69

Targeted .NET Platform

Xamarin forms Android

Operating System and version

iMac Monterey

ashishdhingra commented 1 year ago

Hi @bryceburrows,

Good morning.

Thanks for opening the issue. Could you please enable verbose logging and shared the detailed logs? Verbose logging could be enabled using the below code:

Amazon.AWSConfigs.LoggingConfig.LogResponses = Amazon.ResponseLoggingOption.Always;
Amazon.AWSConfigs.LoggingConfig.LogTo = Amazon.LoggingOptions.SystemDiagnostics;
Amazon.AWSConfigs.AddTraceListener("Amazon", new System.Diagnostics.ConsoleTraceListener());

Thanks, Ashish

bryceburrows commented 1 year ago

Hiya ConsoleTraceListener doesn't exist in the Xamarin forms version the app currently targets. I tried pointing a defaulttracerlistener to log to a file but nothing was created :/

Do you have another option?

ashishdhingra commented 1 year ago

Hiya ConsoleTraceListener doesn't exist in the Xamarin forms version the app currently targets. I tried pointing a defaulttracerlistener to log to a file but nothing was created :/

Do you have another option?

@bryceburrows You could try using log4net as mentioned in https://aws.amazon.com/blogs/developer/logging-with-the-aws-sdk-for-net/ to log to a file. Quick question, how are you authenticating via Cognito and then using the temporary session credentials in your application?

github-actions[bot] commented 1 year ago

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

bryceburrows commented 1 year ago

Hi there, i don't seem to be able to get log4net going either - can i get some instruction on making it work?

ashishdhingra commented 1 year ago

@bryceburrows Could you please share the following:

Few Xamarin samples for reference here

Thanks, Ashish

bryceburrows commented 1 year ago

thankyou for your reply - it is appreciated

Are you authenticating via Cognito and then using the temporary session credentials in your application?

it falls over in this line (in release build only - like i say debug works)

credentials = new CognitoAWSCredentials( "ap-southeast-2:extrabigthatisntgoingtobeshared", // Identity pool ID RegionEndpoint.APSoutheast2 // Region

As it throws the exception here, the credentials don't get returned to be used for anything

Does it work in release build for a normal console application?

I shall try and build one and see.

Does the error occurs in actual deployed application? If yes, have you tried running the release build via Visual Studio in an emulator?

as it is not working , i haven't deployed it outside my dev environment. I am running it from visual studio on an actual phone.

I tried a console app and that worked in both debug and release and i can call the lambda from there....so the problem remains in the xamarin release build

ashishdhingra commented 1 year ago

thankyou for your reply - it is appreciated

Are you authenticating via Cognito and then using the temporary session credentials in your application?

it falls over in this line (in release build only - like i say debug works)

credentials = new CognitoAWSCredentials( "ap-southeast-2:extrabigthatisntgoingtobeshared", // Identity pool ID RegionEndpoint.APSoutheast2 // Region

As it throws the exception here, the credentials don't get returned to be used for anything

Does it work in release build for a normal console application?

I shall try and build one and see.

Does the error occurs in actual deployed application? If yes, have you tried running the release build via Visual Studio in an emulator?

as it is not working , i haven't deployed it outside my dev environment. I am running it from visual studio on an actual phone.

I tried a console app and that worked in both debug and release and i can call the lambda from there....so the problem remains in the xamarin release build

@bryceburrows Could you please check if there are Cognito settings in appsettings.Development.json which are not present in appsettings.Release.json? Would it be possible to share sample code solution to reproduce the issue at my end?

Thanks, Ashish

bryceburrows commented 1 year ago

I found a solution Clean

  1. remove all AWS sdks
  2. reinstall AWS sdks
  3. In Xamarin Android build options - compile with no SDK linking. rebuild and run - all works.

Cle

ashishdhingra commented 1 year ago

I found a solution Clean

  1. remove all AWS sdks
  2. reinstall AWS sdks
  3. In Xamarin Android build options - compile with no SDK linking. rebuild and run - all works.

Cle

@bryceburrows Thanks for your reply. Good to hear that it works for you. Closing this issue.

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

santoshParihar commented 4 months ago

@ashishdhingra Hi, I am getting the same exception :

{
  "message": "The type initializer for 'Amazon.CognitoIdentity.AmazonCognitoIdentityConfig' threw an exception.",
  "stackTrace": "LogCapture.GetStackTrace ",
  "type": "Exception"
},

I am using ASW SDK for Unity and used s3 package. 
I integrated it into Unity Editor, It works perfectly. I can send the file to s3.

When I create an Android build/ iOS build, It throws the above error and hence can't send the file to s3. 

I am using IL2CPP , Unity ver 2020.3.48f1 ,  .Net ver : 2.0

more detail : 

2024-03-19 12:58:22.042 17823-17958/com.winzo.gold.stag E/Unity: ArgumentException: Object of type 'System.Object[]' cannot be converted to type 'UnityEngine.AndroidJavaObject[]'. at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00000] in <00000000000000000000000000000000>:0 at System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00000] in <00000000000000000000000000000000>:0 at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0 at Amazon.Util.Internal.AmazonHookedPlatformInfo.Init () [0x00000] in <00000000000000000000000000000000>:0 at Amazon.Util.Internal.AmazonH

2024-03-19 12:58:22.179 17823-17958/com.winzo.gold.stag E/Unity: MissingMethodException: Default constructor not found for type Amazon.Util.Internal.PlatformServices.NetworkReachability at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0 at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0 at Amazon.Util.Internal.PlatformServices.ServiceFactory..ctor () [0x00000] in <00000000000000000000000000000000>:0 at Amazon.Util.Internal.PlatformServices.ServiceFactory..cctor () [0x00000] in <00000000000000000000000000000000>:0 at Amazon.Util.Internal.InternalSDKUtils.BuildUserAgentString (System.String serviceSdkVersion) [0x00000] in <00000000000000000000000000000000>:0 at Amazon.CognitoIdentity.AmazonCognitoIdentityConfig..cctor () [0x00000] in <00000000000000000000000000000000>:0 at Amazon.CognitoIdentity.AmazonCognitoIdentityClient..ctor (Amazon.Runtime.AWSCredentials credentials, Amazon.RegionEndpoint region) [0x00000]