Azure Functions C#

Introduction to Azure Functions C#

The following article provides an outline for Azure Functions C#. Microsoft’s primary computing service is Azure function C#. We gain agility in development by focusing on our code and not worrying about server maintenance by using Azure functions. We are seeing the advantages of Azure functions and the simplicity of the event-driven model functions for integrating Azure services. The Azure function C# executes small pieces of code from larger applications.

What is Azure Functions C#?

We are scaling our Azure functions and using them for processing data, integrating with the systems, and building microservices and simple APIs. For creating the Azure function, we are using Visual Studio to create the C# function project for running the same in a serverless environment.

Azure function is a serverless concept of cloud-native design that allows for small pieces of code to be deployed and executed without the need for any server infrastructure or configuration. When the demand for an Azure function increases, multiple resources are automatically allocated to the service.

How to Create Azure Functions C# in Visual Studio?

The below steps show how we can create the Azure function C# by using Visual Studio.

1. In the first step, we are logging into the Azure portal as follows.

Azure Service

2. After we need to click on new and need to search the function app to create a new function as follows.

Create Resources

3. After creating the function, we need to search the function name which we have defined at the time of creation.

Azure function apps

4. After opening the function, we need to click on the webhook+API option to select the language and trigger as follows.

Csharp

5. After selecting the language, in this step, we select the database for creating it on Azure. We are selecting the SQL database as follows.

Databses

6. After selecting the database, in this step, we create the tables in a specified database. We are creating the table below as follows.

Create TABLE azure_function ( stud_id int (10) Primary key, stud_name nvarchar (max) )

7. After creating the table, in this step, we will click on a function to check the code of the Azure function as follows.

Check function

8. After checking the function now in this step, we are viewing the file of the Azure function as follows.

function.json

9. After viewing the file in this step, we are adding the new file in the specified function. We are adding the below code in a new file as follows.

{ "frameworks": { "net46":{ "dependencies": { "Dapper": "1.42.0", "System.Data.SqlClient":"4.1.0", "Microsoft.WindowsAzure.ConfigurationManager":"3.2.1" } } } }

10. Now, in this step, we are configuring the connection of the Azure SQL database with the function app. For the same, we need to click on the function app as follows.

Market Place

11. After configuring the connection now in this, we click on the data connection to add a new connection as follows.

SQL Database

12. After creating the database, in this step, we open the trigger function and add the below code into it.

log.Info($"C# request.RequestUri={req.RequestUri}");
var successful = true;
try
{
    var con = ConfigurationManager.ConnectionStrings["con string"].ConnectionString;
    using (var connection = new SqlConnection(con))
    {
        connection.Open();
        var rLog = await req.Content.ReadAsAsync<LogRequest>();
        connection.Execute("INSERT INTO [dbo].[azure_function] ([logreq]) VALUES (@Log)", rLog);
        log.Info("DB log");
    }
}
catch
{
    successful = false;
}
return !successful ? req.CreateResponse(HttpStatusCode.BadRequest, "Not processed") : req.CreateResponse(HttpStatusCode.OK, "Data saved");
}

public class LogRequest
{
    public int Id { get; set; }
    public string Log { get; set; }
}

13. After adding the code in this step, we compile the code and run the same in input as follows.

{
"Log" : "Compiled and run successfully"
}

Conclusion

We are scaling our Azure functions and using them for processing data, integrating with the systems, and building microservices and simple APIs. Azure function C# is the Microsoft primary compute service; by using Azure functions, we are gaining the agility of development by focusing on our code, and we have no worries about maintaining the servers.


Similar Articles