Learning Dapr Part 2: Build Your First Asp.Net Core Application on Dapr

In this tutorial, you will learn how to create an asp.net core application and running on Dapr local environment.

Prerequisites

This tutorial requires you have following server/components installed on your machine:

Step 1 – Create ASP.NET Core Project

Before following steps, please make sure you have installed the .NET Core SDK. You can check installed .net core sdks by run following command:

$ dotnet --list-sdks

The output should be like:

2.1.4 [/usr/local/share/dotnet/sdk]
2.1.302 [/usr/local/share/dotnet/sdk]
2.2.300 [/usr/local/share/dotnet/sdk]
3.0.100 [/usr/local/share/dotnet/sdk]
3.1.201 [/usr/local/share/dotnet/sdk]
5.0.102 [/usr/local/share/dotnet/sdk]

Then you can create a asp.net core web api service OrderService using command:

$ dotnet new webapi -o OrderService

The -o OrderService parameter creates a directory named OrderService with the source files for the app.

Run following command to Trust the HTTPS development certificate (On MacOS), for other platform refer to Get started with ASP.NET Core for detail:

dotnet dev-certs https --trust

Run following command under the OrderService folder to start up the service:

$ dotnet run

Call the built in rest API ,

Get https://localhost:5001/WeatherForecast

a json result will be shown if everything goes well.

Stop the service, and let’s to run this api service on Dapr in next step.

Step 2 – Run the ASP.NET Core API Service with Dapr

Run following command under the OrderService folder to start up the asp.net core api service with Dapr

$ dapr run --app-id order-service --dapr-http-port 5000 dotnet run

The output text that looks like the following, along with logs:

Try to call the api again: https://localhost:5001/WeatherForecast and check the result. A json result should be responded.

Found Issues

1. Dapr list shows empty data

The api service is good, but the data is empty or 0 after running command: Dapr list

This could be a bug of Dapr, will update this article when there are updates.

Reason:

it is issue of the parameters --dapr-http-port 5000 in the previous command, as 5000 or 5001 (https)port is the default port of asp.net core api service, so 5000 port is using by the api service.

Solution:

use another available port or ignore both --app-port and --dapr-http-port in the command, and it works as well.

for example:

$ dapr run --app-id order-service --dapr-http-port 6000 -- dotnet run

or

$ dapr run --app-id order-service -- dotnet run

Summary

In this tutorial, you have learned how to create an asp.net core api service with dotnet core cli, and run the api service with Dapr.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.