Skip to content

Commit f4d95a4

Browse files
WhitWaldomsfussell
andauthored
Modernized .NET examples for binding inputs and outputs (#4621)
* Modernized .NET example to include modern syntax for controller example and add a minimal API version for completeness Signed-off-by: Whit Waldo <[email protected]> * Removed //dependencies text as no one does this Signed-off-by: Whit Waldo <[email protected]> * Modernized example for binding outputs in .NET Signed-off-by: Whit Waldo <[email protected]> * Update daprdocs/content/en/developing-applications/building-blocks/bindings/howto-bindings.md Co-authored-by: Mark Fussell <[email protected]> Signed-off-by: Whit Waldo <[email protected]> --------- Signed-off-by: Whit Waldo <[email protected]> Co-authored-by: Mark Fussell <[email protected]>
1 parent 79f25d6 commit f4d95a4

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

daprdocs/content/en/developing-applications/building-blocks/bindings/howto-bindings.md

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,40 +110,30 @@ The code examples below leverage Dapr SDKs to invoke the output bindings endpoin
110110

111111
{{% codetab %}}
112112

113+
Here's an example of using a console app with top-level statements in .NET 6+:
114+
113115
```csharp
114-
//dependencies
115-
using System;
116-
using System.Collections.Generic;
117-
using System.Net.Http;
118-
using System.Net.Http.Headers;
116+
using System.Text;
119117
using System.Threading.Tasks;
120118
using Dapr.Client;
121-
using Microsoft.AspNetCore.Mvc;
122-
using System.Threading;
123119
124-
//code
125-
namespace EventService
120+
var builder = WebApplication.CreateBuilder(args);
121+
builder.Services.AddDaprClient();
122+
var app = builder.Build();
123+
124+
const string BINDING_NAME = "checkout";
125+
const string BINDING_OPERATION = "create";
126+
127+
var random = new Random();
128+
using var daprClient = app.Services.GetRequiredService<DaprClient>();
129+
130+
while (true)
126131
{
127-
class Program
128-
{
129-
static async Task Main(string[] args)
130-
{
131-
string BINDING_NAME = "checkout";
132-
string BINDING_OPERATION = "create";
133-
while(true)
134-
{
135-
System.Threading.Thread.Sleep(5000);
136-
Random random = new Random();
137-
int orderId = random.Next(1,1000);
138-
using var client = new DaprClientBuilder().Build();
139-
//Using Dapr SDK to invoke output binding
140-
await client.InvokeBindingAsync(BINDING_NAME, BINDING_OPERATION, orderId);
141-
Console.WriteLine("Sending message: " + orderId);
142-
}
143-
}
144-
}
132+
await Task.Delay(TimeSpan.FromSeconds(5));
133+
var orderId = random.Next(1, 1000);
134+
await client.InvokeBindingAsync(BINDING_NAME, BINDING_OPERATION, orderId);
135+
Console.WriteLine($"Sending message: {orderId}");
145136
}
146-
147137
```
148138

149139
{{% /codetab %}}

daprdocs/content/en/developing-applications/building-blocks/bindings/howto-triggers.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,35 @@ Below are code examples that leverage Dapr SDKs to demonstrate an input binding.
119119

120120
{{% codetab %}}
121121

122+
The following example demonstrates how to configure an input binding using ASP.NET Core controllers.
123+
122124
```csharp
123-
//dependencies
124125
using System.Collections.Generic;
125126
using System.Threading.Tasks;
126127
using System;
127128
using Microsoft.AspNetCore.Mvc;
128129
129-
//code
130-
namespace CheckoutService.controller
130+
namespace CheckoutService.controller;
131+
132+
[ApiController]
133+
public sealed class CheckoutServiceController : ControllerBase
131134
{
132-
[ApiController]
133-
public class CheckoutServiceController : Controller
135+
[HttpPost("/checkout")]
136+
public ActionResult<string> getCheckout([FromBody] int orderId)
134137
{
135-
[HttpPost("/checkout")]
136-
public ActionResult<string> getCheckout([FromBody] int orderId)
137-
{
138-
Console.WriteLine("Received Message: " + orderId);
139-
return "CID" + orderId;
140-
}
138+
Console.WriteLine($"Received Message: {orderId}");
139+
return $"CID{orderId}";
141140
}
142141
}
142+
```
143143

144+
The following example demonstrates how to configure the same input binding using a minimal API approach:
145+
```csharp
146+
app.MapPost("checkout", ([FromBody] int orderId) =>
147+
{
148+
Console.WriteLine($"Received Message: {orderId}");
149+
return $"CID{orderId}"
150+
});
144151
```
145152

146153
{{% /codetab %}}

0 commit comments

Comments
 (0)