How to Create an Azure Function App Using C# | .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will create a function app in the azure portal create an azure function project in visual studio and deploy the implemented function app onto the microsoft azure cloud [Music] hi i'm a software engineer with more than 10 years of experience with the net platform in this video we create and deploy our first azure function app what is the azure function service azure functions is the serverless service on microsoft azure it can be compared to aws lambda or google cloud functions azure functions provides an event-driven server-less platform to solve problems using various programming languages in comparison to azure app service azure functions allows developers to focus on program solving instead of dealing with web server or application hosting a function app contains one or many functions deployed together as a deployment unit that's enough we need to know to follow this tutorial if you want to learn more about the architecture of azure functions take a look at the official website now let's open the azure portal to create the function app in the azure portal click on the create a resource button the function app resource is featured in the popular product section we click on the create link to open the create function app wizard in the basics tab we select our subscription and resource groups if you are new to azure i suggest reading the organize your azure resources article linked in the video description where azure subscriptions and resource groups are explained in detail we create a new resource group named calculator next we need to provide details about the function app we want to create first of all we need to create the global unique function app name that will be the sub-domain for the azurewebsites.net domain this url is where we will access our function app for our function app we chose calculator-func next we select code as the publish option because we won't add docker containers in this example next we need to choose a runtime stack azure functions app supports various programming platforms and languages besides.net there is no js python java and even powershell core support in this example we want to write our code using c-sharp and select.net as a runtime stack after selecting the runtime stack the version selection contains two options there is.net core 3.1 and dotnet 6 which both are at the time of creating this video supported.net versions because we want to use the latest version and start a new project it makes sense to select.net 6 for our example the last option in the basics tab is to select the region if you are new to azure i have a link in the video description that helps you select the best region for you for this example i select west europe which is the region i have most of my resources finally we click the next button that takes us to the hosting tab first we need to choose a storage account a storage account is a service that allows us to store data for example you can store blob data a storage account can be used directly from an application or in this case we connect our function app with the storage account the storage account will hold our implemented code log data and other information we won't go into more detail in this tutorial we have the option to take the auto generated storage account name but i suggest naming your resources yourself it helps finding the right resource in the azure portal we name the storage account calculator func because we only want to store data from our azure function in the storage account next we can choose between linux and windows as the operating system to execute our functions net is cross-platform and supports both platforms for this tutorial i choose windows the last option in the hosting tab is to choose the pricing model there are three options and we choose the consumption type which is the default selection it allows the function to scale and the only pay for resources you consume if you want to learn more about the other pricing options take a look at the documentation we click on the next button networking is not available for azure function apps that use the consumption plan we click on the next button to get to the monitoring tab we can automatically create an application insights instance to monitor our azure function i would usually suggest creating an application insights instance and integrating it into your function app to monitor your functions however keep things simple for this tutorial and focus on creating our first azure function app i disable application insights we click the next button once again as described in the portal we can set tags to organize our azure resources i skip this step and we click on the review and create button we see a summary of how we configured our azure function app it makes sense to check for typing errors and other mistakes before hitting the create button however keep in mind that deleting azure resources after their creation is always an option let's click the create button and wait until the function app is deployed while we wait for azure to create our function app we create a new azure function project in visual studio 2022 in the create new project dialog we select the azure functions project template on the next page we name the project calculator and set its location we use the same name as the solution name and click on create the next dialog is specific to azure function apps and lets us create either an empty project or a project with a function with a particular trigger there are q triggers http triggers lob triggers timer triggers and much more for our first function we select the http trigger which creates a function that will run whenever it receives an http request we stick with the defaults for the other options and press the create button after a few seconds visual studio created a project for us let's take a look at the project structure first let's open the properties of the project using the context menu the output type of the project is class library and the target framework is.net six now let's look at the installed nuget packages we open the packages folder in the solution explorer and see that we only have a single dependency so far the microsoft.net.sdk.functions package contains everything we need to implement a basic azure function next we open the function1.cs file it contains the definition and implementation of the generated function let's look at it in more detail on line 15 we have the function name attribute it defines the name of the function we can name the class containing the function different from the name of the azure function however for c-sharp azure function apps i would suggest naming both the class and the function the same in the following line we have the method definition of the run method this method gets called by the azure functions runtime when a function trigger is executed the first argument of the run method is of type http request it also has an attribute of type http trigger this definition is to see sharp way of telling azure functions that the function should be run whenever the http trigger gets executed the second argument of the run method is a logger logging is very helpful when dealing with azure functions because often you have a set of different functions to build a solution and you need to make sure you understand what happens or doesn't happen in your system the implementation of the method looks for a name query argument and writes a response message depending on whether a name is provided in the http request now that we understand the basics of the azure function app and the generated function in our project let's implement the function for our calculator function app we rename the class to sum we also set the function name attribute to sum next we delete the content of the run method but keep the log statement on the first line of the implementation and the return statement next read the x and y arguments from the query string and parse them into an int variable after that we'll calculate the sum of both values and store it in the result variable last but not least we change the argument of the ok object result to the result variable that's it for now let's build and start the application in the console we see that our application is running a function with the name sum at the specified url on localhost let's open a browser open the specified url and append the x and y greedy arguments let's set x to 10 and y to 16. we hit enter and get 26 as the respawns for our http request in the console window of the running function app we see a log statement that the function has been executed i want to note that you can debug the function as you would with any other.net code however i don't cover debugging in this tutorial instead let's publish and deploy the function app to microsoft azure back in visual studio we right-click the project and select the publish menu of course it's possible to build and deploy the function app from a ci cd pipeline like azure devops however we're going to publish the function app from visual studio for this tutorial to keep things simple for now in the publish dialog we select azure as the target and click next we set azure function app on windows for the specific target and press the next button again now we select the calculator function app we created in the first part of this tutorial after that we click on the finish button to create the publish profile we now have a published profile but haven't deployed the app to deploy the application to microsoft azure we click on the publish button it takes a few seconds to build and deploy the function app if the deployment is successful we see an information box at the top let's copy the site url open a browser again and use the url and apply the same x and y query arguments to it we get the 404 response when running a function with an http trigger we need to provide a key for security reasons let's go back to the function app in the azure portal and open the function app resource in the menu on the left we click on the functions menu we see a list of all the functions within our deployed function app we currently only have our sum function in this list to see more details we click on the name of the function we click on the get function url button in the toolbar a popup opens and we copy the url which contains a code argument to the clipboard we insert the url from the clipboard in the other browser tab and append the x and y arguments finally we hit enter to send the http request and this time we get 26 as a result of our request congratulations you created and implemented your first azure function app using csharp.net6. if you want to learn more about net development subscribe to this channel and watch this video next to learn everything you need to know about.net 6.
Info
Channel: Claudio Bernasconi
Views: 58,221
Rating: undefined out of 5
Keywords: Azure Functions, Azure Function, Azure Tutorial, Microsoft Azure, C#, CSharp, .NET, .NET 6, dotnet, dotnet 6, Serverless, .NET serverless, 2022, C# in 2022, How to Create an Azure Function, Introduction to Azure Function, Azure Function App, Function App Tutorial, Azure Fuction App Tutorial, Visual Studio, Claudio Bernasconi, Introduction to Azure Functions
Id: kdRVComKwOc
Channel Id: undefined
Length: 12min 48sec (768 seconds)
Published: Wed Jan 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.