Skip to content

Commit

Permalink
Add local debugging instructions
Browse files Browse the repository at this point in the history
Update readme.md to include instructions for how to get local debugging
working in C# Azure Functions apps.

Minor updates to ExecuteFunction.cs
  • Loading branch information
MGudgin committed Mar 3, 2020
1 parent d614e78 commit 066ec97
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"playfab.loginId": "[email protected]"
}
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,67 @@ Azure Functions related to the PlayFab developer experience
This repository contains various Azure Functions that contribute to the PlayFab developer experience.
Specifically, various implementation of ExecuteFunction, which are used to support local debugging of
Azure Functions when using CloudScript.

Setting up local debugging involves 2 broad steps;

* Adding an implementation of ExecuteFunction to your local Azure Functions app
* Adding a settings file to tell the PlayFab SDK to call that local implementation from your game.

Once those steps are complete, you can run your local Azure Functions app under
the debugger (e.g. in VS Code or Visual Studio), set your breakpoints and run
your game client.

The rest of this document provides details on the above two steps.

# Local implementation of ExecuteFunction

## For C# Azure Functions apps

To get the local implementation of ExecuteFunction set up in your C# Azure Functions app, add the [ExecuteFunction.cs](https://github.com/PlayFab/pf-af-devfuncs/blob/master/csharp/ExecuteFunction.cs) file to your local Azure Functions app.

# Required environment variables for local implementation of ExecuteFunction

Next, add two settings to your local.settings.json file;

| Name | Value |
|--|--|
| PLAYFAB_TITLE_ID | Your title ID, in hex form |
| PLAYFAB_DEV_SECRET_KEY | Secret key for your title |

For example;

```
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "...",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"PLAYFAB_TITLE_ID": "B55D",
"PLAYFAB_DEV_SECRET_KEY": "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMM"
}
}
```

# Configuring PlayFab SDK to call local ExecuteFunction implementation

To tell the PlayFab SDK to redirect ExecuteFunction API calls to your local implementation, add a file called playfab.local.settings.json to one of two places;

* The temporary directory on your machine
* TMPDIR environment variable on Linux/Mac
* TEMP environment variable on Windows
* The directory of your game executable.

The content of the file should be as follows;

```
{ "LocalApiServer": "http://localhost:7071/api/" }
```

To stop local redirects and make ExecuteFunction call the PlayFab API server simply delete the playfab.local.settings.json file.

The above is supported in the following SDKs;

* [PlayFab C# SDK](https://github.com/PlayFab/CSharpSDK)
* [PlayFab Unity SDK](https://github.com/PlayFab/UnitySDK)
* [Unreal 4 Marketplace PlugIn for PlayFab](https://github.com/PlayFab/UnrealMarketplacePlugin)
49 changes: 25 additions & 24 deletions csharp/ExecuteFunction.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
// Copyright (C) Microsoft Corporation. All rights reserved.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using PlayFab.Internal;
using PlayFab.Json;
using PlayFab.ProfilesModels;

namespace PlayFab.AzureFunctions
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using PlayFab.Internal;
using PlayFab.Json;
using PlayFab.ProfilesModels;

public static class LocalExecuteFunction
{
private const string DEV_SECRET_KEY = "PLAYFAB_DEV_SECRET_KEY";
private const string TITLE_ID = "PLAYFAB_TITLE_ID";
private const string CLOUD_NAME = "PLAYFAB_CLOUD_NAME";
private static readonly HttpClient httpClient = new HttpClient();

/// <summary>
/// A local implementation of the ExecuteFunction feature. Provides the ability to execute an Azure Function with a local URL with respect to the host
/// of the application this function is running in.
/// </summary>
/// <param name="functionRequest">The execution request</param>
/// <param name="httpRequest">The HTTP request</param>
/// <param name="log">A logger object</param>
/// <returns>The function execution result(s)</returns>
Expand Down Expand Up @@ -87,7 +87,7 @@ await httpClient.PostAsync(azureFunctionUri, functionRequestContent))
{
FunctionName = execRequest.FunctionName,
FunctionResult = await ExtractFunctionResult(functionResponseContent),
ExecutionTimeMilliseconds = (int) executionTime,
ExecutionTimeMilliseconds = (int)executionTime,
FunctionResultTooLarge = false
};

Expand Down Expand Up @@ -115,7 +115,8 @@ await httpClient.PostAsync(azureFunctionUri, functionRequestContent))
/// </summary>
/// <param name="callerEntityToken">The entity token of the entity profile being fetched</param>
/// <returns>The entity's profile</returns>
private static async Task<EntityProfileBody> GetEntityProfile(string callerEntityToken) {
private static async Task<EntityProfileBody> GetEntityProfile(string callerEntityToken)
{
// Construct the PlayFabAPI URL for GetEntityProfile
var getProfileUrl = GetServerApiUri("/Profile/GetProfile");

Expand Down Expand Up @@ -354,7 +355,7 @@ private static async Task<string> DecompressHttpBody(HttpRequest request)
{
using (var gZipStream = new GZipStream(responseStream, CompressionMode.Decompress, false))
{
byte[] buffer = new byte[4*1024];
byte[] buffer = new byte[4 * 1024];
using (var output = new MemoryStream())
{
int read;
Expand Down Expand Up @@ -413,7 +414,7 @@ private static byte[] CompressResponseBody(object responseObject, HttpRequest re
private static byte[] StreamToBytes(Stream input)
{
input.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[4*1024];
byte[] buffer = new byte[4 * 1024];
using (var output = new MemoryStream())
{
int read;
Expand Down

0 comments on commit 066ec97

Please sign in to comment.