mirror of https://github.com/dapr/docs.git
Merge branch 'v1.15' into dapr-latest-version-1.15.4
This commit is contained in:
commit
1ec5fc9f23
|
|
@ -72,38 +72,27 @@ The following example shows how to save and retrieve a single key/value pair usi
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
|
|
||||||
//dependencies
|
using System.Text;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
//code
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
namespace EventService
|
builder.Services.AddDaprClient();
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
var random = new Random();
|
||||||
|
//Resolve the DaprClient from its dependency injection registration
|
||||||
|
using var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
|
|
||||||
|
while(true)
|
||||||
{
|
{
|
||||||
class Program
|
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||||
{
|
var orderId = random.Next(1,1000);
|
||||||
static async Task Main(string[] args)
|
//Using Dapr SDK to save and get state
|
||||||
{
|
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
|
||||||
string DAPR_STORE_NAME = "statestore";
|
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
|
||||||
while(true) {
|
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, "order_1");
|
||||||
System.Threading.Thread.Sleep(5000);
|
Console.WriteLine($"Result after get: {result}");
|
||||||
using var client = new DaprClientBuilder().Build();
|
|
||||||
Random random = new Random();
|
|
||||||
int orderId = random.Next(1,1000);
|
|
||||||
//Using Dapr SDK to save and get state
|
|
||||||
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
|
|
||||||
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
|
|
||||||
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, "order_1");
|
|
||||||
Console.WriteLine("Result after get: " + result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -359,23 +348,20 @@ Below are code examples that leverage Dapr SDKs for deleting the state.
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
//code
|
const string DAPR_STORE_NAME = "statestore";
|
||||||
namespace EventService
|
|
||||||
{
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
class Program
|
builder.Services.AddDaprClient();
|
||||||
{
|
var app = builder.Build();
|
||||||
static async Task Main(string[] args)
|
|
||||||
{
|
//Resolve the DaprClient from the dependency injection registration
|
||||||
string DAPR_STORE_NAME = "statestore";
|
using var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
//Using Dapr SDK to delete the state
|
|
||||||
using var client = new DaprClientBuilder().Build();
|
//Use the DaprClient to delete the state
|
||||||
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
|
await client.DeleteStateAsync(DAPR_STORE_NAME, "order_1", cancellationToken: cancellationToken);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
To launch a Dapr sidecar for the above example application, run a command similar to the following:
|
To launch a Dapr sidecar for the above example application, run a command similar to the following:
|
||||||
|
|
@ -540,22 +526,19 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving multip
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
//code
|
using System.Threading.Tasks;
|
||||||
namespace EventService
|
|
||||||
{
|
const string DAPR_STORE_NAME = "statestore";
|
||||||
class Program
|
|
||||||
{
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
static async Task Main(string[] args)
|
builder.Services.AddDaprClient();
|
||||||
{
|
var app = builder.Build();
|
||||||
string DAPR_STORE_NAME = "statestore";
|
|
||||||
//Using Dapr SDK to retrieve multiple states
|
//Resolve the DaprClient from the dependency injection registration
|
||||||
using var client = new DaprClientBuilder().Build();
|
using var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
IReadOnlyList<BulkStateItem> multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List<string> { "order_1", "order_2" }, parallelism: 1);
|
|
||||||
}
|
IReadOnlyList<BulkStateItem> multipleStateResult = await client.GetBulkStateAsync(DAPR_STORE_NAME, new List<string> { "order_1", "order_2" }, parallelism: 1);
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
To launch a Dapr sidecar for the above example application, run a command similar to the following:
|
To launch a Dapr sidecar for the above example application, run a command similar to the following:
|
||||||
|
|
@ -567,28 +550,21 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
The above example returns a `BulkStateItem` with the serialized format of the value you saved to state. If you prefer that the value be deserialized by the SDK across each of your bulk response items, you can instead use the following:
|
The above example returns a `BulkStateItem` with the serialized format of the value you saved to state. If you prefer that the value be deserialized by the SDK across each of your bulk response items, you can instead use the following:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
//code
|
using System.Threading.Tasks;
|
||||||
namespace EventService
|
|
||||||
{
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
static async Task Main(string[] args)
|
|
||||||
{
|
|
||||||
string DAPR_STORE_NAME = "statestore";
|
|
||||||
//Using Dapr SDK to retrieve multiple states
|
|
||||||
using var client = new DaprClientBuilder().Build();
|
|
||||||
IReadOnlyList<BulkStateItem<Widget>> mulitpleStateResult = await client.GetBulkStateAsync<Widget>(DAPR_STORE_NAME, new List<string> { "widget_1", "widget_2" }, parallelism: 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Widget
|
const string DAPR_STORE_NAME = "statestore";
|
||||||
{
|
|
||||||
string Size { get; set; }
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
string Color { get; set; }
|
builder.Serivces.AddDaprClient();
|
||||||
}
|
var app = builder.Build();
|
||||||
}
|
|
||||||
|
//Resolve the DaprClient from the dependency injection registration
|
||||||
|
using var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
|
|
||||||
|
IReadOnlyList<BulkStateItem<Widget>> mulitpleStateResult = await client.GetBulkStateAsync<Widget>(DAPR_STORE_NAME, new List<string> { "widget_1", "widget_2" }, parallelism: 1);
|
||||||
|
|
||||||
|
record Widget(string Size, string Color);
|
||||||
```
|
```
|
||||||
|
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
@ -791,44 +767,36 @@ Below are code examples that leverage Dapr SDKs for performing state transaction
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//dependencies
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using System.Threading.Tasks;
|
||||||
using System.Threading;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
//code
|
const string DAPR_STORE_NAME = "statestore";
|
||||||
namespace EventService
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
builder.Serivces.AddDaprClient();
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
//Resolve the DaprClient from the dependency injection registration
|
||||||
|
using var client = app.Services.GetRequiredService<DaprClient>();
|
||||||
|
|
||||||
|
var random = new Random();
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
class Program
|
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||||
{
|
var orderId = random.Next(1, 1000);
|
||||||
static async Task Main(string[] args)
|
var requests = new List<StateTransactionRequest>
|
||||||
{
|
{
|
||||||
string DAPR_STORE_NAME = "statestore";
|
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
|
||||||
while(true) {
|
new StateTransactionRequest("order_2", null, StateOperationType.Delete)
|
||||||
System.Threading.Thread.Sleep(5000);
|
};
|
||||||
Random random = new Random();
|
var cancellationTokenSource = new CancellationTokenSource();
|
||||||
int orderId = random.Next(1,1000);
|
var cancellationToken = cancellationTokenSource.Token;
|
||||||
using var client = new DaprClientBuilder().Build();
|
|
||||||
var requests = new List<StateTransactionRequest>()
|
//Use the DaprClient to perform the state transactions
|
||||||
{
|
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
|
||||||
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
|
Console.WriteLine($"Order requested: {orderId}");
|
||||||
new StateTransactionRequest("order_2", null, StateOperationType.Delete)
|
Console.WriteLine($"Result: {result}");
|
||||||
};
|
|
||||||
CancellationTokenSource source = new CancellationTokenSource();
|
|
||||||
CancellationToken cancellationToken = source.Token;
|
|
||||||
//Using Dapr SDK to perform the state transactions
|
|
||||||
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
|
|
||||||
Console.WriteLine("Order requested: " + orderId);
|
|
||||||
Console.WriteLine("Result: " + result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue