Skip to content

Commit

Permalink
InvariantCulture
Browse files Browse the repository at this point in the history
Signed-off-by: Erwin Kramer <[email protected]>
  • Loading branch information
erwinkramer committed Jul 20, 2024
1 parent 46f1b0a commit 224104c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
25 changes: 16 additions & 9 deletions bindings/csharp/http/batch/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<Orders>(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<DaprPayload>(payload);
Expand All @@ -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();
});

Expand Down
17 changes: 11 additions & 6 deletions bindings/csharp/sdk/batch/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<Orders>(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<string,string>(){
var command = new Dictionary<string, string>(){
{"sql",
sqlText}
};
Expand Down

0 comments on commit 224104c

Please sign in to comment.