Cloud Events - .NET Core

A simple web app written in ASP.NET and C# that can receive and send Cloud Events that you can use for testing. It supports running in two modes:

  1. The default mode has the app reply to your input events with the output event, which is simplest for demonstrating things working in isolation, but is also the model for working for the Knative Eventing Broker concept.

  2. K_SINK mode has the app send events to the destination encoded in $K_SINK, which is useful to demonstrate how folks can synthesize events to send to a Service or Broker when not initiated by a Broker invocation (e.g. implementing an event source)

The application will use $K_SINK-mode whenever the environment variable is specified.

Do the following the steps to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample, by running the following commands:

  1. git clone -b "release-0.25" https://github.com/knative/docs knative-docs
  2. cd knative-docs/docs/serving/samples/cloudevents/cloudevents-dotnet

Before you begin

  • A Kubernetes cluster with Knative installed and DNS configured. Follow the installation instructions if you need to create one.
  • Docker installed and running on your local machine, and a Docker Hub account configured (we’ll use it for a container registry).

The sample code

  1. If you look in controllers\CloudEventsController.cs, you will see two key functions for the different modes of operation:
  1. private async Task<IActionResult> ReceiveAndSend(CloudEvent receivedEvent) {
  2. // This is called whenever an event is received if $K_SINK is set, and sends a new event
  3. // to the url in $K_SINK.
  4. }
  5. private IActionResult ReceiveAndReply(CloudEvent receivedEvent) {
  6. // This is called whenever an event is received if $K_SINK is NOT set, and it replies with
  7. // the new event instead.
  8. }
  1. If you look in Dockerfile, you will see a method for pulling in the dependencies and building an ASP.NET container based on Alpine. You can build and push this to your registry of choice via:
  1. docker build -t <image> .
  2. docker push <image>
  1. If you look in service.yaml, take the <image> name and insert it into the image: field.
  1. kubectl apply -f service.yaml

Testing the sample

Get the URL for your Service with:

  1. $ kubectl get ksvc
  2. NAME URL LATESTCREATED LATESTREADY READY REASON
  3. cloudevents-dotnet http://cloudevents-dotnet... cloudevents-dotnet-ss5pj cloudevents-dotnet-ss5pj True

Then send a cloud event to it with:

  1. $ curl -X POST \
  2. -H "content-type: application/json" \
  3. -H "ce-specversion: 1.0" \
  4. -H "ce-source: curl-command" \
  5. -H "ce-type: curl.demo" \
  6. -H "ce-id: 123-abc" \
  7. -d '{"name":"Dave"}' \
  8. <service-URL>

Where <service-URL> is the URL from the kubectl get ksvc command.

You will get back:

  1. {
  2. "specversion": "1.0",
  3. "type": "dev.knative.docs.sample",
  4. "source": "https://github.com/knative/docs/docs/serving/samples/cloudevents/cloudevents-dotnet",
  5. "id": "d662b6f6-35ff-4b98-bffd-5ae9eee23dab",
  6. "time": "2020-05-19T01:26:23.3500138Z",
  7. "datacontenttype": "application/json",
  8. "data": {
  9. "message": "Hello, Dave"
  10. }
  11. }

Removing the sample app deployment

To remove the sample app from your cluster, delete the service record:

  1. kubectl delete --filename service.yaml