The following sections guide you through creating and invoking a single .NET Core action.
In order to compile, test and archive .NET Core projects, you must have the .NET Core SDK installed locally and the environment variable DOTNET_HOME
set to the location where the dotnet
executable can be found.
A .NET Core action is a .NET Core class library with a method called Main
that has the exact signature as follows:
public Newtonsoft.Json.Linq.JObject Main(Newtonsoft.Json.Linq.JObject);
For example, create a C# project called Apache.OpenWhisk.Example.Dotnet
:
dotnet new classlib -n Apache.OpenWhisk.Example.Dotnet -lang "C#"
cd Apache.OpenWhisk.Example.Dotnet
Install the Newtonsoft.Json NuGet package as follows:
dotnet add package Newtonsoft.Json -v 12.0.1
Now create a file called Hello.cs
with the following content:
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);
}
}
}
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
To use on a deployment of OpenWhisk that contains the runtime as a kind:
wsk action update helloDotNet helloDotNet.zip --main Apache.OpenWhisk.Example.Dotnet::Apache.OpenWhisk.Example.Dotnet.Hello::Main --kind dotnet:2.2
Action invocation is the same for .NET Core actions as it is for Swift and JavaScript actions:
wsk action invoke --result helloDotNet --param name World
{
"greeting": "Hello World!"
}