The purpose of this demo is to show how to add Dapr to a new .Net app and debug it.
Open the demo_myapp.code-workspace file and click the Open Workspace button in the lower right corner.
This will reload your Codespace and scope your Explorer to just the folders needed for this demo.
To provision the infrastructure before the demo execute the following command in the terminal.
./demo.ps1 -deployOnly
Running local
dapr run --app-id myapp --app-port 5000 --dapr-http-port 3500 -- dotnet run --urls http://*:5000
All the commands below will be performed in a PowerShell terminal. Some of the commands require Trackyon.Utils
From the root of the workspace create and change into src/myapp:
mcd src/myapp
Now in the folder create a new web API project:
dotnet new webapi --no-https
At this point we can use Dapr with this app without changing a single line.
dapr run --app-id myapp --app-port 5000 --dapr-http-port 3500 -- dotnet run --urls http://*:5000
The application can be accessed at http://localhost:3500/v1.0/invoke/myapp/method/weatherforecast. Using Dapr to call the web service frees any clients from having to locate and secure communications to the service. To call another service or any component using Dapr simply target localhost:3500.
However, the application can still be accessed at http://localhost:5000/weatherforecast. This show the incremental adoption of Dapr. The service could start to use Dapr before existing clients are ready to migrate because the service is accessible via the orginal and Dapr addresses.
One of the benefits of using the Dapr address is you get observability for free. Visiting Zipkin and clicking Run Query will show the calls made via Dapr.
In the terminal press Ctrl + C to stop the application. Next add the Dapr package to the project.
dotnet add package dapr.aspnetcore
This step is optional because all the Dapr functionality can be accessed via HTTP or gRPC requests. However, adding the package will make working with Dapr even easier.
Open Program.cs. Update the call to AddControllers by adding AddDapr after it.
builder.Services.AddControllers().AddDapr();
This update enables dependency injection of the DaprClient class.
Next, after the call to UseAuthorization add UseCloudEvents.
app.UseCloudEvents();
This adds middleware to unwrap Cloud Events used with Pub/Sub.
Finally add a call to MapSubscribeHandler.
app.MapSubscribeHandler();
This allows code to subscribe to events via attributes. With those three changes in place we can turn this code into a stateful, event driven service.
By default this code returns random weather data. But a real system might return data stored from events posted to it about the current weather. So we are going to update the data model to have a location property and add code that get executed for each event and stores the data.
Open the WeatherForecast.cs file and add.
public string? Location { get; set; }
Open the WeatherForecastController.cs file and add.
[HttpPost]
[Dapr.Topic("pubsub", "new")]
public async Task<ActionResult<WeatherForecast>> PostWeatherForecast(WeatherForecast model, [FromServices] Dapr.Client.DaprClient daprClient)
{
await daprClient.SaveStateAsync<WeatherForecast>("statestore", model.Location, model);
return model;
}
Update the Get method to return a single WeatherForecast object.
[HttpGet(Name = "GetWeatherForecast")]
[Route("{location}")]
public async Task<WeatherForecast> Get(string location, [FromServices] Dapr.Client.DaprClient daprClient)
{
return await daprClient.GetStateAsync<WeatherForecast>("statestore", location);
}
With our code complete let's setup debugging of our Dapr application. Bring up the command palette (Press Ctrl + Shift + P on Windows and Linux, Cmd + Shift + P on Mac, or just press F1 on all platforms). Run the .NET: Generate Assets for Build and Debug command. This will create a launch.json and tasks.json file.
Next we need to execute the Dapr: Scaffold Dapr Tasks command. Select .NET Core Launch (web) for the first prompt. Set the Dapr Id to myapp and finally enter 5000 for the port.
Select Run and Debug from the Activity Bar then select .NET Core Launch (web) with Dapr (MyApp).
Now open the launch.json file by clicking the gear icon to the right of the dropdown. Local the .NET Core Launch (web) with Dapr (MyApp) section of the file and delete the serverReadyAction and sourceFileMap objects. This will prevent a browser being open each time we debug our service.
Local the env object and add.
"ASPNETCORE_URLS": "http://localhost:5000"
This will make sure our service starts on port 5000.
Notice the preLaunchTask and postDebugTask that start and stop Dapr and our application for us.
Open tasks.json and locate the dapr-debug section. Under the appPort add:
"httpPort": 3500,
"grpcPort": 50001,
This will make sure Dapr is listening for request on port 3500 for HTTP request and 50001 for gRPC requests.
With all the files updated from the Run menu select Start Debugging. As the code starts up open WeatherForecastController.cs and set a breakpoint in PostWeatherForecast and Get methods.
Finally, open sampleRequests.http. Click Send Request above the POST request. This will post an event to the pubsub component and the first break point will be hit. Press the Continue button in the debug toolbar to continue execution. This will save the weather forecast to the state store.
HTTP/1.1 204 No Content
Server: fasthttp
Date: Sun, 28 Aug 2022 23:41:07 GMT
Traceparent: 00-2f68a523e7142f460091cb0130793223-39ce8522c9953e35-01
Connection: close
Return to sampleRequests.http and click Send Request above the GET request. The second break will be hit. Press the Continue button in the debug toolbar to continue execution. This will read the previously saved weather forecast from the state store and return it.
HTTP/1.1 200 OK
Server: Kestrel
Date: Sun, 28 Aug 2022 23:40:10 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 85
Traceparent: 00-7def0499fe51d72e18f10cde11a306d3-cfe09fcf61f01ed5-01
Connection: close
[
{
"date": "0001-01-01T00:00:00",
"temperatureC": 46,
"temperatureF": 114,
"summary": "hot",
"location": "77379"
}
]
Press the Stop button on the debug toolbar or select Stop Debugging from the Run menu to stop debugging.
Running in Azure
dapr run --app-id azure --dapr-http-port 3500 --components-path ./components/azure
Running in AWS
dapr run --app-id aws --dapr-http-port 3500 --components-path ./components/aws
When you are done with the demo you can clean up the cloud resources by running the cleanUp.ps1 script using the following commands:
./cleanUp.ps1