Dapr is an event-driven portable runtime for building micro-services, for more information refer to the documentation. Be aware that Dapr is currently under constant evolution, so some of the code used in the sample could be changed or deprecated in the future.
This sample is based on Dapr version 0.4 and focuses on the ASP.NET core integration for its Service integracion, State management and Publish / Subscribe messaging capabilities.
You can find a Postman collection file to test all these sample cases in the /Postman directory.
There is a Hello World sample in the ServiceInvocationController.cs file.
To run the sample locally follow these steps:
- Ensure you have all prerequisites configured correctly
- Make sure you are in the project folder
- Install and start Dapr in standalone mode, via the following command
dapr init
- Start the ASP.NET application with the application listening on HTTP port 5000 (default for Kestrel) and the Dapr sidecar on 5001
dapr run --app-id sample --app-port 5000 --port 5001 dotnet run
Note: The app-id can be anything but you need it to invoke the methods through Dapr.
Now your application should be running on port 5000 while dapr is running on port 5001. You can close the application through Ctrl + C.
First, to test the application itself:
curl http://localhost:5000/hello
should show in the same window
World
and in the dapr running window should show:
== APP == Hello, World.
Now we can test dapr
curl http://localhost:5001/v1.0/invoke/sample/method/hello
which should have the same output in both windows. The sample is the dapr app id and hello is the topic.
This sample in the PubSubMessagingController.cs file is a very basic application that hosts an Order API. This API publishes a message on a topic and another part of the application is subscribing on it.
At startup time, Dapr performs a GET to see on what is has to subscribe
curl http://localhost:5000/dapr/subscribe
The application should return an array that includes the order topic among others
["ordertopic","hello","deposit","withdraw"]
Now you can trigger the Order API to test the application
POST http://localhost:5000/api/order
{
"id" : "123",
"reference" : "tvh",
"product" : "laptop",
"quantity" : 1,
"price" : 1300.00
}
If we analyse the logs, you’ll notice the following entries:
- The Order API gets invoked on api/order
- The order was parsed and received successfully
- The order was published on the ordertopic
- The Dapr sidecar subscribed on the order and invoked the app on /ordertopic
- The order was processed
In case you’re running Dapr standalone and when you didn’t specify any explicit messagebus component, then Dapr will autogenerate component YAML files that interact with the local Redis container that ships with it. In this case, two files were added:
- messagebus.yaml: Redis message bus running locally
- statestore.yaml: Redis state store running locally
There is a simple banking sample in the StateManagementController.cs file. The sample uses the Dapr state-store for its data storage.
It exposes the following endpoints over HTTP:
- GET
/{account}
: Get the balance for the account specified byid
- POST
/deposit
: Accepts a JSON payload to deposit money to an account - POST
/withdraw
: Accepts a JSON payload to withdraw money from an account
The application also registers for pub-sub with the deposit
and withdraw
topics.
Deposit Money
On Windows:
curl -X POST http://localhost:5000/deposit -H "Content-Type: application/json" -d "{ \"id\": \"17\", \"amount\": 12 }"
Output:
{"id":"17","balance":12}
Withdraw Money
On Windows:
curl -X POST http://localhost:5000/withdraw -H "Content-Type: application/json" -d "{ \"id\": \"17\", \"amount\": 10 }"
Outpt:
{"id":"17","balance":2}
Get Balance
curl http://localhost:5000/17
Output:
{"id":"17","balance":2}
Withdraw Money (pubsub)
Publish events using Dapr cli:
On Windows:
dapr publish -t withdraw -p "{\"id\": \"17\", \"amount\": 15 }"
Deposit Money (pubsub)
Publish events using Dapr cli:
On Windows:
dapr publish -t deposit -p "{\"id\": \"17\", \"amount\": 15 }"
In case you want to debug the code inside Visual Studio 2019, while it’s running as a Dapr application, you just need attach to the process with the same name than the project inside the list of running processes.