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 <whit.waldo@innovian.net>

* Removed //dependencies text as no one does this

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

* Modernized example for binding outputs in .NET

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

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

Co-authored-by: Mark Fussell <markfussell@gmail.com>
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

---------

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
Co-authored-by: Mark Fussell <markfussell@gmail.com>
This commit is contained in:
Whit Waldo 2025-05-09 18:17:22 -05:00 committed by GitHub
parent 79f25d6049
commit f4d95a46cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 39 deletions

View File

@ -110,40 +110,30 @@ The code examples below leverage Dapr SDKs to invoke the output bindings endpoin
{{% codetab %}}
Here's an example of using a console app with top-level statements in .NET 6+:
```csharp
//dependencies
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
//code
namespace EventService
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDaprClient();
var app = builder.Build();
const string BINDING_NAME = "checkout";
const string BINDING_OPERATION = "create";
var random = new Random();
using var daprClient = app.Services.GetRequiredService<DaprClient>();
while (true)
{
class Program
{
static async Task Main(string[] args)
{
string BINDING_NAME = "checkout";
string BINDING_OPERATION = "create";
while(true)
{
System.Threading.Thread.Sleep(5000);
Random random = new Random();
int orderId = random.Next(1,1000);
using var client = new DaprClientBuilder().Build();
//Using Dapr SDK to invoke output binding
await client.InvokeBindingAsync(BINDING_NAME, BINDING_OPERATION, orderId);
Console.WriteLine("Sending message: " + orderId);
}
}
}
await Task.Delay(TimeSpan.FromSeconds(5));
var orderId = random.Next(1, 1000);
await client.InvokeBindingAsync(BINDING_NAME, BINDING_OPERATION, orderId);
Console.WriteLine($"Sending message: {orderId}");
}
```
{{% /codetab %}}

View File

@ -119,28 +119,35 @@ Below are code examples that leverage Dapr SDKs to demonstrate an input binding.
{{% codetab %}}
The following example demonstrates how to configure an input binding using ASP.NET Core controllers.
```csharp
//dependencies
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Mvc;
//code
namespace CheckoutService.controller
namespace CheckoutService.controller;
[ApiController]
public sealed class CheckoutServiceController : ControllerBase
{
[ApiController]
public class CheckoutServiceController : Controller
[HttpPost("/checkout")]
public ActionResult<string> getCheckout([FromBody] int orderId)
{
[HttpPost("/checkout")]
public ActionResult<string> getCheckout([FromBody] int orderId)
{
Console.WriteLine("Received Message: " + orderId);
return "CID" + orderId;
}
Console.WriteLine($"Received Message: {orderId}");
return $"CID{orderId}";
}
}
```
The following example demonstrates how to configure the same input binding using a minimal API approach:
```csharp
app.MapPost("checkout", ([FromBody] int orderId) =>
{
Console.WriteLine($"Received Message: {orderId}");
return $"CID{orderId}"
});
```
{{% /codetab %}}