Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
vermeir_nico_introducing_net_6_getting_started_with_blazor_m.pdf
Скачиваний:
19
Добавлен:
26.06.2023
Размер:
11.64 Mб
Скачать

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

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

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

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