question

Raf avatar image
Raf asked

Is there a way to create FunctionContext in .NET 7?

Hi!

We're upgrading our Azure Functions from v3 to v4 and we're now using .NET 7.0. The problem now is that this version of .NET uses HttpRequestData instead of HttpRequestMessage in the receiving end of the HttpTrigger function, as shown in the example below:

 [Function("TEST_FUNCTION")]
         public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
         {
             _logger.LogInformation("C# HTTP trigger function processed a request.");
    
             var response = req.CreateResponse(HttpStatusCode.OK);
             response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
    
             response.WriteString("Welcome to Azure Functions!");
    
             return response;
         }

I use to use the method Create in FunctionContext from PlayFab.Plugins.CloudScript but the Create function only has HttpRequestMessage as an input parameter (which I believe it's no longer supported in .NET 7) as shown below from PlayFab's SDK:

 namespace PlayFab.Plugins.CloudScript
 {
     public class FunctionContext<TFunctionArgument>
     {
         protected FunctionContext();
    
         public PlayFabApiSettings ApiSettings { get; }
         public PlayFabAuthenticationContext AuthenticationContext { get; set; }
         public EntityProfileBody CallerEntityProfile { get; set; }
         public TFunctionArgument FunctionArgument { get; set; }
         public string CurrentPlayerId { get; set; }
    
         [AsyncStateMachine(typeof(FunctionContext<>.<Create>d__21))]
         public static Task<FunctionContext<TFunctionArgument>> Create(HttpRequestMessage request);
     }
 }

Is there anything obvious that i am missing? How does one populate the FunctionContext in .NET 7 without using HttpRequestMessage ?

Thanks in advance!! :) Raf

apissdksCloudScript
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

·
Xiao Zha avatar image
Xiao Zha answered

The PlayFab CloudScript Plugin is outdated. You can refer to PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Learn to use "FunctionExecutionContext" in CS2AFHelperClasses.cs to get the context. And in my test, CS2AFHelperClasses.cs works fine in dotnet 7.0.

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.

Raf avatar image Raf commented ·

Thanks!

I wish I saw this before but I finally managed to get it to work myself! I'm honestly not even sure how the CS2AFHelperClasses would work as my HttpRequest "req" parameter would just come as null after I switched to .NET 7.

The way I made it work was to use the HttpRequestData class instead and wrapping my own FunctionContext by stripping the data in "req.FunctionContext.BindingContext.BindingData".

I'm sure that is not the proper nor the best way to do it but it worked and after a full day of pain I'm now a happy camper.

1 Like 1 ·

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.