Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke Lambda function failed with Unity 2017.1.0f3 #696

Closed
nzmkey opened this issue Jul 12, 2017 · 17 comments
Closed

Invoke Lambda function failed with Unity 2017.1.0f3 #696

nzmkey opened this issue Jul 12, 2017 · 17 comments

Comments

@nzmkey
Copy link

nzmkey commented Jul 12, 2017

After upgrade to Unity 2017.1.0, all Lambda function failed to invoke.
The LambdaExample provided with the plugin aws-sdk-unity_3.3.121.0 also failed.

Expected Behavior

Call function via InvokeAsync should work without exception

Current Behavior

With the LambdaExample, clicking the Invoke button cause an exception:

InvalidOperationException: Cannot override system-specified headers
UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/UnityWebRequest/WebRequestBindings.gen.cs:479)
UnityEngine.WWW..ctor (System.String url, System.Byte[] postData, System.Collections.Generic.Dictionary`2 headers) (at C:/buildslave/unity/build/Runtime/WebRequestWWW/UWRWWW.cs:62)
Amazon.Runtime.Internal.UnityMainThreadDispatcher+<InvokeRequest>d__7.MoveNext ()
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Amazon.Runtime.Internal.UnityMainThreadDispatcher:ProcessRequests()
Amazon.Runtime.Internal.UnityMainThreadDispatcher:Update()

Possible Solution

Not a solution but a workaround mention in #643
Add below after UnityInitializer is attached to gameobject

Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;

Steps to Reproduce (for bugs)

Requirement:
Unity 2017.1.0f3
aws-sdk-unity_3.3.121.0

Setup LambdaExample provided in the plugin (Setup Cognito and Region)
Run scene
Enter function name and press the invoke button

Your Environment

Unity 2017.1.0f3
aws-sdk-unity_3.3.121.0
Windows 10

@gokarnm gokarnm added the Unity label Jul 17, 2017
@paulogodinho
Copy link

Related thread on Unity Forum:
https://forum.unity3d.com/threads/www-exception-in-2017-1-but-not-5-6.476528/

In 2017.1 WWW class was rewritten to be a wrapper on top of UnityWebRequest. This imposes a more strict rules for headers you can set, specifically not allowing to set headers that are either set automatically of can cause problems.
The Content-Type is set automatically to correct value and should not be set manually. Even in old WWW implementation setting this header was asking for trouble.

@nzmkey Possible Solution worked just fine for my DynamoDB calls, thank you for reporting and providing a solution.

@ddough44
Copy link

How can I use AWS API Gateway with latest Unity version? Since headers cannot be modified, I cannot use WWW class. IT fails because I'm trying to add the headers such as "X-Amz-Security-Token", "X-Amz-Content-SHA256", "Authorization", etc..

@wetpwnage
Copy link

Possible Solution***

Not a solution but a workaround mention in #643
Add below after UnityInitializer is attached to gameobject

Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;

I am not sure how to apply this fix exactly. My code......
public void Awake()
{
UnityInitializer.AttachToGameObject(this.gameObject);

 ConfirmAmazonCredentials();

}

private DynamoDBContext Context
{
get
{
if (_context == null)
_context = new DynamoDBContext(_ddbClient);

        return _context;
    }
}

public void GetCognitoPoolRegion()
{
_CognitoPoolRegion = RegionEndpoint.GetBySystemName(CognitoPoolRegion);
}
public void GetDynamoRegion()
{
_DynamoRegion = RegionEndpoint.GetBySystemName(DynamoRegion);
}
public void GetCredentials()
{
if (credentials == null)
{
credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoPoolRegion);
}
}
public void GetDDBClient()
{
_ddbClient = new AmazonDynamoDBClient(credentials, _DynamoRegion);
}

public void ConfirmAmazonCredentials()
{

    GetCognitoPoolRegion();
    
    GetDynamoRegion();
    
    GetCredentials();
    
    GetDDBClient();

    canGetPwnCoins = true;
}

Just like others, everything worked perfectly fine before the 2017 update...

I tried adding "Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;" to several different places throughout the code but haven't found the right spot yet.

@supertommy
Copy link

The #643 workaround doesn't appear to work in 2017.2 or 2017.1.1p3. The problematic header is Content-Length which UnityWebRequest will automatically add:

The content-length header will be automatically populated based on the contents of the attached DownloadHandler, if any, and cannot be set to a custom value.

https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.SetRequestHeader.html

I'll have to figure out how to build the dlls to test a fix but the solution should be straight-forward (remove Content-Length from the headers for Unity). I just started using the AWSSDK so I could be wrong.

@paulogodinhoaq
Copy link

Is it safe to say that the AWS SDK is not working outside of unity 5.6.x+?

@Yukichu
Copy link

Yukichu commented Oct 23, 2017

Still no update on this? Unity is on 2017.2 now, been out for a while. Why is SDK still broken?

@paulogodinhoaq
Copy link

Any input from the dev team would be a nice addition. My projects are stuck at 5.6 because the framework simply dropped support for unity.

@Yukichu
Copy link

Yukichu commented Oct 23, 2017

I used the workaround for a Windows build and it appeared to work with Unity 2017.2.0f3 in editor and with compiled Windows x64 build. I cannot confirm for other build types. Still disappointing no official fix.

@supertommy
Copy link

I implemented a hack/workaround that seems pretty solid by intercepting the request that gets queued into the UnityRequestQueue. Requests get processed in the Update method in UnityMainThreadDispatcher. If you have a MonoBehaviour that sits above UnityMainThreadDispatcher in the AWSPrefab then your component's Update will get called first.

UnityRequestQueue happens to be a singleton so with some reflection, you can get a reference to the private requests Queue and then use Peek() to get the next request and remove the bad headers before it gets processed in UnityMainThreadDispatcher.

Obviously going the route of using reflection to get at a private property is not good practice but I need it to work now... ¯\_(ツ)_/¯

@chenchen2015
Copy link

chenchen2015 commented Nov 9, 2017

Here's a temporary fix for Unity 2017.1 versions. Presumably, this should also work in Unity 2017.2 versions if you have the same bug, but I haven't tested. As mentioned above, all you need is to manually override the target HttpClient interface with UnityWebRequest. To do this, the best way I have found is to add it right after UnityMainThreadDispatcher awakes.

// UnityMainThreadDispatcher.cs
public void Awake()
{
      _logger = Logger.GetLogger(this.GetType());
      // Call the method to process requests at a regular interval.
      _nextUpdateTime = Time.unscaledTime;
      _nextUpdateTime += _updateInterval;

      // Override HttpClient here
     Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;
}

Simply override the HttpClient in the Awake() method in UnityMainThreadDispatcher.cs code, and compile again. Now everything should be back working again!

Update
I have uploaded some compiled DLL assemblies here on Google Drive. This is based on AWS SDK v3.3.186.1 release.

@dave-colenso
Copy link

Hi @chenchen2015 - Could you please supply your dll with this fix?

@chenchen2015
Copy link

@dave-colenso I have updated my answer to include the compiled assemblies. Hope it helps!

@mgrogin
Copy link

mgrogin commented Dec 3, 2017

@chenchen2015 Is there a step by step on how to Compile the DLL assemblies to solve this issue (I tried using your dlls but it's not the same version as the latest version)?
I'm getting build errors:

  1. I'm using Unity 2017 and visual studio 2017 on windows 10
  2. I opened aws-sdk-net\sdk\AWSSDK.Unity.sln
    It seems that visual studio doesnt recognize Unity Engine properly:
  • MonoBehaviour cannot be found
  • using UnityEngine; is is greyed out and it says Using directive is unnecessary
    under references UnityEngine's path is C:\Program Files\Unity\Editor\Data\PlaybackEngines\windowsstandalonesupport\Managed\UnityEngine.dll
    (I can't add refferences or change that for some reason but the dll does exist in that location)

Thank you!


1>------ Build started: Project: AWSSDK.Core.Unity, Configuration: Release Any CPU ------ 1> Restoring NuGet packages... 1> To prevent NuGet from downloading packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages'. 1> Feeds used: 1> C:\Users\Administrator\AppData\Local\NuGet\Cache 1> C:\Users\Administrator\.nuget\packages\ 1> https://api.nuget.org/v3/index.json 1> 1> All packages listed in C:\projects\aws-sdk-net\sdk\src\Core\packages.config are already installed. 1>C:\projects\aws-sdk-net\sdk\src\Core\Amazon.Util\Internal\_unity\InternalSDKUtils.unity.cs(26,7,26,23): warning CS0105: The using directive for 'System.Threading' appeared previously in this namespace 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(188,10,188,29): error CS0246: The type or namespace name 'MonoPInvokeCallbackAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(188,10,188,29): error CS0246: The type or namespace name 'MonoPInvokeCallback' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(212,10,212,29): error CS0246: The type or namespace name 'MonoPInvokeCallbackAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(212,10,212,29): error CS0246: The type or namespace name 'MonoPInvokeCallback' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(230,10,230,29): error CS0246: The type or namespace name 'MonoPInvokeCallbackAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(230,10,230,29): error CS0246: The type or namespace name 'MonoPInvokeCallback' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(248,10,248,29): error CS0246: The type or namespace name 'MonoPInvokeCallbackAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\ThirdParty\_unity\iOS4Unity\Foundation\Callbacks.cs(248,10,248,29): error CS0246: The type or namespace name 'MonoPInvokeCallback' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\Amazon.Util\_unity\IO\Internal\NetworkInfo.cs(28,23,28,42): error CS0246: The type or namespace name 'NetworkReachability' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\_unity\UnityInitializer.cs(36,37,36,50): error CS0246: The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\_unity\UnityInitializer.cs(51,47,51,57): error CS0246: The type or namespace name 'GameObject' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\Amazon.Runtime\Pipeline\_unity\UnityMainThreadDispatcher.cs(34,46,34,59): error CS0246: The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\Amazon.Runtime\Pipeline\_unity\UnityWebRequestWrapper.cs(188,16,188,30): error CS0246: The type or namespace name 'AsyncOperation' could not be found (are you missing a using directive or an assembly reference?) 1>C:\projects\aws-sdk-net\sdk\src\Core\Amazon.Runtime\Internal\Transform\_unity\UnityWebResponseData.cs(122,37,122,40): error CS0246: The type or namespace name 'WWW' could not be found (are you missing a using directive or an assembly reference?) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

@chenchen2015
Copy link

@mgrogin I think you might be missing the reference of UnityEngine.dll. Go to the Reference section of your project under Solution Explorer and find if there's a warning sign on the UnityEngine.dll reference. If so, it means the existing dll reference did not work and you need to find and add UnityEngine.dll reference manually.

The dll file should be located at [Unity Install Path]\Editor\Data\Managed folder. If there's any other Unity or Mono related assembly file that are missing, you should be able to locate them here as well. Now simply right click Reference and select Add Reference then add the correct dll to your project. You should be good to go.

Hope this helps.

@mgrogin
Copy link

mgrogin commented Dec 4, 2017

Thanks for your answer @chenchen2015.
Unfortunately that doesn't seem to be the problem - as I wrote before there is a reference to "C:\Program Files\Unity\Editor\Data\PlaybackEngines\windowsstandalonesupport\Managed\UnityEngine.dll" and the file is there.
For some reason it doesn't see the Unity classes like MonoBehaviour in that dll.

I wonder if it has something to do with the fact that I have Unity 2017 installed and not 5

There are no warning signs on the refferences

Thanks.

@oismaelash
Copy link

oismaelash commented Jun 23, 2018

Possible Solution
Not a solution but a workaround mention in #643
Add below after UnityInitializer is attached to gameobject

Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;

Work fine !!

@github-actions
Copy link

We have noticed this issue has not recieved attention in a year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jul 10, 2020
@github-actions github-actions bot added closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests