Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvariantCulture to fix localization #1053

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions bindings/csharp/http/batch/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 System.IO;

using Microsoft.AspNetCore.Localization;
using System.Globalization;
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

Expand All @@ -28,20 +27,32 @@ limitations under the License.
var daprUrl = $"{baseURL}:{daprPort}/v1.0/bindings/{sqlBindingName}";

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var invariantCulture = CultureInfo.InvariantCulture;
options.DefaultRequestCulture = new RequestCulture(invariantCulture);
options.SupportedCultures = [invariantCulture];
});

var app = builder.Build();

if (app.Environment.IsDevelopment()) {app.UseDeveloperExceptionPage();}
if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); }

app.UseRequestLocalization();

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..");

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 +61,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
30 changes: 20 additions & 10 deletions bindings/csharp/sdk/batch/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,45 @@ 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 System.IO;
using System.Text;

using Microsoft.AspNetCore.Localization;
using System.Globalization;
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

var cronBindingName = "cron";
var sqlBindingName = "sqldb";

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var invariantCulture = CultureInfo.InvariantCulture;
options.DefaultRequestCulture = new RequestCulture(invariantCulture);
options.SupportedCultures = [invariantCulture];
});

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.UseRequestLocalization();

// Triggered by Dapr input binding
app.MapPost("/" + cronBindingName, async () =>
{
Console.WriteLine("Processing batch..");

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
Loading