|
| 1 | +# Create a function |
| 2 | + |
| 3 | +Install FaasNet template. This utility can be used to generate Function project in C#. |
| 4 | + |
| 5 | +``` |
| 6 | +dotnet new --install FaasNet.Templates |
| 7 | +``` |
| 8 | + |
| 9 | +Create a Function project |
| 10 | + |
| 11 | +``` |
| 12 | +mkdir QuickStart |
| 13 | +cd QuickStart |
| 14 | +
|
| 15 | +mkdir src |
| 16 | +cd src |
| 17 | +
|
| 18 | +dotnet new faasnetfn -n Function |
| 19 | +``` |
| 20 | + |
| 21 | +The following files will be created : |
| 22 | + |
| 23 | +* *Startup.cs* and *Program.cs*: the application entry point. |
| 24 | +* *HelloWorldConfiguration.cs*: configuration properties of the function. |
| 25 | +* *FunctionHandler.cs*: contains the business logic. This class has one function which accepts one parameter and returns a JSON result. The input parameter has two distinct properties : |
| 26 | + |
| 27 | + * Configuration: its value is coming from the gateway, it will be used to configure the behavior of the function for example : `ConnectionString` and `SQL Statement`. |
| 28 | + * Input: value passed by caller. |
| 29 | + |
| 30 | +In case the Visual Studio Support is needed, a solution can be created : |
| 31 | + |
| 32 | +``` |
| 33 | +cd .. |
| 34 | +dotnet new sln -n QuickStart |
| 35 | +``` |
| 36 | + |
| 37 | +Add the Function project into the solution. |
| 38 | + |
| 39 | +``` |
| 40 | +dotnet sln add ./src/Function/Function.csproj |
| 41 | +``` |
| 42 | + |
| 43 | +# Deploy a function |
| 44 | + |
| 45 | +> [!WARNING] |
| 46 | +> Before you start, Make sure your working environment is properly configured. |
| 47 | +
|
| 48 | +When the Function project is ready, it can be deployed to the Gateway API. |
| 49 | + |
| 50 | +First of all, open a command prompt and execute the following command line to create a Docker file. The `DIRECTORY` variable must be replaced by the directory of the Function.csproj project. |
| 51 | + |
| 52 | +``` |
| 53 | +FaasNet.CLI function -df <DIRECTORY> |
| 54 | +``` |
| 55 | + |
| 56 | +Execute the following instruction to locally build the Docker image. |
| 57 | +The `DIRECTORY` variable must be replaced by the directory of the Function.csproj project, and the `IMAGENAME` variable must be replaced by the name of the Docker image for example : localhost:5000/function. |
| 58 | + |
| 59 | +``` |
| 60 | +FaasNet.CLI function -db <DIRECTORY> -t <IMAGENAME> |
| 61 | +``` |
| 62 | + |
| 63 | +Execute the following command line to push the local Docker image into a registry. The `IMAGENAME` variable must be replaced by the name of the Docker Image. |
| 64 | + |
| 65 | +``` |
| 66 | +FaasNet.CLI function -dp <IMAGENAME> |
| 67 | +``` |
| 68 | + |
| 69 | +Finally, execute the latest command line to deploy the function into the Gateway API. Replace the `FUNCTIONNAME` variable by the name of your function and replace the `IMAGENAME` variable by the name of your Docker image. |
| 70 | + |
| 71 | +``` |
| 72 | +FaasNet.CLI function deploy -name <FUNCTIONAME> -image <IMAGENAME> |
| 73 | +``` |
| 74 | + |
| 75 | +# Execute a function |
| 76 | + |
| 77 | +> [!WARNING] |
| 78 | +> Before you start, Make sure you have a function deployed in the Gateway API. |
| 79 | +
|
| 80 | +Execute the following command to invoke a function. Replace the `FUNCTIONNAME` variable by the name of your function. |
| 81 | + |
| 82 | +``` |
| 83 | +FaasNet.CLI function invoke <FUNCTIONNAME> -input {} -configuration {'firstName':'coucou'} |
| 84 | +``` |
| 85 | + |
| 86 | +The following message is displayed |
| 87 | + |
| 88 | +``` |
| 89 | +{ |
| 90 | + "content": { |
| 91 | + "message": "Hello 'coucou'" |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
0 commit comments