question

Jakub Smekal avatar image
Jakub Smekal asked

ExecuteFunction local debugging error

Hello,

I created an Azure function using this tutorial - Quickstart: Writing a PlayFab CloudScript using Azure Functions.

It works when published to Azure, but local debugging causes an error.

Local code:

PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest()
        {
            Entity = new PlayFab.CloudScriptModels.EntityKey()
            {
                Id = PlayFabSettings.staticPlayer.EntityId,
                Type = PlayFabSettings.staticPlayer.EntityType,
            },
            FunctionName = "LoadPlayerData",
            FunctionParameter = new Dictionary<string, object>() { { "inputValue", "Test" } },
            GeneratePlayStreamEvent = false
        }, (ExecuteFunctionResult result) =>
        {
            if (result.FunctionResultTooLarge ?? false)
            {
                Debug.Log("This can happen if you exceed the limit that can be returned from an Azure Function, See PlayFab Limits Page for details.");
                return;
            }
            Debug.Log($"The {result.FunctionName} function took {result.ExecutionTimeMilliseconds} to complete");
            Debug.Log($"Result: {result.FunctionResult.ToString()}");
        }, (PlayFabError error) =>
        {
            Debug.Log($"Opps Something went wrong: {error.GenerateErrorReport()}");
        });

Server code:

[FunctionName("LoadPlayerData")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

            dynamic args = context.FunctionArgument;

            var message = $"Hello {context.CallerEntityProfile.Lineage.MasterPlayerAccountId}!";
            log.LogInformation(message);

            dynamic inputValue = null;
            if (args != null && args["inputValue"] != null)
            {
                inputValue = args["inputValue"];
            }

            log.LogDebug($"HelloWorld: {new { input = inputValue } }");

            return new { messageValue = message };
        }

Output from local debugging:

Executed 'ExecuteFunction' (Failed, Id=0548fbfc-5141-4bc0-8a69-fd8fbbbe852a, Duration=1380ms)
System.Private.CoreLib: Exception while executing function: ExecuteFunction. PlayFabAllSDK: Invalid JSON string (Parameter 'json').

Any help would be appreciated

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jakub Smekal avatar image
Jakub Smekal answered

Unity throws different error - HTTP/1.1 500 Internal Server Error

But I managed to find out that the problem is in deserializing the host.json file. For some reason the string is invalid. I fixed it by replacing

hostFileContent = ReadAllFileText(currDirHostFile);

by

hostFileContent = File.ReadAllText(currDirHostFile);

in the GetHostRoutePrefix() function in ExecuteFunction.cs

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gosen Gao avatar image
Gosen Gao answered

It seems like you are using Unity to call ExecuteFunction. If the error comes from Unity too, it should be an encoding issue, you can refer to Invalid JSON string error when executing ExecuteFunction - Playfab Community.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.