![](/user_photo/_userpic.png)
- •Table of Contents
- •About the Author
- •Acknowledgments
- •Introduction
- •Version Support
- •Supported Versions
- •A Unified Platform
- •Roadmap
- •Supported Operating Systems
- •Command Line Interface
- •Desktop Development
- •Blazor
- •MAUI
- •Wrapping Up
- •.NET 6 Architecture
- •Runtimes
- •CoreCLR
- •Mono
- •WinRT
- •Managed Execution Process
- •Desktop Packs
- •Wrapping Up
- •Dotnet New
- •Dotnet Restore
- •NuGet.config
- •Dotnet Build
- •Dotnet Publish
- •Dotnet Run
- •Dotnet Test
- •Using the CLI in GitHub Actions
- •Other Commands
- •Wrapping Up
- •WinAPI
- •WinForms
- •STAThread
- •WinForms Startup
- •DPI Mode
- •Responding to Scale Events
- •Visual Styles
- •Text Rendering
- •The Message Loop
- •The Form Designer
- •WPF Startup
- •XAML Layout
- •Visual Tree
- •Data Binding
- •Windows App SDK
- •Building a Windows App SDK application
- •Using Windows APIs with Windows App SDK
- •Packaging
- •Migrating to .NET 6
- •Upgrade Assistant
- •Wrapping Up
- •Blazor WebAssembly
- •Creating a Blazor Wasm Project
- •Blazor Progressive Web Apps
- •Exploring the Blazor Client Project
- •Blazor in .NET 6
- •Blazor Component System
- •Creating Blazor Pages
- •Running a Blazor App
- •Blazor Server
- •SignalR
- •Blazor Desktop
- •Wrapping Up
- •Project Structure
- •Exploring MAUI
- •The Cross-Platform World
- •Application Lifecycle
- •MVVM
- •MVVM Toolkit
- •Wrapping Up
- •Model-View-Controller
- •Routing
- •Views
- •Controllers
- •Controller-Based APIs
- •Minimal APIs
- •Wrapping Up
- •Web Apps
- •Creating an App Service
- •Static Web Apps
- •Web App for Containers
- •Docker
- •Azure Functions
- •Deploying Azure Functions
- •Wrapping Up
- •Record Types
- •Monolith Architecture
- •Microservices
- •Container Orchestration
- •Kubernetes
- •Docker Compose
- •Dapr
- •Installing Dapr
- •Dapr State Management
- •Wrapping Up
- •Roslyn
- •Compiler API
- •Diagnostic API
- •Scripting API
- •Workspace API
- •Syntax Tree
- •Roslyn SDK
- •Source Generators
- •Writing a Source Generator
- •Debugging Source Generators
- •Wrapping Up
- •Garbage Collector
- •The Heap
- •The Stack
- •Garbage Collection
- •A Look at the Threadpool
- •Async in .NET 6
- •Await/Async
- •Cancellations
- •WaitAsync
- •Conclusion
- •Index
Chapter 8 Microsoft Azure
Listing 8-12. HTTP Trigger with POST
[FunctionName("AddBook")]
[OpenApiOperation(operationId: "AddBook", tags: new[] { "Books" })] [OpenApiRequestBody("application/json", typeof(Book), Required = true)] [OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: "application/json", bodyType: typeof(Book), Description = "A newly
added book")]
public async Task<IActionResult> AddBook([HttpTrigger("post", Route = "books")] HttpRequest req)
{
var book = await JsonSerializer.DeserializeAsync<Book>(req.Body); await _bookCatalogService.AddBook(book);
return new CreatedResult($"/books/{book.Id}", book);
}
The major difference with the GET requests is that we specify the trigger to be of type POST and that we need to fetch the request body from the HttpRequest object. The Body property is a stream; fortunately, the JsonSerializer in System.Text.Json can accept a stream and deserialize it to any type.
Deploying Azure Functions
Finally we need to get these Functions in the cloud. We can do this straight from Visual Studio 2022 by right-clicking the project and selecting Publish. In the first step of the wizard, we specify that we want to publish to Azure. In the second step, we can choose if we want to create an Azure Function running on Windows or Linux or in a container.
253
![](/html/75672/2303/html_CACnH9VZ6O.sqXh/htmlconvd-Yz4fCu262x1.jpg)
Chapter 8 Microsoft Azure
Figure 8-28. Choosing a Function type
For this demo, we will choose a Windows-based Function, but other options work just as well. In the next step of the wizard, we can click the + sign to start creating a new Function. The Function Name we choose here needs to be unique across Azure. The Plan Type has three options, Consumption, Premium, and Dedicated (App Service). What you choose here has an impact on scaling, resources per instance, and support for advanced functionality such as virtual network connectivity. More information on the different plan types is found at https://docs.microsoft.com/en-us/azure/azure- functions/functions-scale. Azure Functions also require a Storage account because they rely on storage for managing Triggers and logging.
254
![](/html/75672/2303/html_CACnH9VZ6O.sqXh/htmlconvd-Yz4fCu263x1.jpg)
Chapter 8 Microsoft Azure
Figure 8-29. Creating a new Function
After the function is created, we can go to the API Management step. This is an option to integrate Functions into an API management resource, but that goes beyond the scope of this book, so we can safely skip this step by selecting the Skip this step option that will enable the Create button. Once all steps are done, we can hit the Publish button and Visual Studio will work its magic creating Azure resources and pushing our Function to it.
Looking at the Azure Portal, we can find our newly created Azure Functions resource, with the three Functions that we defined in code.
255
![](/html/75672/2303/html_CACnH9VZ6O.sqXh/htmlconvd-Yz4fCu264x1.jpg)
Chapter 8 Microsoft Azure
Figure 8-30. Functions on the portal
Opening the details of a function gives us the option to copy the url. The url is more than the route we defined in code; it needs to include a code for security reasons.
Figure 8-31. Function url with function key
Executing a GET request to this URL using Postman gives the result in Figure 8-32.
256