question

Gustavo Dorte Carriel de Lara avatar image
Gustavo Dorte Carriel de Lara asked

How to debug the context on an Azure Function called by a rule?


In general, I'm having a tough time debugging the HttpRequest of an Azure Function when it's being called by a rule. I've been considering calling an external API and giving them the json just so I can read it! Let me show you what I've been struggling with:

My code so far:

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

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

            var settings = new PlayFabApiSettings
            {
                TitleId = context.TitleAuthenticationContext.Id,
                DeveloperSecretKey = "-----" //this is hardcoded
            };

            PlayFabAuthenticationContext authContext = new PlayFabAuthenticationContext()
            {
                EntityToken = context.TitleAuthenticationContext.EntityToken
            };

            PlayFabServerInstanceAPI sapi = new PlayFabServerInstanceAPI(settings, authContext);

            PlayFab.PlayFabResult<PlayFab.ServerModels.GrantCharacterToUserResult> result = await sapi.GrantCharacterToUserAsync(new PlayFab.ServerModels.GrantCharacterToUserRequest()
            {
                CharacterName = "test", //I wanted to use the purchased ItemId here
                CharacterType = "Commander",
                PlayFabId = "----" //This is hardcoded
            });

            log.LogInformation("Testing log.LogInformation");

            if (result.Error != null)
            {
                return new BadRequestObjectResult("Oops");
            }
            else
            {
                return new OkObjectResult("All good!");
            }
        }
    }
}

For this code, I need to know how to get the PlayerId and the ItemId of the item that was added to the player's inventory when that rule was called.

Being able to debug the context would help me a lot! I can't use PostMan for that because the HttpRequest is built by PlayFab when the rule is triggered.I also tried returning data and using the log, to no avail.

I really just want to see what's on that json string.

Any tips?

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.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

The CS2AFHelperClasses.cs has the PlayerPlayStreamFunctionExecutionContext, which is used for Azure Function called by a rule. You can refer to the code below to get the PlayFabId and ItemId.

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

log.LogInformation(context.PlayerProfile.PlayerId);
log.LogInformation(context.PlayStreamEventEnvelope.EventData);

JObject jsonO = JObject.Parse(context.PlayStreamEventEnvelope.EventData);
string itemId = jsonO["ItemId"].ToString();
log.LogInformation(itemId);
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.

Gustavo Dorte Carriel de Lara avatar image Gustavo Dorte Carriel de Lara commented ·

That was beyond helpful, @Gosen Gao! I cannot thank you enough!

If people out there are having problems debugging the HttpRequest, it came to me that one can do the following code:

namespace OnPurchasedCommander.Function
{
    public static class OnCommanderWasPurchased
    {
        [FunctionName("OnCommanderWasPurchased")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log)
        {
            string json = await req.ReadAsStringAsync();
            return new OkObjectResult(json);
        }
    }
}

If your event is registered at the PlayStream, you'll see the json in there!

What happened to me was that my function was taking more than 1000ms to finish and PlayFab wouldn't give me the return.
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.