Skip to content

Commit 753aafe

Browse files
weltekialexellis
authored andcommitted
Update the ASP.NET blog post for .NET 8.0
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
1 parent 5e29252 commit 753aafe

1 file changed

Lines changed: 60 additions & 60 deletions

File tree

_posts/2019-12-03-asp-net-core.md

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ categories:
99
- kubernetes
1010
- kubecon
1111
- events
12+
- dotnet
1213
author_staff_member: alex
1314
dark_background: true
1415

1516
---
1617
In this tutorial I'll show you how to build an ASP.NET Core API that you can deploy to [Kubernetes](https://kubernetes.io/) easily using [OpenFaaS](https://openfaas.com/). We'll be using steps from the official tutorial provided by the [.NET team](https://devblogs.microsoft.com/dotnet/) and explaining any custom steps taken along the way.
1718

19+
> Last Updated: 2024-04-11
20+
1821
# Why is OpenFaaS + ASP.NET Core a good combination?
1922

2023
ASP.NET Core provides a high-performance, lean, and portable runtime that enterprises can use to build robust APIs. The .NET team has worked hard to provide upgrade paths for companies with existing codebases and where that isn't an option, the familiar language can mean that moving code across from legacy code-bases can be done piecemeal.
@@ -33,20 +36,20 @@ The complete code example is [available on GitHub](https://github.com/alexellis/
3336

3437
### Setup OpenFaaS
3538

36-
It's assumed that you already have Kubernetes and OpenFaaS set up, but if you do not then [k3d](https://github.com/rancher/k3d), [KinD](https://kind.sigs.k8s.io), and [minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) can make good local options. I like working with remote clusters since they don't affect the battery life of my laptop, or the CPU/memory of my desktop and are always ready. A good option for a cheap remote cluster may be [DigitalOcean.com](https://m.do.co/c/2962aa9e56a1) or [Civo.com](https://civo.com/).
39+
It's assumed that you already have Kubernetes and OpenFaaS set up, but if you do not then [k3d](https://github.com/k3d-io/k3d), [KinD](https://kind.sigs.k8s.io), and [minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) can make good local options. I like working with remote clusters since they don't affect the battery life of my laptop, or the CPU/memory of my desktop and are always ready. A good option for a cheap remote cluster may be [DigitalOcean.com](https://m.do.co/c/2962aa9e56a1) or [Civo.com](https://civo.com/).
3740

3841
I'll provide two resources for you to get started:
3942

4043
* [OpenFaaS Deployment on Kubernetes](https://docs.openfaas.com/deployment/kubernetes/) - start here if you're confident with deploying Kubernetes
4144
* [OpenFaaS step-by-step workshop](https://github.com/openfaas/workshop) - start here if Docker and Kubernetes are brand new to you
4245

43-
### Install .NET Core 3.1
46+
### Install .NET Core 8.0
4447

45-
Head over to the following site and download .NET Core for your OS, 2.2 will also work if that's what you're currently using:
48+
Head over to the following site and download .NET SDK for your OS:
4649

47-
[.NET Core 3.1 download page](https://dotnet.microsoft.com/download/dotnet-core/3.1)
50+
[.NET Core 8.0 download page](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
4851

49-
I recommend you download the installer and the SDK.
52+
> This tutorial also works for previous versions if that's what you're currently using.
5053
5154
The .NET product automatically reports telemetry, you can [turn this off](https://docs.microsoft.com/en-gb/dotnet/core/tools/telemetry) if you wish. I added `DOTNET_CLI_TELEMETRY_OPTOUT` to my `$HOME/.bash_profile` file.
5255

@@ -82,51 +85,54 @@ This uses the `webapi` project type (that's a REST API endpoint)
8285
dotnet new webapi -o openfaas-api --no-https
8386
```
8487

85-
This is the controller that was generated for us:
88+
The following code shows the contents of the Program.cs that was generated for us::
8689

8790
```cs
88-
using System;
89-
using System.Collections.Generic;
90-
using System.Linq;
91-
using System.Threading.Tasks;
92-
using Microsoft.AspNetCore.Mvc;
93-
using Microsoft.Extensions.Logging;
91+
var builder = WebApplication.CreateBuilder(args);
9492

95-
namespace openfaas_api.Controllers
96-
{
97-
[ApiController]
98-
[Route("[controller]")]
99-
public class WeatherForecastController : ControllerBase
100-
{
101-
private static readonly string[] Summaries = new[]
102-
{
103-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
104-
};
93+
// Add services to the container.
94+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
95+
builder.Services.AddEndpointsApiExplorer();
96+
builder.Services.AddSwaggerGen();
10597

106-
private readonly ILogger<WeatherForecastController> _logger;
98+
var app = builder.Build();
10799

108-
public WeatherForecastController(ILogger<WeatherForecastController> logger)
109-
{
110-
_logger = logger;
111-
}
100+
// Configure the HTTP request pipeline.
101+
if (app.Environment.IsDevelopment())
102+
{
103+
app.UseSwagger();
104+
app.UseSwaggerUI();
105+
}
112106

113-
[HttpGet]
114-
public IEnumerable<WeatherForecast> Get()
115-
{
116-
var rng = new Random();
117-
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
118-
{
119-
Date = DateTime.Now.AddDays(index),
120-
TemperatureC = rng.Next(-20, 55),
121-
Summary = Summaries[rng.Next(Summaries.Length)]
122-
})
123-
.ToArray();
124-
}
125-
}
107+
var summaries = new[]
108+
{
109+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
110+
};
111+
112+
app.MapGet("/weatherforecast", () =>
113+
{
114+
var forecast = Enumerable.Range(1, 5).Select(index =>
115+
new WeatherForecast
116+
(
117+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
118+
Random.Shared.Next(-20, 55),
119+
summaries[Random.Shared.Next(summaries.Length)]
120+
))
121+
.ToArray();
122+
return forecast;
123+
})
124+
.WithName("GetWeatherForecast")
125+
.WithOpenApi();
126+
127+
app.Run();
128+
129+
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
130+
{
131+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
126132
}
127133
```
128134

129-
You can view it with [VSCode](https://code.visualstudio.com) at `./openfaas-api/Controllers/WeatherForecastController.cs`
135+
You can view it with [VSCode](https://code.visualstudio.com) at `./openfaas-api/Program.cs`
130136

131137
We need to package the service in a container for OpenFaaS to be able to serve traffic, but for now let's try it out locally.
132138

@@ -137,7 +143,7 @@ cd openfaas-api/
137143
dotnet run
138144
```
139145

140-
Then access the URL given such as http://localhost:5000 - add `WeatherForecast` to the path to form the URL for the controller: `http://localhost:5000/WeatherForecast`
146+
Then access the URL given such as http://localhost:5026 - add `WeatherForecast` to the path to form the URL for the controller: `http://localhost:5026/WeatherForecast`
141147

142148
![](/images/2019-asp-net-core/local.png)
143149

@@ -154,14 +160,14 @@ docker --version
154160
We'll simply use the example [from the tutorial](https://dotnet.microsoft.com/learn/aspnet/microservice-tutorial/docker-file), but edit it for the name we picked:
155161

156162
```Dockerfile
157-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
163+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
158164
WORKDIR /src
159165
COPY openfaas-api.csproj .
160166
RUN dotnet restore
161167
COPY . .
162168
RUN dotnet publish -c release -o /app
163169

164-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
170+
FROM mcr.microsoft.com/dotnet/aspnet:8.0
165171
WORKDIR /app
166172
COPY --from=build /app .
167173
ENTRYPOINT ["dotnet", "openfaas-api.dll"]
@@ -231,7 +237,6 @@ You should now have:
231237
./openfaas-api/
232238
./openfaas-api/Dockerfile
233239
./openfaas-api/Program.cs
234-
./openfaas-api/Controllers/WeatherForecastController.cs
235240
# etc
236241
```
237242

@@ -254,21 +259,22 @@ You should see the image built successfully, but we need to make a couple of add
254259
The OpenFaaS API expects Docker images to conform to a [runtime workload contract](https://docs.openfaas.com/reference/workloads/), we can either implement that in our code by changing the HTTP port and adding a health-check, or by using the [OpenFaaS watchdog component](https://docs.openfaas.com/architecture/watchdog/).
255260

256261
```Dockerfile
257-
FROM openfaas/of-watchdog:0.7.2 as watchdog
262+
FROM ghcr.io/openfaas/of-watchdog:0.9.15 as watchdog
258263
259-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
264+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
260265
WORKDIR /src
261266
COPY openfaas-api.csproj .
262267
RUN dotnet restore
263268
COPY . .
264269
RUN dotnet publish -c release -o /app
265270
266-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
271+
FROM mcr.microsoft.com/dotnet/aspnet:8.0
267272
WORKDIR /app
268273
COPY --from=build /app .
269274
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
270275
RUN chmod +x /usr/bin/fwatchdog
271276
277+
ENV ASPNETCORE_HTTP_PORTS=80
272278
ENV fprocess="dotnet openfaas-api.dll"
273279
ENV upstream_url="http://127.0.0.1:80"
274280
ENV mode="http"
@@ -280,12 +286,10 @@ The changes add the `fwatchdog` process as the entrypoint to the container. It r
280286

281287
> See also: [OpenFaaS workloads](https://docs.openfaas.com/reference/workloads/)
282288

283-
You can run the Docker image locally as a test before deploying it to OpenFaaS.
289+
You can run the function image locally as a test before deploying it to OpenFaaS.
284290

285291
```sh
286-
faas-cli build
287-
288-
docker run --rm -p 8080:8080 -ti alexellis2/api
292+
faas-cli local-run
289293
```
290294

291295
Then access the site as before: `http://localhost:8080/WeatherForecast`
@@ -420,15 +424,11 @@ Official Template: false
420424

421425
* Auto-scaling
422426

423-
OpenFaaS has built-in auto-scaling rules based upon requests per second, and support for [Kubernetes HPAv2 also](https://docs.openfaas.com/tutorials/kubernetes-hpa/).
424-
425-
[![OpenFaaS workflow](https://github.com/openfaas/faas/blob/master/docs/of-workflow.png?raw=true)](https://docs.openfaas.com/architecture/stack/)
426-
427-
Try [Lab 9 of the OpenFaaS workshop](https://github.com/openfaas/workshop#lab-9---advanced-feature---auto-scaling), where you can learn how to test auto-scaling for your new ASP.NET Core application.
427+
Functions can be autoscaled horizontally or scaled to zero with the [OpenFaaS Standard autoscaler](https://docs.openfaas.com/architecture/autoscaling/)
428428

429429
* Secrets and API keys
430430

431-
You can learn how to securely manage APIs and secrets using the [lessons in Lab 10](https://github.com/openfaas/workshop#lab-10---advanced-feature---secrets)
431+
You can learn how to securely manage APIs and secrets using the following blog post: [Configure your OpenFaaS functions for staging and production](https://www.openfaas.com/blog/custom-environments/)
432432

433433
* Versioning of .NET runtimes
434434

@@ -440,11 +440,11 @@ That's fine, you can create different templates or you can just specify the runt
440440

441441
You can apply 12-factor configuration through the `environment` section of your OpenFaaS stack.yml file.
442442

443-
See also: [Lab 4: Inject configuration through environmental variables](https://github.com/openfaas/workshop/blob/master/lab4.md)
443+
See also: [Configure your OpenFaaS functions for staging and production](https://www.openfaas.com/blog/custom-environments/)
444444

445445
## Wrapping up
446446

447-
In a short period of time we were able to deploy an ASP.NET Core application using .NET 3.1 or 2.x to Kubernetes, have it scale out and build into an immutable Docker image. OpenFaaS made this task much simpler than it would have been if we'd tried to program directly against Kubernetes.
447+
In a short period of time we were able to deploy an ASP.NET Core application using .NET 8.0 or 9.0 to Kubernetes, have it scale out and build into an immutable Docker image. OpenFaaS made this task much simpler than it would have been if we'd tried to program directly against Kubernetes.
448448

449449
If you aren't quite convinced yet, then watch my KubeCon talk on the PLONK Stack that combines [OpenFaaS](https://openfaas.com/) with Kubernetes and several other CNCF projects like [Prometheus](https://prometheus.io) and [NATS](https://nats.io/) to create a platform for application developers.
450450

0 commit comments

Comments
 (0)