From d7d7d10cf403d6d369674864cc7984cbab426199 Mon Sep 17 00:00:00 2001 From: Erwin Kramer Date: Sat, 20 Jul 2024 16:23:50 +0200 Subject: [PATCH 1/3] InvariantCulture Signed-off-by: Erwin Kramer --- bindings/csharp/http/batch/program.cs | 25 ++++++++++++++++--------- bindings/csharp/sdk/batch/program.cs | 17 +++++++++++------ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/bindings/csharp/http/batch/program.cs b/bindings/csharp/http/batch/program.cs index 2775ce449..200e1e0d2 100644 --- a/bindings/csharp/http/batch/program.cs +++ b/bindings/csharp/http/batch/program.cs @@ -16,7 +16,7 @@ limitations under the License. using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; - +using System.Globalization; //dapr run --app-id batch-http --app-port 7001 --resources-path ../../../components -- dotnet run @@ -30,18 +30,23 @@ limitations under the License. var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); -if (app.Environment.IsDevelopment()) {app.UseDeveloperExceptionPage();} +if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // Triggered by Dapr input binding -app.MapPost("/" + cronBindingName, async () => { +app.MapPost("/" + cronBindingName, async () => +{ Console.WriteLine("Processing batch.."); + // Ensure the culture is set to one that uses dot notation + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + string jsonFile = File.ReadAllText("../../../orders.json"); var ordersArray = JsonSerializer.Deserialize(jsonFile); - foreach(Order ord in ordersArray?.orders ?? new Order[] {}){ + foreach (Order ord in ordersArray?.orders ?? new Order[] { }) + { var sqlText = $"insert into orders (orderid, customer, price) values ({ord.OrderId}, '{ord.Customer}', {ord.Price});"; var payload = new DaprPayload(sql: new DaprPostgresBindingMetadata(cmd: sqlText), operation: "exec"); var orderJson = JsonSerializer.Serialize(payload); @@ -50,19 +55,21 @@ limitations under the License. Console.WriteLine(sqlText); // Insert order using Dapr output binding via HTTP Post - try { + try + { var resp = await httpClient.PostAsync(daprUrl, content); resp.EnsureSuccessStatusCode(); - } - catch (HttpRequestException e) { + } + catch (HttpRequestException e) + { Console.WriteLine(e.ToString()); throw e; } } - + Console.WriteLine("Finished processing batch"); - + return Results.Ok(); }); diff --git a/bindings/csharp/sdk/batch/program.cs b/bindings/csharp/sdk/batch/program.cs index 60dd6225e..a8c5d8544 100644 --- a/bindings/csharp/sdk/batch/program.cs +++ b/bindings/csharp/sdk/batch/program.cs @@ -17,7 +17,7 @@ limitations under the License. using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; using Dapr.Client; - +using System.Globalization; // dapr run --app-id batch-sdk --app-port 7002 --resources-path ../../../components -- dotnet run @@ -27,18 +27,23 @@ limitations under the License. var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); -if (app.Environment.IsDevelopment()) {app.UseDeveloperExceptionPage();} +if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // Triggered by Dapr input binding -app.MapPost("/" + cronBindingName, async () => { - +app.MapPost("/" + cronBindingName, async () => +{ Console.WriteLine("Processing batch.."); + + // Ensure the culture is set to one that uses dot notation + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + string jsonFile = File.ReadAllText("../../../orders.json"); var ordersArray = JsonSerializer.Deserialize(jsonFile); using var client = new DaprClientBuilder().Build(); - foreach(Order ord in ordersArray?.orders ?? new Order[] {}){ + foreach (Order ord in ordersArray?.orders ?? new Order[] { }) + { var sqlText = $"insert into orders (orderid, customer, price) values ({ord.OrderId}, '{ord.Customer}', {ord.Price});"; - var command = new Dictionary(){ + var command = new Dictionary(){ {"sql", sqlText} }; From a314d7afdf963a7de87cf00b392419633f56cc6d Mon Sep 17 00:00:00 2001 From: Erwin Kramer Date: Sat, 20 Jul 2024 16:31:23 +0200 Subject: [PATCH 2/3] alphabetical Signed-off-by: Erwin Kramer --- bindings/csharp/http/batch/program.cs | 2 +- bindings/csharp/sdk/batch/program.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/csharp/http/batch/program.cs b/bindings/csharp/http/batch/program.cs index 200e1e0d2..1f9a3c245 100644 --- a/bindings/csharp/http/batch/program.cs +++ b/bindings/csharp/http/batch/program.cs @@ -11,12 +11,12 @@ You may obtain a copy of the License at limitations under the License. */ using System; +using System.Globalization; using System.IO; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; -using System.Globalization; //dapr run --app-id batch-http --app-port 7001 --resources-path ../../../components -- dotnet run diff --git a/bindings/csharp/sdk/batch/program.cs b/bindings/csharp/sdk/batch/program.cs index a8c5d8544..31b0f6082 100644 --- a/bindings/csharp/sdk/batch/program.cs +++ b/bindings/csharp/sdk/batch/program.cs @@ -11,13 +11,13 @@ You may obtain a copy of the License at limitations under the License. */ using System; +using System.Globalization; using System.IO; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; using Dapr.Client; -using System.Globalization; // dapr run --app-id batch-sdk --app-port 7002 --resources-path ../../../components -- dotnet run From af8ee64b86cf959c1c136472f51abfcdb4622ef1 Mon Sep 17 00:00:00 2001 From: Erwin Kramer Date: Fri, 26 Jul 2024 09:41:16 +0200 Subject: [PATCH 3/3] implement invariantCulture inside WebApplicationBuilder Signed-off-by: Erwin Kramer --- bindings/csharp/http/batch/program.cs | 18 ++++++++++++------ bindings/csharp/sdk/batch/program.cs | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/bindings/csharp/http/batch/program.cs b/bindings/csharp/http/batch/program.cs index 1f9a3c245..4535696dd 100644 --- a/bindings/csharp/http/batch/program.cs +++ b/bindings/csharp/http/batch/program.cs @@ -10,13 +10,12 @@ You may obtain a copy of the License at See the License for the specific language governing permissions and limitations under the License. */ -using System; + +using Microsoft.AspNetCore.Localization; using System.Globalization; -using System.IO; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; -using Microsoft.AspNetCore.Mvc; //dapr run --app-id batch-http --app-port 7001 --resources-path ../../../components -- dotnet run @@ -28,10 +27,20 @@ limitations under the License. var daprUrl = $"{baseURL}:{daprPort}/v1.0/bindings/{sqlBindingName}"; var builder = WebApplication.CreateBuilder(args); + +builder.Services.Configure(options => +{ + var invariantCulture = CultureInfo.InvariantCulture; + options.DefaultRequestCulture = new RequestCulture(invariantCulture); + options.SupportedCultures = [invariantCulture]; +}); + var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } +app.UseRequestLocalization(); + var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); @@ -40,9 +49,6 @@ limitations under the License. { Console.WriteLine("Processing batch.."); - // Ensure the culture is set to one that uses dot notation - CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; - string jsonFile = File.ReadAllText("../../../orders.json"); var ordersArray = JsonSerializer.Deserialize(jsonFile); foreach (Order ord in ordersArray?.orders ?? new Order[] { }) diff --git a/bindings/csharp/sdk/batch/program.cs b/bindings/csharp/sdk/batch/program.cs index 31b0f6082..354d95410 100644 --- a/bindings/csharp/sdk/batch/program.cs +++ b/bindings/csharp/sdk/batch/program.cs @@ -10,13 +10,11 @@ You may obtain a copy of the License at See the License for the specific language governing permissions and limitations under the License. */ -using System; + +using Microsoft.AspNetCore.Localization; using System.Globalization; -using System.IO; -using System.Text; using System.Text.Json; using System.Text.Json.Serialization; -using Microsoft.AspNetCore.Mvc; using Dapr.Client; // dapr run --app-id batch-sdk --app-port 7002 --resources-path ../../../components -- dotnet run @@ -25,18 +23,25 @@ limitations under the License. var sqlBindingName = "sqldb"; var builder = WebApplication.CreateBuilder(args); + +builder.Services.Configure(options => +{ + var invariantCulture = CultureInfo.InvariantCulture; + options.DefaultRequestCulture = new RequestCulture(invariantCulture); + options.SupportedCultures = [invariantCulture]; +}); + var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } +app.UseRequestLocalization(); + // Triggered by Dapr input binding app.MapPost("/" + cronBindingName, async () => { Console.WriteLine("Processing batch.."); - // Ensure the culture is set to one that uses dot notation - CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; - string jsonFile = File.ReadAllText("../../../orders.json"); var ordersArray = JsonSerializer.Deserialize(jsonFile); using var client = new DaprClientBuilder().Build();