You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-04-19-dotnet8-template.md
+44-4Lines changed: 44 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ The new template is called: `dotnet8-csharp` and has the following benefits:
28
28
29
29
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.
30
30
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.
32
32
33
33
We'll then briefly discuss a few other features like dependency injection and how to use ASP.NET middlewares such as the static fileserver.
34
34
@@ -239,9 +239,44 @@ public static class Handler
239
239
}
240
240
```
241
241
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:
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 functionhandler directory. All files in this folder will be copied into the final functionimage. 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`functioncreate 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`:
245
280
246
281
```c#
247
282
public static class Handler
@@ -253,7 +288,7 @@ public static class Handler
253
288
// Setup the file server to serve static files.
254
289
app.UseFileServer();
255
290
256
-
app.MapGet("/", () => "Hello from OpenFaaS.");
291
+
app.MapGet("/employees", async () => .... );
257
292
}
258
293
259
294
// MapServices can be used to configure additional
@@ -262,10 +297,15 @@ public static class Handler
262
297
{
263
298
}
264
299
}
300
+
```
265
301
302
+
After building and deploying the functionagain with `faas-cli up` it should now start serving the `index.html` file. You can verify this with curl:
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)
0 commit comments