You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/courses/dapr-aspire/service-invocation.md
+334-4Lines changed: 334 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,337 @@ tags: ['.NET Aspire', 'DAPR']
6
6
weight: 2
7
7
---
8
8
9
-
-[ ] Overview
10
-
-[ ] Service Invocation with Aspire
11
-
-[ ] Service Invocation with DAPR
12
-
-[ ] Deployment
9
+
This article shows how to use `Service Invocation` with `DAPR`'s building block, combined with `.NET Aspire` for orchestration.
10
+
11
+
You'll learn how services can communicate reliably in a distributed environment, with `Aspire` managing the setup and `DAPR` providing service discovery and resilient invocation patterns.
12
+
13
+
## Resources
14
+
15
+
### 👩💻 Source Code
16
+
17
+
If you are stuck, you can refer the final source code, available at [GitHub Repository](https://github.com/NetRecipes/service-invocation)
18
+
19
+
## What is Service Invocation, and Why It Matters?
20
+
21
+
**Service invocation** is the ability of one service to call another service's API in a distributed system.
22
+
23
+
In microservices architectures, services need to communicate with each other — an order service might call a pricing service, or a frontend might call multiple backend APIs.
24
+
25
+
Without proper service invocation patterns, you face challenges like hardcoded URLs, lack of service discovery, no retry logic, and difficult debugging across service boundaries.
26
+
27
+
For example, think of an e‑commerce system where `ServiceA` needs to calculate discounted prices by calling `ServiceB`'s discount calculation endpoint.
28
+
Without `DAPR`, you'd need to:
29
+
30
+
- Manually manage service URLs and ports
31
+
- Implement your own retry and timeout logic
32
+
- Handle service discovery yourself
33
+
- Deal with networking complexities
34
+
35
+
`DAPR` solves this by providing a simple, consistent API for service-to-service communication, while `Aspire` orchestrates the environment so services can discover and call each other seamlessly.
36
+
37
+
You get built-in service discovery, automatic retries, distributed tracing, and the flexibility to switch between different invocation methods — all without changing your service code.
38
+
39
+
> 💡 **Note:**`DAPR`'s service invocation isn't just about making HTTP calls — it provides resilience patterns, observability, and security features that would otherwise require significant custom implementation.
40
+
41
+
## Hands‑On Setup
42
+
43
+
We'll scaffold a new `.NET Aspire` solution and add two Web API services that communicate with each other.
44
+
45
+
Each command below is shown individually with its purpose explained.
46
+
47
+
### 1. Create the Aspire host project
48
+
49
+
This sets up the orchestration project named `ServiceInvocation`.
50
+
51
+
```bash
52
+
dotnet new aspire --name ServiceInvocation --no-https --output .
53
+
```
54
+
55
+
### 2. Create the 2 Web API services
56
+
57
+
Generates 2 Web API projects called `ServiceA` & `ServiceB` using controllers.
58
+
59
+
And add both services to the solution
60
+
61
+
```bash
62
+
dotnet new webapi --name ServiceA --no-https --use-controllers
63
+
dotnet new webapi --name ServiceB --no-https --use-controllers
64
+
dotnet sln add .\ServiceA\ .\ServiceB\
65
+
```
66
+
67
+
### 3. Migrate to the new `.slnx` format (Optional)
68
+
69
+
Converts the solution to the modern format used by Aspire.
70
+
And cleans up the legacy solution file, leaving only `ServiceInvocation.slnx`.
71
+
72
+
```bash
73
+
dotnet sln migrate
74
+
rm ServiceInvocation.sln
75
+
```
76
+
77
+
> 💡 **Note:** The newer `.slnx` solution format is a general .NET enhancement. It's cleaner and more minimal than the traditional `.sln`, reducing boilerplate and making solutions easier to manage in modern .NET projects.
78
+
79
+
Now, open `ServiceInvocation.slnx` with `Visual Studio` or `Rider`, or simply open the directory with `VS Code`.
80
+
81
+
Alternatively, you can skip these steps and clone the final companion repository: [NetRecipes/service-invocation](https://github.com/NetRecipes/service-invocation).
82
+
83
+
## NuGet Packages
84
+
85
+
Depending on your IDE, install the following NuGet packages in the specified projects:
86
+
87
+
### ServiceA and ServiceB
88
+
89
+
In both `ServiceA` and `ServiceB` projects, install the following NuGet packages to enable `DAPR` integration, API documentation, and UI enhancements.
90
+
91
+
(Note: `Microsoft.AspNetCore.OpenApi` is usually included by default in the Web API template, so you may not need to install it separately.)
92
+
93
+
| Package ID | Purpose |
94
+
|------------|---------|
95
+
|[Dapr.AspNetCore](https://www.nuget.org/packages/Dapr.AspNetCore)| Adds DAPR integration to ASP.NET Core, including service invocation, state management, pub/sub, and bindings. |
96
+
|[Swashbuckle.AspNetCore.SwaggerUI](https://www.nuget.org/packages/Swashbuckle.AspNetCore.SwaggerUI)| Provides Swagger UI for interactive API documentation and testing. |
97
+
|[AspNetCore.SwaggerUI.Themes](https://www.nuget.org/packages/AspNetCore.SwaggerUI.Themes)| Adds modern, customizable themes to Swagger UI for better developer experience. |
In the `ServiceInvocation.AppHost` project, install the following NuGet package to enable integration with `DAPR` sidecars.
108
+
109
+
| Package ID | Purpose |
110
+
|------------|---------|
111
+
|[CommunityToolkit.Aspire.Hosting.Dapr](https://www.nuget.org/packages/CommunityToolkit.Aspire.Hosting.Dapr)| Integrates DAPR sidecars into an Aspire application, enabling service orchestration with DAPR building blocks. |
Support for mTLS, access control policies, and API token authentication.
330
+
Services can communicate securely without manual certificate management.
331
+
332
+
-[x] Flexibility
333
+
Switch between HTTP, gRPC, or other protocols without changing service code.
334
+
`DAPR` abstracts the communication layer.
335
+
336
+
## Summary
337
+
338
+
With `DAPR` and `Aspire`, you can build resilient, discoverable service-to-service communication with minimal boilerplate.
339
+
340
+
Instead of managing URLs, retries, and timeouts manually, DAPR handles these concerns automatically — thanks to `DAPR`'s abstraction and `Aspire`'s orchestration.
341
+
342
+
Your services communicate by app ID, not by URL, making them more maintainable and cloud-ready from day one.
0 commit comments