question

NJ avatar image
NJ asked

Azure Functions cloud script api PlayFabAuthenticationContext and FunctionContext

Hello, I've a simple azure function to get a sharedgroup from playfab server api:

     [FunctionName("GetSharedGroup")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req)
        {
            var context = await FunctionContext<GetSharedGroupRequest>.Create(req);
            var request = new GetSharedGroupDataRequest
            {
                AuthenticationContext = context.AuthenticationContext,
                SharedGroupId = context.FunctionArgument.SharedGroupId
            };


           var playfabServerInstanceAPI =  new PlayFabServerInstanceAPI(
                new PlayFabApiSettings
                {
                    TitleId = Environment.GetEnvironmentVariable(Constants.PLAYFAB_TITLE_ID, EnvironmentVariableTarget.Process),
                    VerticalName = Environment.GetEnvironmentVariable(Constants.PLAYFAB_CLOUD_NAME, EnvironmentVariableTarget.Process),
                    DeveloperSecretKey = Environment.GetEnvironmentVariable(Constants.PLAYFAB_DEV_SECRET_KEY, EnvironmentVariableTarget.Process)
                }
            );


            return await playfabServerInstanceAPI.GetSharedGroupDataAsync(request);
        }

FunctionContext<TFunctionArgument> is referenced from PlayFab.Plugins.CloudScript but it does not work and throws object reference not set to an instance of an object exception. I guess PlayFab.Plugins.CloudScript is outdated.

If I replace my code with this :

        [FunctionName("GetSharedGroup")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req)
        {
            var context = JsonConvert.DeserializeObject<FunctionExecutionContext<JoinMatchRequest>>(await req.Content.ReadAsStringAsync());


            var request = new GetSharedGroupDataRequest
            {
                AuthenticationContext = new PlayFabAuthenticationContext { EntityToken = context.TitleAuthenticationContext.EntityToken },
                SharedGroupId = context.FunctionArgument.SharedGroupId
            };


            var playfabServerInstanceAPI = new PlayFabServerInstanceAPI(
                 new PlayFabApiSettings
                 {
                     TitleId = Environment.GetEnvironmentVariable(Constants.PLAYFAB_TITLE_ID, EnvironmentVariableTarget.Process),
                     VerticalName = Environment.GetEnvironmentVariable(Constants.PLAYFAB_CLOUD_NAME, EnvironmentVariableTarget.Process),
                     DeveloperSecretKey = Environment.GetEnvironmentVariable(Constants.PLAYFAB_DEV_SECRET_KEY, EnvironmentVariableTarget.Process)
                 }
             );


            return await playfabServerInstanceAPI.GetSharedGroupDataAsync(request);
        }

It works, but the problem here is that as you see I am manually creating PlayFabAuthenticationContext and I pass only EntityToken, but I can not pass ClientSessionTicket because sample models don't have it. I was following this documentation and I've manually imported sample models into my project:.

https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/quickstart#playfab-cloudscript-context-variables-and-server-sdks-

Is it a best practice to use cloud script and azure functions like this or am I missing something. Also it's strange that PlayFab.Plugins.CloudScript does not work, because the official example also uses it:

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

sdksCloudScript
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

I am sorry that our samples may be out of data. We suggest you to follow the documents to build your Azure Function. It is the best practice to use CloudScript using Azure Functions.

For more information please refer to https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/quickstart

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.

NJ avatar image NJ commented ·

After I redeployed, it start working. strange, probably because I've added <DefineConstants>NETCOREAPP3_1</DefineConstants> to my csproj.

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.