Reader small image

You're reading from  .NET MAUI Projects - Third Edition

Product typeBook
Published inFeb 2024
Reading LevelN/a
PublisherPackt
ISBN-139781837634910
Edition3rd Edition
Languages
Right arrow
Authors (3):
Michael Cummings
Michael Cummings
author image
Michael Cummings

Michael Cummings is a Senior Development Engineer with Microsoft. He currently works on the Visual Studio tooling for building WPF, Xamarin.Forms, and .NET MAUI apps. He has experience as a developer and architect with concentrations in app development, design, deployment, and business process as it relates to technology. Michael has been a graphics and game programmer since the days of the TI99-4/A. He started the Boston XNA Developers Group. He has contributed to many open-source projects, including AXIOM, a .NET/Mono 3D rendering engine, and Planeshift, a 3D MMORPG. Michael also has experience with native game and graphics development (C++/DirectX) on Windows. Michael lives in Lexington, MA with his wife and their dog. When not working with technology he enjoys watching movies, trying out new recipes, and the occasional game of full-contact racquetball.
Read more about Michael Cummings

Daniel Hindrikes
Daniel Hindrikes
author image
Daniel Hindrikes

Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.
Read more about Daniel Hindrikes

Johan Karlsson
Johan Karlsson
author image
Johan Karlsson

Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.
Read more about Johan Karlsson

View More author details
Right arrow

Setting Up a Backend for a Game Using Azure Services

In this chapter, we will set up a backend for a game app with real-time communication. We will not only create a backend that can scale up to handle a large number of users but also scale down when the number of users is reduced. To build that backend, we will use a serverless architecture based on services in Microsoft Azure.

The following topics will be covered in this chapter:

  • Understanding the different Azure serverless services
  • Creating a SignalR service in Microsoft Azure
  • Using Azure Functions as an application programming interface (API)

Technical requirements

To be able to complete this project, you need to have Visual Studio for Mac or PC installed, as well as the necessary .NET MAUI components. See Chapter 1, Introduction to .NET MAUI, for more details on how to set up your environment.

You also need an Azure account. If you have a Visual Studio subscription, there are a specific amount of Azure credits included each month. To activate your Azure benefits, go to https://my.visualstudio.com.

You can also create a free account, where you can use selected services for free over 12 months. You will get $200 worth of credit to explore any Azure service for 30 days, and you can also use the free services at any time. Read more at https://azure.microsoft.com/en-us/free/.

If you do not have and do not want to sign up for a free Azure account, you can use local development tools to run the services without Azure.

You can find the full source for the code in this chapter at https://github.com/PacktPublishing/MAUI...

Project overview

The main aim of this project will be to set up the backend for a game. A large part of the project will be the configuration that we will carry out in the Azure portal. We will also write some code for the Azure functions that will handle the SignalR connections and a bit of the game logic and state. SignalR is a library that makes real-time communication in applications easier. Azure SignalR is a service that makes it easier to connect multiple clients to send messages via the SignalR library. SignalR is described in more detail later. There will be functions to return information about the SignalR connection, manage matching players to play against each other, and post the result of each player’s turn to the SignalR service.

The following diagram shows an overview of the architecture of this application:

Figure 9.1 – Application architecture

The estimated time to complete this part of the project is about 2 hours.

...

Understanding the different Azure serverless services

Before we start to build a backend with a serverless architecture, we need to define what serverless means. In a serverless architecture, the code will run on a server, but we don’t need to worry about that; the only thing we need to focus on is building our software. We let someone else handle everything to do with servers. We don’t need to think about how much memory or central processing units (CPUs) the server needs, or even how many servers we need. When we use services in Azure, Microsoft takes care of this for us.

Azure SignalR Service

Azure SignalR Service is a service in Microsoft Azure for real-time communication between a server and clients. The service will push content to the clients without them having to poll the server to get content updates. SignalR can be used for multiple types of applications, including mobile applications, web applications, and desktop applications.

SignalR will use WebSockets...

Building the serverless backend

In this section, we will set up the backend based on the services described in the preceding section.

Creating a SignalR service

The first service that we will set up is the one for SignalR. To create such a service, proceed as follows:

  1. Go to the Azure portal at https://portal.azure.com.
  2. Create a new resource. The SignalR Service resource is in the Web & Mobile category.
  3. Provide a name for the resource in the form.
  4. Select the subscription you want to use for this project.

    We recommend that you create a new Resource group and use it for all the resources that we will create for this project. The reason that we want one resource group is that it is easier to track which resources are related to this project, and it is also easier to delete all the resources together.

  5. Select a location that is close to your users.
  6. Select a pricing tier. For this project, we will use the Free tier. We can always use the Free tier for...

Deploying the functions to Azure

The final step in this chapter is to deploy the functions to Azure. You can do that as a part of a continuous integration/continuous deployment (CI/CD) pipeline – for example, with Azure DevOps. But the easiest way to deploy the functions, in this case, is to do it directly from Visual Studio. Perform the following steps to deploy the functions:

  1. Right-click on the SticksAndStones.Functions project and select Publish.
  2. Select Azure as the destination for publishing and click Next:
Figure 9.14 – Target selection when publishing

Figure 9.14 – Target selection when publishing

  1. Choose Azure Function App (Windows) in the Specific target tab, then click Next:
Figure 9.15 – Container selection when publishing

Figure 9.15 – Container selection when publishing

  1. Sign in to the same Microsoft account that we used in the Azure portal when we were creating the Function App resource.
  2. Select the subscription that contains the function app. All function...

Summary

In this chapter, we started by learning about a few Azure services, including SignalR, and Functions. Then, we created the services in Azure that our game server backend would need – a SignalR service for real-time communication, and finally, the Functions service to host our backend functions. After this, we implemented the Azure functions that would provide the functionality for our game.

We wrapped up this chapter by publishing our function code to the Azure Functions instance in Azure.

In the next chapter, we will build a game app that will use the backend we have built in this project.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Projects - Third Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781837634910
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Michael Cummings

Michael Cummings is a Senior Development Engineer with Microsoft. He currently works on the Visual Studio tooling for building WPF, Xamarin.Forms, and .NET MAUI apps. He has experience as a developer and architect with concentrations in app development, design, deployment, and business process as it relates to technology. Michael has been a graphics and game programmer since the days of the TI99-4/A. He started the Boston XNA Developers Group. He has contributed to many open-source projects, including AXIOM, a .NET/Mono 3D rendering engine, and Planeshift, a 3D MMORPG. Michael also has experience with native game and graphics development (C++/DirectX) on Windows. Michael lives in Lexington, MA with his wife and their dog. When not working with technology he enjoys watching movies, trying out new recipes, and the occasional game of full-contact racquetball.
Read more about Michael Cummings

author image
Daniel Hindrikes

Daniel Hindrikes is a developer and architect with a passion for developing mobile apps powered by the cloud. Daniel fell in love with Xamarin in the early days of Xamarin when he realized that he could use C# even for iOS and Android apps, and that he could share code with the Windows apps he was also building. But Daniel started to build mobile apps long before that, working on Android apps with Java and even Java ME apps (a long, long time ago). Daniel enjoys sharing his knowledge and can be found speaking at conferences, blogging, or recording the podcast, The Code Behind. Daniel works at the company tretton37 in Sweden and has experience working with both local and global customers.
Read more about Daniel Hindrikes

author image
Johan Karlsson

Johan Karlsson has been working with Xamarin since the days of MonoTouch and Mono for Android, and it all started with writing a game. He is a full-stack developer, currently focusing on mobile applications using Xamarin, but has in the past worked a lot with ASP.NET MVC, Visual Basic.NET (not proud), and C# in general. Also, he's created a whole bunch of databases in SQL Server over the years. Johan works at tretton37 in Sweden and has about 20 years' experience in the trade of assembling ones and zeros.
Read more about Johan Karlsson