A .NET action is a .NET class library with a method called Main
or MainAsync
that has the exact signature as follows:
Synchronous:
public Newtonsoft.Json.Linq.JObject Main(Newtonsoft.Json.Linq.JObject);
Asynchronous:
public async System.Threading.Tasks.Task<Newtonsoft.Json.Linq.JObject> MainAsync(Newtonsoft.Json.Linq.JObject);
In order to compile, test and archive .NET projects, you must have the .NET SDK installed locally and ensure that the dotnet
executable is included in the PATH
environment variable.
For example, create a C# project called Apache.OpenWhisk.Example.Dotnet
:
dotnet new classlib -n Apache.OpenWhisk.Example.Dotnet -lang C# -f netstandard2.1 cd Apache.OpenWhisk.Example.Dotnet
Install the Newtonsoft.Json NuGet package as follows:
dotnet add package Newtonsoft.Json -v 13.0.1
Now create a file called Hello.cs
with the following content:
Synchronous example:
using System; using Newtonsoft.Json.Linq; namespace Apache.OpenWhisk.Example.Dotnet { public class Hello { public JObject Main(JObject args) { string name = "stranger"; if (args.ContainsKey("name")) { name = args["name"].ToString(); } JObject message = new JObject(); message.Add("greeting", new JValue($"Hello, {name}!")); return (message); } } }
Asynchronous example:
using System; using Newtonsoft.Json.Linq; using System.Threading.Tasks; namespace Apache.OpenWhisk.Example.Dotnet { public class Hello { public async Task<JObject> MainAsync(JObject args) { await Task.Delay(10); // Just do a delay to have an async/await process occur. string name = "stranger"; if (args.ContainsKey("name")) { name = args["name"].ToString(); } JObject message = new JObject(); message.Add("greeting", new JValue($"Hello, {name}!")); return (message); } } }
Publish the project as follows:
dotnet publish -c Release -o out
Zip the published files as follows:
cd out zip -r -0 helloDotNet.zip *
You need to specify the name of the function handler using --main
argument. The value for main
needs to be in the following format: {Assembly}::{Class Full Name}::{Method}
, e.q.:
Apache.OpenWhisk.Example.Dotnet::Apache.OpenWhisk.Example.Dotnet.Hello::Main
Apache.OpenWhisk.Example.Dotnet::Apache.OpenWhisk.Example.Dotnet.Hello::MainAsync
To use on a deployment of OpenWhisk that contains the runtime as a kind:
Synchronous:
wsk action update helloDotNet helloDotNet.zip --main Apache.OpenWhisk.Example.Dotnet::Apache.OpenWhisk.Example.Dotnet.Hello::Main --kind dotnet:6.0
Asynchronous:
wsk action update helloDotNet helloDotNet.zip --main Apache.OpenWhisk.Example.Dotnet::Apache.OpenWhisk.Example.Dotnet.Hello::MainAsync --kind dotnet:6.0
Action invocation is the same for .NET actions as it is for Swift and JavaScript actions:
wsk action invoke --result helloDotNet --param name World
{ "greeting": "Hello World!" }
./gradlew core:dotnet3.1:distDocker
This will produce the image whisk/action-dotnet-v6.0
Build and Push image
docker login ./gradlew core:action-dotnet-v6.0:distDocker -PdockerImagePrefix=$prefix-user -PdockerRegistry=docker.io
Deploy OpenWhisk using ansible environment that contains the kind dotnet:6.0
Assuming you have OpenWhisk already deploy locally and OPENWHISK_HOME
pointing to root directory of OpenWhisk core repository.
Set ROOTDIR
to the root directory of this repository.
Redeploy OpenWhisk
cd $OPENWHISK_HOME/ansible ANSIBLE_CMD="ansible-playbook -i ${ROOTDIR}/ansible/environments/local" $ANSIBLE_CMD setup.yml $ANSIBLE_CMD couchdb.yml $ANSIBLE_CMD initdb.yml $ANSIBLE_CMD wipe.yml $ANSIBLE_CMD openwhisk.yml
Or you can use wskdev
and create a soft link to the target ansible environment, for example:
ln -s ${ROOTDIR}/ansible/environments/local ${OPENWHISK_HOME}/ansible/environments/local-dotnet wskdev fresh -t local-dotnet
Install dependencies from the root directory on $OPENWHISK_HOME repository
pushd $OPENWHISK_HOME ./gradlew install popd
Using gradle to run all tests
./gradlew :tests:test
Using gradle to run some tests
./gradlew :tests:test --tests Net6_0ActionContainerTests
Using IntelliJ:
To use as docker action push to your own dockerhub account
docker tag whisk/action-dotnet-v6.0 $user_prefix/action-dotnet-v6.0 docker push $user_prefix/action-dotnet-v6.0
Then create the action using your the image from dockerhub
wsk action update helloDotNet helloDotNet.zip --main Apache.OpenWhisk.Example.Dotnet::Apache.OpenWhisk.Example.Dotnet.Hello::Main --docker $user_prefix/action-dotnet-v6.0
The $user_prefix
is usually your dockerhub user id.