question

Tommy Li avatar image
Tommy Li asked

[WIP] C# Source Generator for Azure Functions boilerplate code

I made a C# source generator for generating Azure Functions boilerplate code, so that the actual server code is more similar to the previous JS CloudScripts. It's a proof of concept, and I still need to test whether the generated code actually works on actual servers.

The repo is at https://github.com/heshuimu/PlayFabAzureFunctionTool

With the generator, I can write server code more like the previous JS CloudScript:

using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PlayFab;
using K4.AzureFunctions;

[assembly: DefaultAuthorizationLevel(DummyAuthLevel.Function)]

public class DummyClass
{
        public PlayFabServerInstanceAPI Server { get; init; }
        public string CurrentPlayerID { get; init; }

        public class DummyInput
        {
        }

        [AzureFunction(Name = "abc", AuthorizationLevel = DummyAuthLevel.Anonymous)]
        public async Task<int> DummyInstanceMethod(DummyInput input)
        {
                return 1;
        }

        [AzureFunction]
        public async Task DummVoidMethod(DummyInput input, ILogger log)
        {
        }

        [AzureFunction]
        public static void DummyStaticMethod(PlayFabServerInstanceAPI server, string currentPlayerID)
        {
        }
}

And the tool will generate the following:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using PlayFab.Plugins.CloudScript;
using PlayFab;

class GeneratedAzureFunction
{
        [FunctionName("abc")]
        public static async Task<IActionResult> abc_Generated([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
        {
                var functionContext = await FunctionContext<DummyClass.DummyInput>.Create(req);

                PlayFabServerInstanceAPI server = new(functionContext.ApiSettings, functionContext.AuthenticationContext);
                string currentPlayerID = functionContext.CurrentPlayerId;

                var result = await new DummyClass(){ Server = server, CurrentPlayerID = currentPlayerID }.DummyInstanceMethod(functionContext.FunctionArgument);

                return new OkObjectResult(result);
        }

        [FunctionName("DummVoidMethod")]
        public static async Task<IActionResult> DummVoidMethod_Generated([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log)
        {
                var functionContext = await FunctionContext<DummyClass.DummyInput>.Create(req);

                PlayFabServerInstanceAPI server = new(functionContext.ApiSettings, functionContext.AuthenticationContext);
                string currentPlayerID = functionContext.CurrentPlayerId;

                await new DummyClass(){ Server = server, CurrentPlayerID = currentPlayerID }.DummVoidMethod(functionContext.FunctionArgument, log);

                return new OkResult();
        }

        [FunctionName("DummyStaticMethod")]
        public static async Task<IActionResult> DummyStaticMethod_Generated([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log)
        {
                var functionContext = await FunctionContext.Create(req);

                PlayFabServerInstanceAPI server = new(functionContext.ApiSettings, functionContext.AuthenticationContext);
                string currentPlayerID = functionContext.CurrentPlayerId;

                DummyClass.DummyStaticMethod(server, currentPlayerID);

                return new OkResult();
        }
}
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

Thank you very much for sharing. But one thing to note, this PlayFab.Plugins.CloudScript is a beta plugin, it is out of date and will not be updated anymore. Now we recommend using PlayFab.Samples which implemented in the CS2AFHelperClasses.cs. For more information, please refer to this document.

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.

Tommy Li avatar image Tommy Li commented ·

I understand the difference between the two files. I used the former as a basis because the class in there had a lot more going on and was more convenient. I'll maintain that copy myself as needed.

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.