mirror of https://github.com/dapr/docs.git
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:
parent
79f25d6049
commit
f4d95a46cc
|
|
@ -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 %}}
|
||||
|
|
|
|||
|
|
@ -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 %}}
|
||||
|
|
|
|||
Loading…
Reference in New Issue