question

doronhn avatar image
doronhn asked

Azure Functions - context return null, Scheduled Tasks faild [two questions]

Hey,

i am trying to create an azure function to run 2 API calls every day.

i have two questions that drive me crazy:

1.

i looked at the Get started and sample projects and they all had this line:

[FunctionName("GetCountPlayersInSegmentAndUpdateTitleData")]
        public static async Task<dynamic> GetCountPlayersInSegmentAndUpdateTitleData(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {

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

for me, this line return context = null.
do i need to do something else?

*** i need the context for this rows:

PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
            PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);

2. i set TitleId and PLAYFAB_DEV_SECRET_KEY manually inside the code. the function run perfectly . so i created a scheduled tasks to run daily. but when i run this task manually i got staus failed with this log:

{
    "TaskInstanceId": "DE87C6AE4A3C9D13-3",
    "TaskIdentifier": {
        "Name": "GetCountPlayersInSegmentAndUpdateTitleData",
        "Id": "DE87C6AE4A3C9D13"
    },
    "StartedAt": "2020-10-15T14:16:55.659133",
    "CompletedAt": "2020-10-15T14:17:00.2059635",
    "Status": "Failed",
    "PercentComplete": null,
    "EstimatedSecondsRemaining": null,
    "ScheduledByUserId": "89ECA9AFDF1BC51B",
    "Result": null
}

do i need to return specific object \ value from my function?

apissdksCloudScripttasks
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.

Citrus Yan avatar image Citrus Yan commented ·

Could you please share the code snippet of the function "GetCountPlayersInSegmentAndUpdateTitleData" you are using so that we can investigate further? (Please remove any sensitive info)

0 Likes 0 ·
Antonio Franchinotti avatar image Antonio Franchinotti commented ·

Check if this solves the 'null context' issue:

  1. Change 'req' type to 'HttpRequestMessage'.
  2. Use 'PlayFab.Plugins.CloudScript.FunctionContext.Create' to parse the request (comes included in the C# SDK)
 var context = await FunctionContext<dynamic>.Create(req);
0 Likes 0 ·
doronhn avatar image doronhn Antonio Franchinotti commented ·

I dont have FunctionContext.

i am working with Visual studio.

0 Likes 0 ·
untitled.png (38.3 KiB)
Antonio Franchinotti avatar image Antonio Franchinotti doronhn commented ·

I forgot that this is a little hidden, you have to install PlayFabCloudScriptPlugin

(this package is on alpha)

0 Likes 0 ·
Show more comments
doronhn avatar image
doronhn answered

i didnt fixed it, but i manage to work around it...

thanks

1 comment
10 |1200

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

Citrus Yan avatar image Citrus Yan commented ·

Could please share your implementation with the community? That should be helpful for other developers.

0 Likes 0 ·
doronhn avatar image
doronhn answered

I need this plugin for what?

For the function context?

Where its written?

Thanks

2 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.

Antonio Franchinotti avatar image Antonio Franchinotti commented ·

Yep, for the function context, you can see this working on the Azure Functions of the TicTacToe Unity Sample

0 Likes 0 ·
Antonio Franchinotti avatar image Antonio Franchinotti Antonio Franchinotti commented ·

The code is here, if you need more details smil

0 Likes 0 ·
doronhn avatar image
doronhn answered

Sure,

 public static class CountPlayersInSegmentAndUpdateTitleData
    {
        [FunctionName("GetCountPlayersInSegmentAndUpdateTitleData")]
        public static async Task<dynamic> GetCountPlayersInSegmentAndUpdateTitleData(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            log.LogDebug("Started before context");


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


            //log.LogDebug("after context",context);


            //PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
            //PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);


            //log.LogDebug("TitleID - ", context.TitleAuthenticationContext.Id);
            //log.LogDebug("Secret Key - ", Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process));


            
            PlayFabSettings.staticSettings.TitleId = "XXX";//context.TitleAuthenticationContext.Id;
            PlayFabSettings.staticSettings.DeveloperSecretKey = "XXXX"; //Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process);
            


            string NUMBER_OF_PLAYER_TODAY_TITLE_DATA_KEY = "NumberOfPlayersToday";


            GetPlayersInSegmentRequest getPlayersInSegmentRequest = new GetPlayersInSegmentRequest()
            {
                MaxBatchSize = 1,
                SegmentId = "XXXXXX"
            };


            var getPlayersInSegmentResult = await PlayFab.PlayFabServerAPI.GetPlayersInSegmentAsync(getPlayersInSegmentRequest);


            log.LogDebug("getPlayersInSegmentResult - ", getPlayersInSegmentResult);


            SetTitleDataRequest setTitleDataRequest = new SetTitleDataRequest()
            {
                Key = NUMBER_OF_PLAYER_TODAY_TITLE_DATA_KEY,
                Value = getPlayersInSegmentResult.Result.ProfilesInSegment.ToString()
            };




            var result = await PlayFab.PlayFabServerAPI.SetTitleDataAsync(setTitleDataRequest);


            if (result.Error == null)
                return "Update Done";
            else
                return "Faild";
                
        }
    }
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.

Antonio Franchinotti avatar image Antonio Franchinotti commented ·

Try with this:

https://gist.github.com/AntonioFranchinotti/7f4777d7b7a2f79e980c4d419ab8ff57

This line is super important, since you can avoid setting manually the TitleId and de DeveloperSecret.

0 Likes 0 ·
doronhn avatar image doronhn Antonio Franchinotti commented ·

now working. any chance you have skype? or email? i think you will solve this is a minute.

thanks

0 Likes 0 ·
Antonio Franchinotti avatar image Antonio Franchinotti doronhn commented ·

You have my email on my profile (antonio.franchinotti@southworks.com), I won't be fully online until Monday, but if I catch your email I will try to help you

Anyway, I think it will be best to keep this thread alive, since it might help other people and PlayFab staff can help you too.

So, what's left to fix?

0 Likes 0 ·
Show more comments

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.