https://github.com/paolosalvatori/web-app-redis-sql-db
This sample shows how to configure an Azure App Service to access Azure Cache for Redis and Azure SQL Database via regional VET integration and Private Endpoints
https://github.com/paolosalvatori/web-app-redis-sql-db
azure azure-app-service azure-application-insights azure-cache-redis azure-log-analytics azure-monitor azure-private-endpoints azure-sql-database azure-storage-blob azure-virtual-networks
Last synced: about 2 months ago
JSON representation
This sample shows how to configure an Azure App Service to access Azure Cache for Redis and Azure SQL Database via regional VET integration and Private Endpoints
- Host: GitHub
- URL: https://github.com/paolosalvatori/web-app-redis-sql-db
- Owner: paolosalvatori
- License: mit
- Created: 2021-03-26T11:40:47.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-06T14:58:35.000Z (about 4 years ago)
- Last Synced: 2025-02-24T00:41:24.285Z (4 months ago)
- Topics: azure, azure-app-service, azure-application-insights, azure-cache-redis, azure-log-analytics, azure-monitor, azure-private-endpoints, azure-sql-database, azure-storage-blob, azure-virtual-networks
- Language: JavaScript
- Homepage:
- Size: 7.5 MB
- Stars: 9
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
---
products: azure, aspnet, azure-application-insights, azure-app-service, azure-blob-storage, azure-storage-accounts, azure-sql, azure-cache-for-redis, azure-database, azure-web-app, azure-log-analytics, azure-nat-gateway, azure-virtual-machines, vs-code
---# How to configure an Azure Web App to call Azure Cache for Redis and Azure SQL Database via Private Endpoints
This sample shows how to deploy an infrastructure and network topology on Azure where an ASP.NET Core web application hosted by an [Azure App Service](https://docs.microsoft.com/en-us/azure/app-service/) accesses data from [Azure Cache for Redis](https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-overview) and [Azure SQL Database](https://docs.microsoft.com/en-us/azure/azure-sql/database/sql-database-paas-overview) using [Azure Private Endpoints](https://docs.microsoft.com/azure/private-link/private-endpoint-overview). The Azure Web App is hosted in a [Standard, Premium, PremiumV2, PremiumV3](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) with [Regional VNET Integration](https://docs.microsoft.com/en-us/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration).
Private endpoints are fully supported also by the Standard tier of [Azure Cache for Redis](https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-private-link). However, to use private endpoints, an Azure Cache for Redis instance needs to have been created after July 28th, 2020. Currently, zone redundancy, portal console support, and persistence to firewall storage accounts are not supported.This sample also shows how to:
- use a system-assigned managed identity to let the Web App access secrets from Azure Key Vault
- deploy an ASP.NET Core application to an Azure App Service using a GitHub Actions workflow
- disable the public network access from the internet to all the managed services used by the application:- Azure Blob Storage Account
- Azure Key Vault
- Azure Cache for Redis
- Azure SQL Database
- Azure Application InsightsAs an alternative solution, this sample also shows how to deploy Premium Azure Cache for Redis in a virtual network. When an Azure Cache for Redis instance is configured with a virtual network, it isn't publicly addressable and can only be accessed from virtual machines and applications within the virtual network.
For more information, see:
- [Azure Private Link for Azure SQL Database and Azure Synapse Analytics](https://docs.microsoft.com/en-us/azure/azure-sql/database/private-endpoint-overview)
- [Web app private connectivity to Azure SQL database](https://docs.microsoft.com/en-us/azure/architecture/example-scenario/private-web-app/private-web-app)
- [Azure Cache for Redis with Azure Private Link](https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-private-link)
- [Configure virtual network support for a Premium Azure Cache for Redis instance](https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-how-to-premium-vnet)In addition, Azure Web Apps can be configured to be called via a private IP address by applications located in the same virtual network, or in a peered network, or on-premises via ExpressRoute or a S2S VPN. For more information, see:
- [Using Private Endpoints for Azure Web App](https://docs.microsoft.com/en-us/azure/app-service/networking/private-endpoint).
- [Create an App Service app and deploy a private endpoint by using an Azure Resource Manager template](https://docs.microsoft.com/en-us/azure/app-service/scripts/template-deploy-private-endpoint).## Deploy to Azure
You can use the following button to deploy the demo to your Azure subscription:
Azure Cache for Redis via Private Endpoints
[](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fpaolosalvatori%2Fweb-app-redis-sql-db%2Fmaster%2Ftemplates%2Fazuredeploy.endpoint.json%3Ftoken%3DAAIW4AOWATWNQLL2JZKDBAK63EOOU)
Azure Cache for Redis in a virtual network
[](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fpaolosalvatori%2Fweb-app-redis-sql-db%2Fmaster%2Ftemplates%2Fazuredeploy.endpoint.json%3Ftoken%3DAAIW4AOWATWNQLL2JZKDBAK63EOOU)
## Architecture
The following picture shows the architecture and network topology of the first solution where a Standard Azure Cache for Redis is accessed by an Azure Web App via [Regional VNET Integration](https://docs.microsoft.com/en-us/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration) and [Azure Private Endpoints](https://docs.microsoft.com/azure/private-link/private-endpoint-overview).

The ARM template deploys the following resources:
- Virtual Network: this virtual network is composed of the following subnets:
- **WebAppSubnet**: this subnet is used for the regional VNET integration with the Azure Web App app hosted by a Premium Plan. For more information, see [Using Private Endpoints for Azure Web App](https://docs.microsoft.com/en-us/azure/app-service/networking/private-endpoint).
- **PrivateEndpointSubnet**: hosts the private endpoints used by the application.
- **VirtualMachineSubnet**: hosts the jumpbox virtual machine and any additional virtual machine used by the solution.
- **AzureBastionSubnet**: hosts Azure Bastion. For more information, see [Working with NSG access and Azure Bastion](https://docs.microsoft.com/en-us/azure/bastion/bastion-nsg).
- Network Security Group: this resource contains an inbound rule to allow access to the jumpbox virtual machine on port 3389 (RDP)
- A Windows 10 virtual machine. This virtual machine can be used as jumpbox virtual machine to simulate a real application and send requests to the Azure Web Apps exposed via [Azure Private Link](https://docs.microsoft.com/en-us/azure/private-link/private-link-overview).
- A Public IP for Azure Bastion
- Azure Bastion is used to access the jumpbox virtual machine from the Azure Portal via RDP. For more information, see [What is Azure Bastion?](https://docs.microsoft.com/en-us/azure/bastion/bastion-overview).
- An ADLS Gen 2 storage account used to store the boot diagnostics logs of the virtual machine as blobs
- A [Standard, Premium, PremiumV2, PremiumV3](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) hosting plan that supports [Regional VNET Integration](https://docs.microsoft.com/en-us/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration)
- An Azure App Service containing an ASP.NET Core application that uses a system-assigned managed identity to read settings from Key vault. The web site is a single page application that stores data in [Azure SQL Database](https://docs.microsoft.com/en-us/azure/azure-sql/database/sql-database-paas-overview) and caches items in Azure Cache for Redis.
- An Application Insights resource used by the Azure Web Apps app to store logs, traces, requests, exceptions, and metrics. For more information, see [Web application monitoring on Azure](https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/app-service-web-app/app-monitoring).
- An Azure SQL Server and [Azure SQL Database](https://docs.microsoft.com/en-us/azure/azure-sql/database/sql-database-paas-overview) hosting the ProductDB relational database used by the Web App.
- An Azure Key Vault used to store the following application settings. These settings are automtically created by the ARM template as secrets in Azure Key Vault:- Azure Cache for Redis connection string
- Azure SQL Database connection string
- Application Insights Instrumentation Key- A private endpoint to the:
- Azure Blob storage account (boot diagnostics logs)
- Azure Cache for Redis
- Azure SQL Database
- Azure Key Vault- A Private DNS Zone Group to link each private endpoint with the corresponding Private DNS Zone.
- The NIC used by the jumpbox virtual machine and for each private endpoint.
- A Log Analytics workspace used to monitor the health status of the services such as the hosting plan or NSG.
- A Private DNS Zone for Azure Blob Storage Account private endpoint (privatelink.blob.core.windows.net)
- A Private DNS Zone for Azure Cache for Redis private endpoint (privatelink.redis.cache.windows.net)
- A Private DNS Zone for Azure SQL Database private endpoint (privatelink.database.windows.net)
- A Private DNS Zone for Azure Key Vault private endpoint (privatelink.vaultcore.azure.net)The following picture shows the architecture and network topology of the first solution where a Standard Azure Cache for Redis is accessed by an Azure Web App via [Regional VNET Integration](https://docs.microsoft.com/en-us/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration) and [Azure Private Endpoints](https://docs.microsoft.com/azure/private-link/private-endpoint-overview).

The ARM template deploys the following resources:
- Virtual Network: this virtual network is composed of the following subnets:
- **WebAppSubnet**: this subnet is used for the regional VNET integration with the Azure Web App app hosted by a Premium Plan. For more information, see [Using Private Endpoints for Azure Web App](https://docs.microsoft.com/en-us/azure/app-service/networking/private-endpoint).
- **PrivateEndpointSubnet**: hosts the private endpoints used by the application.
- **VirtualMachineSubnet**: hosts the jumpbox virtual machine and any additional virtual machine used by the solution.
- **AzureBastionSubnet**: hosts Azure Bastion. For more information, see [Working with NSG access and Azure Bastion](https://docs.microsoft.com/en-us/azure/bastion/bastion-nsg).
- **RedisCacheSubnet**: hosts the Premium Azure Cache for Redis
- Network Security Group: this resource contains an inbound rule to allow access to the jumpbox virtual machine on port 3389 (RDP)
- A Windows 10 virtual machine. This virtual machine can be used as jumpbox virtual machine to simulate a real application and send requests to the Azure Web Apps exposed via [Azure Private Link](https://docs.microsoft.com/en-us/azure/private-link/private-link-overview).
- Azure Bastion is used to access the jumpbox virtual machine from the Azure Portal via RDP. For more information, see [What is Azure Bastion?](https://docs.microsoft.com/en-us/azure/bastion/bastion-overview).
- An ADLS Gen 2 storage account used to store the boot diagnostics logs of the virtual machine as blobs
- A [Standard, Premium, PremiumV2, PremiumV3](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) hosting plan that supports [Regional VNET Integration](https://docs.microsoft.com/en-us/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration)
- An Azure App Service containing an ASP.NET Core application that uses a system-assigned managed identity to read settings from Key vault. The web site is a single page application that stores data in [Azure SQL Database](https://docs.microsoft.com/en-us/azure/azure-sql/database/sql-database-paas-overview) and caches items in Azure Cache for Redis.
- An Application Insights resource used by the Azure Web Apps app to store logs, traces, requests, exceptions, and metrics. For more information, see [Web application monitoring on Azure](https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/app-service-web-app/app-monitoring).
- An Azure SQL Server and [Azure SQL Database](https://docs.microsoft.com/en-us/azure/azure-sql/database/sql-database-paas-overview) hosting the ProductDB relational database used by the Web App.
- An Azure Key Vault used to store the following application settings. These settings are automtically created by the ARM template as secrets in Azure Key Vault:- Azure Cache for Redis connection string
- Azure SQL Database connection string
- Application Insights Instrumentation Key- A private endpoint to the:
- Azure Blob storage account (boot diagnostics logs)
- Azure SQL Database
- Azure Key Vault- A Private DNS Zone Group to link each private endpoint with the corresponding Private DNS Zone.
- The NIC used by the jumpbox virtual machine and for each private endpoint.
- A Log Analytics workspace used to monitor the health status of the services such as the hosting plan or NSG.
- A Private DNS Zone for Azure Blob Storage Account private endpoint (privatelink.blob.core.windows.net)
- A Private DNS Zone for Azure SQL Database private endpoint (privatelink.database.windows.net)
- A Private DNS Zone for Azure Key Vault private endpoint (privatelink.vaultcore.azure.net)## Important Notes
The two ARM templates disable the public access to both Azure SQL Database and Azure Cache for Redis via the `publicNetworkAccess` parameter which default value is set to `false`. Using private endpoints is not enough to secure an application, you also have to disable the public access to the managed services used by the application, in this case Azure SQL Database and Azure Cache for Redis.
In addition, both ARM templates automatically create the connection string to both the Azure Cache for Redis and Azure SQL Database as application settings of the Azure App Service. However, in a production environment, it's recommended to access adopt one of the following approaches:
- Use a system assigned managed identity from the Web App to access Azure SQL Database. For more information, see [](https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi). For more information about the Azure Services that support managed identities, see [Services that support managed identities for Azure resources](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/services-support-managed-identities).
- Store sensitive data like connection strings, encryption keys, certificates, and connection string in [Azure Key Vault](https://docs.microsoft.com/en-us/azure/key-vault/general/overview). For more information, see [Tutorial: Use a managed identity to connect Key Vault to an Azure web app in .NET](https://docs.microsoft.com/en-us/azure/key-vault/general/tutorial-net-create-vault-azure-web-app).## Prerequisites
The following components are required to run this sample:
- [.NET Core 3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1)
- [Visual Studio Code](https://code.visualstudio.com/)
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest)
- [Azure subscription](https://azure.microsoft.com/free/)## Topology Deployment
You can use the ARM template and Bash script included in the sample to deploy to Azure the entire infrastructure necessary to host the demo:
```sh
#!/bin/bash# Clear the screen
clear# Print the menu
echo "================================================="
echo "Install Demo. Choose an option (1-3): "
echo "================================================="
options=("Inject Premium Azure Cache for Redis in a VNET"
"Azure Cache for Redis with Azure Private Link"
"Quit")# Select an option
COLUMNS=0
select opt in "${options[@]}"; do
case $opt in
"Inject Premium Azure Cache for Redis in a VNET")
template="../templates/azuredeploy.vnet.json"
parameters="../templates/azuredeploy.vnet.parameters.json"
resourceGroupName="WebAppSqlDbRedisInVnetRG"
break
;;
"Azure Cache for Redis with Azure Private Link")
template="../templates/azuredeploy.endpoint.json"
parameters="../templates/azuredeploy.endpoint.parameters.json"
resourceGroupName="WebAppSqlDbRedisCacheRG"
break
;;
"Quit")
exit
;;
*) echo "invalid option $REPLY" ;;
esac
done# Variables
location="WestEurope"# SubscriptionId of the current subscription
subscriptionId=$(az account show --query id --output tsv)
subscriptionName=$(az account show --query name --output tsv)# Check if the resource group already exists
createResourceGroup() {
local resourceGroupName=$1
local location=$2# Parameters validation
if [[ -z $resourceGroupName ]]; then
echo "The resource group name parameter cannot be null"
exit
fiif [[ -z $location ]]; then
echo "The location parameter cannot be null"
exit
fiecho "Checking if [$resourceGroupName] resource group actually exists in the [$subscriptionName] subscription..."
if ! az group show --name "$resourceGroupName" &>/dev/null; then
echo "No [$resourceGroupName] resource group actually exists in the [$subscriptionName] subscription"
echo "Creating [$resourceGroupName] resource group in the [$subscriptionName] subscription..."# Create the resource group
if az group create --name "$resourceGroupName" --location "$location" 1>/dev/null; then
echo "[$resourceGroupName] resource group successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$resourceGroupName] resource group in the [$subscriptionName] subscription"
exit
fi
else
echo "[$resourceGroupName] resource group already exists in the [$subscriptionName] subscription"
fi
}# Validate the ARM template
validateTemplate() {
local resourceGroupName=$1
local template=$2
local parameters=$3
local arguments=$4# Parameters validation
if [[ -z $resourceGroupName ]]; then
echo "The resource group name parameter cannot be null"
fiif [[ -z $template ]]; then
echo "The template parameter cannot be null"
fiif [[ -z $parameters ]]; then
echo "The parameters parameter cannot be null"
fiecho "Validating [$template] ARM template..."
if [[ -z $arguments ]]; then
error=$(az deployment group validate \
--resource-group "$resourceGroupName" \
--template-file "$template" \
--parameters "$parameters" 2>&1 | grep 'ERROR:')
else
error=$(az deployment group validate \
--resource-group "$resourceGroupName" \
--template-file "$template" \
--parameters "$parameters" \
--arguments $arguments 2>&1 | grep 'ERROR:')
fiif [[ -z $error ]]; then
echo "[$template] ARM template successfully validated"
else
echo "Failed to validate the [$template] ARM template"
echo "$error"
exit 1
fi
}# Deploy ARM template
deployTemplate() {
local resourceGroupName=$1
local template=$2
local parameters=$3
local arguments=$4# Parameters validation
if [[ -z $resourceGroupName ]]; then
echo "The resource group name parameter cannot be null"
exit
fiif [[ -z $template ]]; then
echo "The template parameter cannot be null"
exit
fiif [[ -z $parameters ]]; then
echo "The parameters parameter cannot be null"
exit
fi# Deploy the ARM template
echo "Deploying [$template] ARM template..."if [[ -z $arguments ]]; then
az deployment group create \
--resource-group $resourceGroupName \
--template-file $template \
--parameters $parameters 1>/dev/null
else
az deployment group create \
--resource-group $resourceGroupName \
--template-file $template \
--parameters $parameters \
--parameters $arguments 1>/dev/null
fiif [[ $? == 0 ]]; then
echo "[$template] ARM template successfully provisioned"
else
echo "Failed to provision the [$template$] ARM template"
exit -1
fi
}# Create Resource Group
createResourceGroup \
"$resourceGroupName" \
"$location"# Validate ARM Template
validateTemplate \
"$resourceGroupName" \
"$template" \
"$parameters"# Deploy ARM Template
deployTemplate \
"$resourceGroupName" \
"$template" \
"$parameters"
```## Create tables and stored procedures
You can use the following `ProductsDB` T-SQL script to initialize the SQL database used by the ASP.NET Core application.
```SQL
IF OBJECT_ID('Products') > 0 DROP TABLE [Products]
GO
-- Create Products table
CREATE TABLE [Products]
(
[ProductID] [int] IDENTITY(1,1) NOT NULL ,
[Name] [nvarchar](50) NOT NULL ,
[Category] [nvarchar](50) NOT NULL ,
[Price] [smallmoney] NOT NULL
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductID]
)
)
GO
-- Create stored procedures
IF OBJECT_ID('GetProduct') > 0 DROP PROCEDURE [GetProduct]
GO
CREATE PROCEDURE GetProduct
@ProductID int
AS
SELECT [ProductID], [Name], [Category], [Price]
FROM [Products]
WHERE [ProductID] = @ProductID
GO
IF OBJECT_ID('GetProducts') > 0 DROP PROCEDURE [GetProducts]
GO
CREATE PROCEDURE GetProducts
AS
SELECT [ProductID], [Name], [Category], [Price]
FROM [Products]
GO
IF OBJECT_ID('GetProductsByCategory') > 0 DROP PROCEDURE [GetProductsByCategory]
GO
CREATE PROCEDURE GetProductsByCategory
@Category [nvarchar](50)
AS
SELECT [ProductID], [Name], [Category], [Price]
FROM [Products]
WHERE [Category] = @Category
GO
IF OBJECT_ID('AddProduct') > 0 DROP PROCEDURE [AddProduct]
GO
CREATE PROCEDURE AddProduct
@ProductID int OUTPUT,
@Name [nvarchar](50),
@Category [nvarchar](50),
@Price [smallmoney]
AS
INSERT INTO Products
VALUES
(@Name, @Category, @Price)
SET @ProductID = @@IDENTITY
GO
IF OBJECT_ID('UpdateProduct') > 0 DROP PROCEDURE [UpdateProduct]
GO
CREATE PROCEDURE UpdateProduct
@ProductID int,
@Name [nvarchar](50),
@Category [nvarchar](50),
@Price [smallmoney]
AS
UPDATE Products
SET [Name] = @Name,
[Category] = @Category,
[Price] = @Price
WHERE [ProductID] = @ProductID
GO
IF OBJECT_ID('DeleteProduct') > 0 DROP PROCEDURE [DeleteProduct]
GO
CREATE PROCEDURE DeleteProduct
@ProductID int
AS
DELETE [Products]
WHERE [ProductID] = @ProductID
GO
-- Create test data
SET NOCOUNT ON
GO
INSERT INTO Products
VALUES
(N'Tomato soup', N'Groceries', 1.39)
GO
INSERT INTO Products
VALUES
(N'Babo', N'Toys', 19.99)
GO
INSERT INTO Products
VALUES
(N'Hammer', N'Hardware', 16.49)
GO
```You can proceed as follows to create the tables and stored procedure in the SQL database:
- VPN into the jumpbox virtual machine using Azure Bastion as shown in the picture below
- Open a browser and connect to the Azure Portal
- Open the Query Editor under the Azure SQL Database resource
- Copy and paste the code in `ProductsDB` T-SQL script into a new query
- Execute the scripts that creates the tables and some test data in the Products table used by the Web App
## ASP.NET Core application
This sample provides an ASP.NET Core single-page application (SPA) to test the topology. The application reads:
- Azure Cache for Redis connection string
- Azure SQL Database connection string
- Application Insights Instrumentation Keyapplication settings from Azure Key Vault using the following code defined in the `Program` class. For more information, see [Azure Key Vault configuration provider in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-5.0). The application uses the system-assigned managed identity of the App Service to access secrets from Azure Key Vault. The ARM template creates Key Vault, the secrets used application settings by the ASP.NET Core aaplication, and the access policies to grant permissions on secrets to the system-assigned managed identity. For more information, see [How to use managed identities for App Service and Azure Functions](https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=dotnet).
### Program.cs
```csharp
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Products.Properties;namespace Products
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var builtConfig = config.Build();
var keyVaultUri = builtConfig[Resources.KeyVaultUri];
if (string.IsNullOrEmpty(keyVaultUri))
{
throw new Exception("KeyVaultUri parameter in the appsettings.json cannot be null or empty");
}
var secretClient = new SecretClient(
new Uri(keyVaultUri),
new DefaultAzureCredential());
config.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
})
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup());
}
}
```The application makes use of the following libraries and features:
- [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) library to read, create, update, and delete string values and [sets](https://redis.io/topics/data-types) in the Azure Cache for Redis. For more information, see [Quickstart: Use Azure Cache for Redis with an ASP.NET web app](https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-web-app-howto).
- [Entity Framework 5.0](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew) to read, create, update, and delete records from the Products table in the Azure SQL Database. For more information, see [Tutorial: Get started with EF Core in an ASP.NET MVC web app](https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-5.0).
- [Dependency injection in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) to inject services into the constructor of the class where it's used. The framework takes on the responsibility of creating an instance of the dependency and disposing of it when it's no longer needed. For more information, see the code of the `ConfigureServices` method in the `Startup` class below.### Startup.cs
```csharp
using System;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
using Products.Properties;
using Products.Models;
using Products.Helpers;namespace Products
{
public class Startup
{
///
/// Creates an instance of the Startup class
///
/// The configuration created by the CreateDefaultBuilder.
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}///
/// Gets or sets the Configuration property.
///
public IConfiguration Configuration { get; }///
/// This method gets called by the runtime. Use this method to add services to the container.
///
/// The services collection.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddApplicationInsightsTelemetry(Configuration[Resources.ApplicationInsightsConnectionString]);
services.AddOptions();
services.AddMvc();
services.AddSingleton(ConnectionMultiplexer.Connect(Configuration.GetConnectionString(Resources.RedisCacheConnectionString)));
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(Resources.SqlServerConnectionString)));// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Products API",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = new Uri("https://www.apache.org/licenses/LICENSE-2.0"),
Contact = new OpenApiContact
{
Name = "Paolo Salvatori",
Email = "[email protected]",
Url = new Uri("https://github.com/paolosalvatori")
},
License = new OpenApiLicense
{
Name = "Use under Apache License 2.0",
Url = new Uri("https://www.apache.org/licenses/LICENSE-2.0")
}
});
});
}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoList API V1");
c.RoutePrefix = "swagger";
});app.UseHttpsRedirection();
app.UseStaticFiles();app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
```The table below shows the code of the REST API implemented by the `ProductsController` class. This API is called via [jQuery](https://api.jquery.com/jquery.ajax/) by the client-side script running in the single-page application.
```csharp
using System;
using System.Linq;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Globalization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
using Products.Models;
using Products.Properties;
using Products.Helpers;namespace Products.Controllers
{
[Route("api/[controller]")]
[Produces("application/json")]
[ApiController]
public class ProductsController : ControllerBase
{
#region Private Instance Fields
private readonly ILogger logger;
private readonly ProductsContext context;
private readonly IDatabase database;
#endregion#region Public Constructors
public ProductsController(ILogger logger,
ProductsContext context,
IConnectionMultiplexer connectionMultiplexer)
{
this.logger = logger;
this.context = context;
database = connectionMultiplexer.GetDatabase();
}
#endregion#region Public Methods
///
/// Gets all the products.
///
/// All the products.
/// Get all the products, if any.
[HttpGet]
[ProducesResponseType(typeof(Product), 200)]
public async Task GetAllProductsAsync()
{
var stopwatch = new Stopwatch();try
{
stopwatch.Start();
logger.LogInformation("Listing all products...");
var values = await database.SetMembersAsync(Resources.RedisKeys);
var items = await database.GetAsync(values.Select(v => (string)v).ToArray());
if (items.Any())
{
var list = items.ToList();
list.Sort((x, y) => x.ProductId - y.ProductId);
return new OkObjectResult(list.ToArray());
}
var products = context.Products.FromSqlRaw(Resources.GetProducts);
foreach (var product in products)
{
var idAsString = product.ProductId.ToString(CultureInfo.InvariantCulture);
await database.SetAsync(idAsString, product);
await database.SetAddAsync(Resources.RedisKeys, idAsString);
}
return new OkObjectResult(products.ToArray());
}
catch (Exception ex)
{
var errorMessage = MessageHelper.FormatException(ex);
logger.LogError(errorMessage);
return StatusCode(400, new { error = errorMessage });
}
finally
{
stopwatch.Stop();
logger.LogInformation($"GetAllProductsAsync method completed in {stopwatch.ElapsedMilliseconds} ms.");
}
}///
/// Gets a specific product by id.
///
/// Id of the product.
/// Product with the specified id.
/// Product found
/// Product not found
[HttpGet("{id}", Name = "GetProductByIdAsync")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(typeof(Product), 404)]
public async Task GetProductByIdAsync(int id)
{
var stopwatch = new Stopwatch();try
{
stopwatch.Start();
logger.LogInformation($"Getting product {id}...");
var product = await database.GetAsync(id.ToString());
if (product != null)
{
return new OkObjectResult(product);
}var products = context.Products.FromSqlRaw(Resources.GetProduct, new SqlParameter
{
ParameterName = "@ProductID",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.Int,
Value = id
});
if (products.Any())
{
product = products.FirstOrDefault();
var idAsString = product.ProductId.ToString(CultureInfo.InvariantCulture);
await database.SetAsync(idAsString, product);
await database.SetAddAsync(Resources.RedisKeys, idAsString);logger.LogInformation($"Product with id = {product.ProductId} has been successfully retrieved.");
return new OkObjectResult(product);
}
else
{
logger.LogWarning($"No product with id = {id} was found");
return null;
}
}
catch (Exception ex)
{
var errorMessage = MessageHelper.FormatException(ex);
logger.LogError(errorMessage);
return StatusCode(400, new { error = errorMessage });
}
finally
{
stopwatch.Stop();
logger.LogInformation($"GetProductByIdAsync method completed in {stopwatch.ElapsedMilliseconds} ms.");
}
}///
/// Creates a new product.
///
///
///
/// Product to create.
/// If the operation succeeds, it returns the newly created product.
/// Product successfully created.
/// Product is null.
[HttpPost]
[ProducesResponseType(typeof(Product), 201)]
[ProducesResponseType(typeof(Product), 400)]
public async Task CreateProductAsync(Product product)
{
var stopwatch = new Stopwatch();try
{
stopwatch.Start();
if (product == null)
{
logger.LogWarning("Product cannot be null.");
return BadRequest();
}var productIdParameter = new SqlParameter
{
ParameterName = "@ProductID",
Direction = ParameterDirection.Output,
SqlDbType = SqlDbType.Int
};var result = await context.Database.ExecuteSqlRawAsync(Resources.AddProduct, new SqlParameter[] {
productIdParameter,
new SqlParameter
{
ParameterName = "@Name",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.NVarChar,
Size = 50,
Value = product.Name
},
new SqlParameter
{
ParameterName = "@Category",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.NVarChar,
Size = 50,
Value = product.Category
},
new SqlParameter
{
ParameterName = "@Price",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.SmallMoney,
Value = product.Price
}
});
if (result ==1 && productIdParameter.Value != null)
{
product.ProductId = (int)productIdParameter.Value;
var idAsString = product.ProductId.ToString(CultureInfo.InvariantCulture);
await database.SetAsync(idAsString, product);
await database.SetAddAsync(Resources.RedisKeys, idAsString);
logger.LogInformation($"Product with id = {product.ProductId} has been successfully created.");
return CreatedAtRoute("GetProductByIdAsync", new { id = product.ProductId }, product);
}
return null;
}
catch (Exception ex)
{
var errorMessage = MessageHelper.FormatException(ex);
logger.LogError(errorMessage);
return StatusCode(400, new { error = errorMessage });
}
finally
{
stopwatch.Stop();
logger.LogInformation($"CreateProductAsync method completed in {stopwatch.ElapsedMilliseconds} ms.");
}
}///
/// Updates a product.
///
/// The id of the product.
/// Product to update.
/// No content.
/// No content if the product is successfully updated.
/// If the product is not found.
[HttpPut("{id}")]
[ProducesResponseType(typeof(Product), 204)]
[ProducesResponseType(typeof(Product), 404)]
public async Task Update(int id, [FromBody] Product product)
{
var stopwatch = new Stopwatch();try
{
stopwatch.Start();
if (product == null || product.ProductId != id)
{
logger.LogWarning("The product is null or its id is different from the id in the payload.");
return BadRequest();
}var result = await context.Database.ExecuteSqlRawAsync(Resources.UpdateProduct, new SqlParameter[] {
new SqlParameter
{
ParameterName = "@ProductID",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.Int,
Value = product.ProductId
},
new SqlParameter
{
ParameterName = "@Name",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.NVarChar,
Size = 50,
Value = product.Name
},
new SqlParameter
{
ParameterName = "@Category",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.NVarChar,
Size = 50,
Value = product.Category
},
new SqlParameter
{
ParameterName = "@Price",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.SmallMoney,
Value = product.Price
}
});if (result == 1)
{
var idAsString = id.ToString(CultureInfo.InvariantCulture);
await database.SetAsync(idAsString, product);
await database.SetAddAsync(Resources.RedisKeys, idAsString);logger.LogInformation("Product with id = {ID} has been successfully updated.", product.ProductId);
}
return new NoContentResult();
}
catch (Exception ex)
{
var errorMessage = MessageHelper.FormatException(ex);
logger.LogError(errorMessage);
return StatusCode(400, new { error = errorMessage });
}
finally
{
stopwatch.Stop();
logger.LogInformation($"Update method completed in {stopwatch.ElapsedMilliseconds} ms.");
}
}///
/// Deletes a specific product.
///
/// The id of the product.
/// No content.
/// No content if the product is successfully deleted.
/// If the product is not found.
[HttpDelete("{id}")]
[ProducesResponseType(typeof(Product), 204)]
[ProducesResponseType(typeof(Product), 404)]
public async Task Delete(string id)
{
var stopwatch = new Stopwatch();try
{
stopwatch.Start();var result = await context.Database.ExecuteSqlRawAsync(Resources.DeleteProduct, new SqlParameter[] {
new SqlParameter
{
ParameterName = "@ProductID",
Direction = ParameterDirection.Input,
SqlDbType = SqlDbType.Int,
Value = id
}
});if (result == 1)
{
var idAsString = id.ToString(CultureInfo.InvariantCulture);
await database.KeyDeleteAsync(idAsString);
await database.SetRemoveAsync(Resources.RedisKeys, idAsString);logger.LogInformation("Product with id = {ID} has been successfully deleted.", id);
}
return new NoContentResult();
}
catch (Exception ex)
{
var errorMessage = MessageHelper.FormatException(ex);
logger.LogError(errorMessage);
return StatusCode(400, new { error = errorMessage });
}
finally
{
stopwatch.Stop();
logger.LogInformation($"Delete method completed in {stopwatch.ElapsedMilliseconds} ms.");
}
}
#endregion
}
}
```## Deploy the code of the ASP.NET Core application
Once the Azure resources have been deployed to Azure (which can take about 10-12 minutes), you need to deploy the ASP.NET Core web application contained in the `src` folder to the newly created Azure App Service. You can customize and use the `deploy-web-app-to-azure.yml` GitHub Actions workflow under the `.github\workflow` folder to deploy the application to Azure App Service. As an alternative, you can use [Visual Studio Code](https://code.visualstudio.com/) or [Visual Studio](https://visualstudio.microsoft.com/) to deploy the ASP.NET Core application to the Azure App Service created by the ARM template.
## Test the Application
After creating the database and deploying the Web App, you can simply navigate to the URL of your Azure App Service to check if the application is up and running, as shown in the following figure.
