Teach you to learn Dapr - 3. Use Dapr to run the first. Net program

Keywords: Docker .NET microsoft

be careful:

The command line tool mentioned in this article is one of Windows Terminal/PowerShell/cmd. It is recommended to use Windows Terminal

It is recommended to run the command line tool as an administrator to avoid stepping on the pit

To ensure smooth operation, it is recommended to use PowerShell to execute set executionpolicy remotesigned first

Install Docker

Because Dapr CLI will start redis, zipkin and placement in Docker by default.

Of course, these do not have to be installed, but it is recommended that the installation can experience the complete capabilities of Dapr and facilitate the learning of subsequent chapters.

  1. Download and install Docker Desktop


  2. Install WSL2 and execute the command using the command line tool

    wsl --instal
    If you can't install directly using wsl, you can install it manually, run PowerShell and execute the following two commands
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    If you encounter the error prompt again: 0x800f080c the function name VirtualMachinePlatform is unknown.

    Please ensure that your operating system version is above Windows 10 build 18917

  3. Restart the computer and download the WSL2 kernel


  4. Run the command line tool and set WSL2 as the default

    wsl --set-default-version 2
  5. Download Ubuntu 20.04


  6. Set Docker to use WSL2

You "may" need a gadget

Can not tell the secret, look at the file name guessing function


Installing Dapr CLI

Official explanation: Dapr CLI is your main tool for various Dapr related tasks. You can use it to run an application with Dapr sidecar, view sidecar logs, list running services, and run the Dapr dashboard.

Does the CLI have to be installed? In fact, it is not, but novices are not recommended to experience these operations. Subsequent articles will explain how to break away from dapr cli.

  1. Run Windows Terminal or PowerShell and execute the command. Be patient here

    iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex

    If cmd, execute the following command:

    powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
  2. Verify the installation, re open the command line tool, execute the command dapr, and see the following prompt that the installation is correct

        ____/ /___ _____  _____
       / __  / __ '/ __ \/ ___/
      / /_/ / /_/ / /_/ / /
      \__,_/\__,_/ .___/_/
    Distributed Application Runtime
      dapr [command]
    Available Commands:
      completion     Generates shell completion scripts
      components     List all Dapr components. Supported platforms: Kubernetes
      configurations List all Dapr configurations. Supported platforms: Kubernetes
      dashboard      Start Dapr dashboard. Supported platforms: Kubernetes and self-hosted
      help           Help about any command
      init           Install Dapr on supported hosting platforms. Supported platforms: Kubernetes and self-hosted
      invoke         Invoke a method on a given Dapr application. Supported platforms: Self-hosted
      list           List all Dapr instances. Supported platforms: Kubernetes and self-hosted
      logs           Get Dapr sidecar logs for an application. Supported platforms: Kubernetes
      mtls           Check if mTLS is enabled. Supported platforms: Kubernetes
      publish        Publish a pub-sub event. Supported platforms: Self-hosted
      run            Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted
      status         Show the health status of Dapr services. Supported platforms: Kubernetes
      stop           Stop Dapr instances and their associated apps. . Supported platforms: Self-hosted
      uninstall      Uninstall Dapr runtime. Supported platforms: Kubernetes and self-hosted
      upgrade        Upgrades a Dapr control plane installation in a cluster. Supported platforms: Kubernetes
      -h, --help      help for dapr
      -v, --version   version for dapr
    Use "dapr [command] --help" for more information about a command.

Initialize Dapr

  1. Executing commands using command line tools

    dapr init

    In addition, there is a simplified installation method

    dapr init --slim

    Look at the official explanation slim

    Two different binaries, daprd and placement, are installed in this mode. Placement is for actor s, as mentioned earlier.

    In this mode, no default component (such as Redis) will be installed for status management or publish / subscribe, so it can only be called by service. Others require users to set their own environment and custom components.

    To put it bluntly, you only use service calls, and slim is OK. Otherwise, you need to manually configure the CLI to replace the work you do.

  2. Verify Dapr version

    dapr --version

    At the current time, my version is

    CLI version: 1.4.0
    Runtime version: 1.4.3
  3. Validation container

    docker ps

    The container of dapr init includes dapr_placement, dapr_redis, dapr_zipkin

  4. Verify the component directory, open the directory% USERPROFILE%\.dapr \, and see the following structure, which indicates that the installation is complete

Run a sample code to see the effect

Operation environment preparation

vs 2022/2019, it is recommended to experience 64 bit VS directly on VS2022


. net 6. If you are not using vs2022, you need to install. Net 6 yourself


Download the sample code from Github

Clone the code base locally using the command line tool

ssh (recommended) use the following command

git clone git@github.com:doddgu/dapr-study-room.git

https use the following command

git clone https://github.com/doddgu/dapr-study-room.git

If you don't have git, you need to install git first. You can Baidu for specific tutorials


Run the. Net sample code using Dapr

  1. Use the command line tool to jump to the source directory dapr study room \ assignment03 \ assignment.server

    Tips: find the source directory in Explorer

    Win 11 can be opened by right clicking the blank area - > in the Windows terminal

    Win 11 enter cmd in the following Explorer

  2. Execute the command dapr run -- app ID assignment server -- app port 5038 dotnet run

    You don't need to understand the meaning of the command line first. It will be explained in subsequent chapters

  3. See the input as follows: the operation is successful

    == APP == info: Microsoft.Hosting.Lifetime[14]
    == APP ==       Now listening on: https://localhost:7038
    == APP == info: Microsoft.Hosting.Lifetime[14]
    == APP ==       Now listening on: http://localhost:5038
    == APP == info: Microsoft.Hosting.Lifetime[0]
    == APP ==       Application started. Press Ctrl+C to shut down.
    == APP == info: Microsoft.Hosting.Lifetime[0]
    == APP ==       Hosting environment: Development
    == APP == info: Microsoft.Hosting.Lifetime[0]
    == APP ==       Content root path: D:\Project\OpenSource\dapr-study-room\Assignment03\Assignment.Server
    time="2021-10-27T18:06:11.8422605+08:00" level=info msg="application discovered on port 5038" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime type=log ver=1.4.3
    time="2021-10-27T18:06:11.8788949+08:00" level=info msg="application configuration loaded" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime type=log ver=1.4.3
    time="2021-10-27T18:06:11.8803982+08:00" level=info msg="actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime.actor type=log ver=1.4.3
    time="2021-10-27T18:06:11.8844485+08:00" level=info msg="dapr initialized. Status: Running. Init Elapsed 1830.3305ms" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime type=log ver=1.4.3
    time="2021-10-27T18:06:11.8920835+08:00" level=info msg="placement tables updated, version: 0" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime.actor.internal.placement type=log ver=1.4.3
  4. There is also a verification method. Use the command line tool to execute the command dapr list

    APP ID, APP PORT and COMMAND are unchanged, and the rest are changed


    assignment-server 49948 49949 5038 dotnet run 13m 2021-10-27 18:06.09 22596

Call Dapr API

  1. Reopen a command line tool

  2. Test the Dapr API and open a command line tool

    dapr invoke --app-id assignment-server --method hello
  3. Verify that the operation was successful

    The command line window output of executing dapr inovoke is as follows

    App invoked successfully

    The output of the command line window executing dapr run is as follows

    == APP == Hello World!

    The Hello World is printed out by the API /hello of Assignment.Server. At this time, your environment is ok and the first example runs successfully

Give yourself a compliment 👍

During the construction of this environment, we can see that dapr is still a little "immature". After all, it is only version 1.4 now, and there is still a long way to go

However, this does not hinder the progress of some ideas brought by him. If you miss the first wave of microservices and Service Mesh, the distributed application runtime (Dapr) can be your new starting point

Source code of this chapter



We are moving towards a new framework and a new ecology

Our goal is free, easy to use, flexible, functional and robust.

So we are learning from the design concept of Building blocks and are making a new framework, MASA Framework. What are its characteristics?

  • The native supports Dapr and allows Dapr to be replaced by traditional communication methods
  • The architecture is unlimited, and single applications, SOA and microservices are supported
  • Support. Net native framework, reduce learning burden, and insist on not making new wheels except for concepts that must be introduced in specific fields
  • Rich ecological support, in addition to the framework, there are a series of products such as component library, permission center, configuration center, troubleshooting center, alarm center and so on
  • The unit test coverage of the core code base is 90%+
  • Open source, free, community driven
  • What else? We're waiting for you to discuss it together

After several months of production project practice, POC has been completed, and the previous accumulation is being reconstructed into a new open source project

At present, the source code has been synchronized to Github (the document site is under planning and will be gradually improved):







QQ group: 7424099

Wechat group: add technology operation wechat (MasaStackTechOps), note the purpose, and invite to join the group

Reprinted from: (Guiguzi)

Posted by idris on Tue, 02 Nov 2021 18:48:13 -0700