Skip to content

Commit 2ab3025

Browse files
weltekialexellis
authored andcommitted
Update middleware and dependency injection example
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
1 parent 339917b commit 2ab3025

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

_posts/2024-04-19-dotnet8-template.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The new template is called: `dotnet8-csharp` and has the following benefits:
2828

2929
In the next section we will walk through an example that show you how to develop and deploy an OpenFaaS function with C# and the new template.
3030

31-
In function will read rows from a database table, and return them as JSON.
31+
The function will read rows from a database table, and return them as JSON.
3232

3333
We'll then briefly discuss a few other features like dependency injection and how to use ASP.NET middlewares such as the static fileserver.
3434

@@ -239,9 +239,44 @@ public static class Handler
239239
}
240240
```
241241
242+
To use Entity Framework Core with our example database model you will need create the database context class `EmployeeDd`. This class is injected into the `/employees` handler and can be used to query the database.
243+
244+
The full example of the `EmployeeDb.cs` file is available on [GitHub](https://github.com/welteki/openfaas-dotnet8-csharp-example/blob/dependency-injection/employee-api/EmployeeDb.cs).
245+
246+
The Entity Framework (EF) Core provider for Npgsql also needs to be added as a dependency to the function. This can be done using the .NET CLI:
247+
248+
```bash
249+
dotnet add employee-api \
250+
package Npgsql.EntityFrameworkCore.PostgreSQL --version 8.0.2
251+
```
252+
242253
## Add Middleware
243254
244-
The MapEndpoints methods gives you access the the `WebApplication` class. This makes it possible to add any existing ASP.NET Core middleware from the function.
255+
The MapEndpoints method gives you access to the `WebApplication` class. This makes it possible to add any existing ASP.NET Core middleware from the function.
256+
257+
In this example we setup a file server to serve static files. Static files can be added in a folder named `static` in the function handler directory. All files in this folder will be copied into the final function image. The static folder is configured as the web root path for the application by default.
258+
259+
To add static files to the `employee-api` function create the static folder:
260+
261+
```bash
262+
mkdir -p employee-api/static
263+
```
264+
265+
We are going to add a basic `index.html` file to the static folder:
266+
267+
```html
268+
<!DOCTYPE html>
269+
<html>
270+
<head>
271+
<title>Hello from OpenFaaS</title>
272+
</head>
273+
<body>
274+
<h1>Hello from OpenFaaS</h1>
275+
</body>
276+
</html>
277+
```
278+
279+
Configure the file server middleware in `Handler.cs`:
245280
246281
```c#
247282
public static class Handler
@@ -253,7 +288,7 @@ public static class Handler
253288
// Setup the file server to serve static files.
254289
app.UseFileServer();
255290
256-
app.MapGet("/", () => "Hello from OpenFaaS.");
291+
app.MapGet("/employees", async () => .... );
257292
}
258293
259294
// MapServices can be used to configure additional
@@ -262,10 +297,15 @@ public static class Handler
262297
{
263298
}
264299
}
300+
```
265301
302+
After building and deploying the function again with `faas-cli up` it should now start serving the `index.html` file. You can verify this with curl:
303+
304+
```bash
305+
curl -i $OPENFAAS_URL/function/employee-api/index.html
266306
```
267307
268-
For more information, see: [ASP.NET Core Middleware](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0)
308+
For more information on how to use middleware, see: [ASP.NET Core Middleware](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0)
269309
270310
## Conclusion
271311

0 commit comments

Comments
 (0)