Page 12 - MSDN Magazine, March 2018
P. 12

I won’t go deeply into the code for
how a user’s identity is managed, but
here’s a quick look. You will be able to
see all of this code in the download. I
created a new pair of Azure Functions
for user management. When a user
chooses to register to the cloud for
score tracking, one of these functions
creates a new GUID for the UserId,
stores it in a separate collection in the
CookieBinge Cosmos DB database
and returns the GUID to the UWP
app. The UWP app then uses EF Core 2
to store that UserId into a new table in
the local database. Part of that GUID
is displayed to users on their account
page, as shown in Figure 1. When
a user plays the CookieBinge game
on another device, they can acquire
the full GUID by sending the partial
GUID that’s available on any other
device they’ve already registered to
another Azure Function. That func­
tion returns the full GUID and the
app will then store that UserId on
the current device. In this way, the
user can post scores from any of their
devices to the cloud, always using the
same UserId. Additionally, the appli­
cation can use that same UserId to
retrieve the scores from all of their
devices from the cloud. An Account­
Service.cs class has functionality for
the local interactions related to the
UserId, including storing and retrieving the UserId from the local database. I came up with this pattern on my own and patted myself on the back, feeling so clever, even though I could probably have leveraged an existing framework.
GetUserTopScores is the method in CloudServices that calls out to the GetUserScores function. Like the previous method, it calls the CallCookieBingeFunctionAsync method, again expecting
An AccountService.cs class has functionality for the local interactions related to the user ID, including storing and retrieving the user ID from the local database.
the returned type to be a list of ScoreViewModel objects. I’m again passing in just a single parameter, which is not only the name of the func­ tion, but the full string that should get appended to the base URL. I’m using string interpolation to combine the function name with the results of the AccountService.AccountId property:
public async Task<List<ScoreViewModel>> GetUserTopScores()
{
}
Calling an Azure Function
That Expects JSON Content
in the Request
The final Azure Function, Store­ Scores, gives me the opportunity to show you how to append JSON to an HttpRequest. StoreScores takes a JSON object and stores its data into the Cosmos DB database. Figure 2 serves as a reminder of how I tested the function in the Azure Portal by send­ ing in a JSON object that follows the expected schema.
To match that schema in the UWP app I created a data transfer object (DTO) struct named StoreScoreDto, which helps me create the JSON body
for the request. Here’s the CloudService.SendBingeToCloudAsync method, which takes the Binge data resulting from game play and sends it to the Azure Function with the aid of the same CallCookie­
BingeFunctionAsync method I used to call the other two functions:
public async void SendBingeToCloudAsync(int count, bool worthIt, DateTime timeOccurred)
{
var storeScore = new StoreScoreDto(AccountService.AccountId,
"Julie", AccountService.DeviceName,
timeOccurred, count, worthIt); var jsonScore = JsonConvert.SerializeObject(storeScore);
var jsonValueScore = JsonValue.Parse(jsonScore);
var results = await CallCookieBingeFunctionAsync<string>("StoreScores",
jsonValueScore); }
SendBingeToCloudAsync begins by taking in the relevant data about the Binge to be stored—the count of cookies consumed, whether or not the binge was worth it and when it occurred. I then create a StoreScoreDto object from that data and use JsonConvert again, this time to serialize the StoreScoreDto into a JSON object. The next step is to create a JsonValue, as I explained earlier, a spe­ cial type in the Windows.Json.Data namespace. I do that using the JsonValue.Parse method, passing in the JSON object represented by jsonScore. The resulting JsonValue is the format required to send the JSON object along with the HTTP request. Now that I’ve got
Figure 2 The Azure Portal View of the StoreScores Function Being Tested with a JSON Request Body
var results = await CallCookieBinge­ FunctionAsync<List<ScoreViewModel>> ($"GetUserScores\\{AccountService.
AccountId}"); return results;
8 msdn magazine
Data Points


































































































   10   11   12   13   14