Page 14 - MSDN Magazine, February 2018
P. 14

Figure 5 The function.json File for GetUserScores
Figure 6 The function.json File for GetGlobalScores
"bindings": [ {
"authLevel": "anonymous", "name": "req",
"type": "httpTrigger", "direction": "in",
"route": "GetUserScores/{userId}"
}, {
"name": "$return", "type": "http", "direction": "out"
}, {
"type": "documentDB",
"name": "documents",
"databaseName": "CookieBinge",
"collectionName": "Binges",
"sqlQuery": "SELECT TOP {top} c.score,c.worthit,c.deviceName,
c.dateTime FROM c WHERE c.userId={userId} ORDER by c.score DESC", "connection": "datapointscosmosdb_DOCUMENTDB",
"direction": "in"
} ],
"disabled": false }
{
"bindings": [
{
"authLevel": "anonymous", "name": "req",
"type": "httpTrigger", "direction": "in"
}, {
"name": "$return", "type": "http", "direction": "out"
}, {
"type": "documentDB",
"name": "documents",
"databaseName": "CookieBinge", "collectionName": "Binges",
"connection": "datapointscosmosdb_DOCUMENTDB", "direction": "in",
"sqlQuery": "SELECT TOP 5 c.userName, c.score,c.worthit,c.deviceName, c.dateTime FROM c ORDER by c.score DESC"
} ],
"disabled": false }
The first binding is the httpTrigger, noted in its type property. Note that all of the rest of its settings are described by the other properties the authLevel, name, direction and the route. Next you can see the http output binding and, finally, the input binding with all of the settings I specified in the form.
Now that you have a better understanding of all of the puzzle pieces of the Function, you don’t really need to go through the forms if you don’t want to. You can just create the function.json file directly and that’s what I’ll do for the second Function. You still need to add the new function to the Function app, so do that, again using a C# HttpTrigger template, and call it GetGlobalScores.
Understanding and applying the bindings in their raw format can certainly save you a lot of time.
But this time, rather than going to the Integration section, open the function.json file in the View Files pane and replace it with the code in Figure 6. Note that the sqlQuery value is wrapped here in the listing, but that string should be on one line.
Because this request doesn’t need to pass in an id, there’s no route on the httpTrigger binding as with the first Function. The output binding is the same as earlier. The only difference with the input binding is that there’s a different query that doesn’t filter on a userId.
Understanding and applying the bindings in their raw format can certainly save you a lot of time. Taking the explicit path of first using the forms to input the settings was beneficial, however, as it helped me comprehend the settings as I was first learning about them. In fact, now that you see how the configuration is stored, you are very well prepared to build your Azure Functions in Visual Studio or Visual Studio Code, rather than in the portal.
Now for the Function code, which is nearly identical to the code for the first Function, except that it doesn’t take in the userId
binding as its first parameter. Here’s the modified signature of the Run method:
public static HttpResponseMessage Run(
HttpRequestMessage req, IEnumerable<dynamic> documents,
TraceWriter log)
I manually modified some of the test data in my Cosmos DB Binges collection to be sure there were a number of different userId values before running the new GetGlobalUserScores Func- tion to validate that everything was working correctly.
Linking the UWP App with the Azure Functions
With the three Azure Functions and the Cosmos DB document database for storing and retrieving user scores in hand, the final installment of this series will return to the UWP app to integrate those Functions. Recall that the UWP app currently uses Entity Framework Core 2 to store the user scores locally onto whatever Windows 10 device where the game is being played. In the next iteration, in addition to the local storage, a user’s scores (with their permission) will be sent to the StoreScores Azure Function to store in the cloud. And users will be able to retrieve a list of their own top scores across all the devices on which they’ve played, as well as to see the top scores of all players around the globe. The app will call the Functions created here in order to report that data to the user.
Please note that I’ll probably shut down my own demo Func- tions at the URLs noted earlier in this article. They’re hosted on my Visual Studio Subscription account, which is for testing purposes and has a spending limit. n
Julie lerman is a Microsoft Regional Director, Microsoft MVP, software team coach and consultant who lives in the hills of Vermont. You can find her presenting on data access and other topics at user groups and conferences around the world. She blogs at the thedatafarm.com/blog and is the author of “Programming Entity Framework,” as well as a Code First and a DbContext edition, all from O’Reilly Media. Follow her on Twitter: @julielerman and see her Pluralsight courses at juliel.me/PS-Videos.
Thanks to the following Microsoft technical expert for reviewing this article: Jeff Hollan
10 msdn magazine
Data Points


































































































   12   13   14   15   16