From 3d9799f6d193b02621d0355801cbe0025111cf82 Mon Sep 17 00:00:00 2001 From: MregXN Date: Tue, 21 Nov 2023 11:21:13 +0800 Subject: [PATCH 1/3] use InvokeHttpClient in service invocation csharp sample Signed-off-by: MregXN --- .../csharp/http/checkout/Program.cs | 16 +++++++--------- .../csharp/http/checkout/checkout.csproj | 4 ++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/service_invocation/csharp/http/checkout/Program.cs b/service_invocation/csharp/http/checkout/Program.cs index 98206e140..76995f23f 100644 --- a/service_invocation/csharp/http/checkout/Program.cs +++ b/service_invocation/csharp/http/checkout/Program.cs @@ -1,21 +1,19 @@ using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +using Dapr.Client; -var baseURL = (Environment.GetEnvironmentVariable("BASE_URL") ?? "http://localhost") + ":" + (Environment.GetEnvironmentVariable("DAPR_HTTP_PORT") ?? "3500"); - -var client = new HttpClient(); -client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); -// Adding app id as part of the header -client.DefaultRequestHeaders.Add("dapr-app-id", "order-processor"); +var client = DaprClient.CreateInvokeHttpClient(appId: "order-processor"); for (int i = 1; i <= 20; i++) { var order = new Order(i); - var orderJson = JsonSerializer.Serialize(order); - var content = new StringContent(orderJson, Encoding.UTF8, "application/json"); + + var cts = new CancellationTokenSource(); + Console.CancelKeyPress += (object? sender, ConsoleCancelEventArgs e) => cts.Cancel(); // Invoking a service - var response = await client.PostAsync($"{baseURL}/orders", content); + var response = await client.PostAsJsonAsync("/orders", order, cts.Token); + Console.WriteLine("Order passed: " + order); await Task.Delay(TimeSpan.FromSeconds(1)); diff --git a/service_invocation/csharp/http/checkout/checkout.csproj b/service_invocation/csharp/http/checkout/checkout.csproj index 42ba9218c..810ef53d4 100644 --- a/service_invocation/csharp/http/checkout/checkout.csproj +++ b/service_invocation/csharp/http/checkout/checkout.csproj @@ -7,4 +7,8 @@ enable + + + + From cb2075f47026a705a9c681e3b8e7936fdba6f864 Mon Sep 17 00:00:00 2001 From: MregXN Date: Tue, 21 Nov 2023 13:54:40 +0800 Subject: [PATCH 2/3] add dependency Signed-off-by: MregXN --- service_invocation/csharp/http/checkout/checkout.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/service_invocation/csharp/http/checkout/checkout.csproj b/service_invocation/csharp/http/checkout/checkout.csproj index 810ef53d4..cff154542 100644 --- a/service_invocation/csharp/http/checkout/checkout.csproj +++ b/service_invocation/csharp/http/checkout/checkout.csproj @@ -9,6 +9,7 @@ + From 5097d9a42e987c28be9d52d58aa54fd182c2e549 Mon Sep 17 00:00:00 2001 From: MregXN Date: Tue, 21 Nov 2023 14:51:29 +0800 Subject: [PATCH 3/3] retrigger to pass validation Signed-off-by: MregXN --- service_invocation/csharp/http/checkout/checkout.csproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/service_invocation/csharp/http/checkout/checkout.csproj b/service_invocation/csharp/http/checkout/checkout.csproj index cff154542..99336c79b 100644 --- a/service_invocation/csharp/http/checkout/checkout.csproj +++ b/service_invocation/csharp/http/checkout/checkout.csproj @@ -7,9 +7,9 @@ enable - - - + + + - + \ No newline at end of file