question

Kim Strasser avatar image
Kim Strasser asked

Azure Functions: The name 'FunctionContext' does not exist in the current context

I migrated to a newer version in VS Code and now I get two errors in my Azure functions.

The namespace Plugins doesn't exist in PlayFab:

 using PlayFab.Plugins.CloudScript;

and:

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

The name 'FunctionContext' does not exist in the current context

What should I change in my code?

My Azure function looks like this:

     [FunctionName("ChangeDisplayname")]
         public static async Task<dynamic> MakeApiCall(
             [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log)
         {
             var context = await FunctionContext<dynamic>.Create(req);
             var args = context.FunctionArgument;
             var language = args["Language"];
             var desireddisplayname = args["NewDisplayname"];
             log.LogInformation($"The new displayname = {desireddisplayname}");
             var playfabid = context.CallerEntityProfile.Lineage.MasterPlayerAccountId;
             var adminApi = new PlayFabAdminInstanceAPI(context.ApiSettings, context.AuthenticationContext);
 ...
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

·
Simon Cui avatar image
Simon Cui answered

Please follow PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Learn to modify your codes. As for context models, please refer to PlayFab CloudScript using Azure Functions Context Models - PlayFab | Microsoft Learn.

You can try the sample codes as below:

  using System.Threading.Tasks;
     using Microsoft.Azure.WebJobs;
     using Microsoft.Azure.WebJobs.Extensions.Http;
     using Microsoft.AspNetCore.Http;
     using Microsoft.Extensions.Logging;
     using Newtonsoft.Json;
     using PlayFab.Samples;
     using PlayFab;
     using System;
        
     namespace My.Function
     {
         public static class myCompany
         {
                [FunctionName("ChangeDisplayname")]
              public static async Task<dynamic> MakeApiCall(
                  [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log)
              {
                  FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
                  var args = context.FunctionArgument;
                  var language = args["Language"];
                  var desireddisplayname = args["NewDisplayname"];
                  log.LogInformation($"The new displayname = {desireddisplayname}");
                  var playfabid = context.CallerEntityProfile.Lineage.MasterPlayerAccountId;
                  var adminApi = new PlayFabAdminInstanceAPI(
                     new PlayFabApiSettings
                     {
                         TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process),
                         DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process),
                     }
                  );
                  var updateNameResult = await adminApi.UpdateUserTitleDisplayNameAsync(
                     new PlayFab.AdminModels.UpdateUserTitleDisplayNameRequest {
                         DisplayName = desireddisplayname,
                         PlayFabId = playfabid
                     }
                  );
        
                  var currentDisplayName = updateNameResult.Result.DisplayName;
                  return currentDisplayName;
        
              }
         }
     }

Please don’t forget to include the CS2AFHelperClasses.cs file into your project, it contains the definition of context and implementation of PlayFab.Samples.

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.