question

cartellonegerardo avatar image
cartellonegerardo asked

How to use a simple GetObjectsRequest with azure cloudscripts?

Previously after some struggling I had managed to make a getObject and a setObject function work in java-script cloud script but I was advised to make them using azure cloud script instead. But I'm struggling to make these work too.

Here is my attempt at doing the function.

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PlayFab;
using PlayFab.Samples;
using PlayFab.Json;
using PlayFab.DataModels;


namespace My.Function
{


public static class GetChat
    {


[FunctionName("getChat")]
        public static async Task<IActionResult> getChat(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
          
            log.LogInformation("C# getChat function processed a request.");
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            var args = context.FunctionArgument;
            var entityProfile = context.CallerEntityProfile;

            var getObjectRequest = new GetObjectsRequest
            {
                Entity = ClassConverter<PlayFab.ProfilesModels.EntityKey, PlayFab.DataModels.EntityKey>.Convert(entityProfile.Entity)
            };
           
            
            var dataApi = new PlayFabDataInstanceAPI(context.FunctionArgument);
            
            var getObjectsResponse = await dataApi.GetObjectsAsync(getObjectRequest);
            var getObjectsResult = getObjectsResponse.Result.Objects;

            return new OkObjectResult(new
            {
                profile = entityProfile,
                getResult = getObjectsResult
            });
        }
}

 public class ClassConverter<SourceClass, TargetClass>
    {
        public static TargetClass Convert(SourceClass input)
        {
            var json = PlayFabSimpleJson.SerializeObject(input);
            return PlayFabSimpleJson.DeserializeObject<TargetClass>(json);
        }
    }


}

I inspired my self from the MakeEntityAPICall function in these sample functions:

https://github.com/PlayFab/PlayFab-Samples/blob/master/Samples/CSharp/AzureFunctions/ExampleFunctions/Handlers.cs

but i read somewhere these samples where outdated and I should avoid using PlayFab.Plugins.CloudScript;

so I changed the

var context=await FunctionContext<dynamic>.Create(req); 

in to

FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

from the CS2AFHelperClasses.cs script.

and few other things for it to compile.

Currently I get an error which I traced to this this simple variable call that causes the error: entityProfile.Entity

here is the terminal output in vs code:

[2022-04-25T18:31:50.498Z] Executed 'getChat' (Failed, Id=5d8f8068-8777-4c22-9bcd-7d1a53ca93d4, Duration=85ms)[2022-04-25T18:31:50.499Z] System.Private.CoreLib: Exception while executing function: getChat. PlayFabServerScipts: Object reference not set to an instance of an object.

I think I have everything properly setup in vs code since I can make simple functions with nothing in them work when testing localy:

Asides from just guess work I'm struggling to find ways to solve this.

Thanks in advanced for any help.

CloudScriptentities
10 |1200

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

1 Answer

·
Made Wang avatar image
Made Wang answered

In my test, the error appears on line 41. To instantiate the PlayFabDataInstanceAPI, you need to pass in TitleId and EntityToken, refer to the code below.

var settings = new PlayFabApiSettings
{
    TitleId = context.TitleAuthenticationContext.Id
};
var authContext = new PlayFabAuthenticationContext
{
    EntityToken = context.TitleAuthenticationContext.EntityToken
};
var dataApi = new PlayFabDataInstanceAPI(settings,authContext);

EntityProfile.Entity refers to line 37? In my tests it works fine.

5 comments
10 |1200

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

cartellonegerardo avatar image cartellonegerardo commented ·

yes it refers to line 37 actually entityProfile is null yesterday I didn't realize. I did a test with a simpler function here

[FunctionName("HttpTrigger1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            var args = context.FunctionArgument;
            var entityProfile = context.CallerEntityProfile;
	log.LogInformation("entityProfile==null? "+(entityProfile==null));
            log.LogInformation("before entityProfile.Entity");
            var a=entityProfile.Entity;
            log.LogInformation("after entityProfile.Entity");
            return new OkObjectResult("dfsdfdsf");
        }
[2022-04-26T08:47:04.672Z] entityProfile==null? True
[2022-04-26T08:47:04.673Z] before entityProfile.Entity
[2022-04-26T08:47:04.692Z] Executed 'HttpTrigger1' (Failed, Id=7c401ddf-fbe0-448b-a8f9-3f446df2502d, Duration=18ms)
...
0 Likes 0 ·
Made Wang avatar image Made Wang cartellonegerardo commented ·

Can you get other data in the context? And are you calling Azure Function via ExecuteFunction?

1 Like 1 ·
cartellonegerardo avatar image cartellonegerardo Made Wang commented ·

yes it it seems everything is null in the context

[2022-04-26T09:50:59.815Z] titleAuthentication==null? True
[2022-04-26T09:50:59.817Z] args==null? True
[2022-04-26T09:50:59.818Z] entityProfile==null? True
[2022-04-26T09:50:59.819Z] before entityProfile.Entity

here also i think i'm calling it via Executefunction here is what i'm doing i click on Execute Function Now:

0 Likes 0 ·
Show more comments
Made Wang avatar image Made Wang cartellonegerardo commented ·
0 Likes 0 ·

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.