kunio_kun
kunio_kun
CC#
Created by kunio_kun on 1/8/2024 in #help
CosmosDB Linq GroupBy
Hi, it is my first project using CosmosDB and it seems like GroupBy is not supported in CosmosDB Linq. Is there any workarounds for this? I prefer not writing the SQL-like syntax if possible, and not pulling everything from the db and process in memory.. Thanks in advance
3 replies
CC#
Created by kunio_kun on 9/4/2023 in #help
❔ Avalonia Apply Theme at Design Time
Hi, I've just getting started with Avalonia and I'm trying to use the Semi.Avalonia theme. I included the theme as instructed in the github but the designer in Avalonia Rider still previews in Fluent light theme. Is there a way to make the preview follow the theme we set in <Application.Styles> <StyleInclude Source="avares://Semi.Avalonia/Themes/Index.axaml"></StyleInclude> </Application.Styles> ? Thanks in advance
2 replies
CC#
Created by kunio_kun on 5/13/2023 in #help
❔ Serving SPA on ASP.NET Core MVC Web API
Hi, I'm trying to serve SPA in a folder client in BaseDirectory. I want it to be served in "/". Here's what I tried
WebApplication app = builder.Build();
app.UseCors(options => {
options.AllowAnyOrigin();
options.AllowAnyHeader();
options.AllowAnyMethod();
});
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

app.MapHub<DevicesHub>("api/hubs/devices");
app.MapControllers();
app.UseSpa(spa => {
spa.Options.SourcePath = Path.Join(app.Environment.ContentRootPath, "client");
});
app.MapFallbackToController("Index", "Fallback");
WebApplication app = builder.Build();
app.UseCors(options => {
options.AllowAnyOrigin();
options.AllowAnyHeader();
options.AllowAnyMethod();
});
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

app.MapHub<DevicesHub>("api/hubs/devices");
app.MapControllers();
app.UseSpa(spa => {
spa.Options.SourcePath = Path.Join(app.Environment.ContentRootPath, "client");
});
app.MapFallbackToController("Index", "Fallback");
And the fallback controller implementation is
public class Fallback: ControllerBase
{
public IActionResult Index()
{
return File("~/client/index.html", "text/html");
}
}
public class Fallback: ControllerBase
{
public IActionResult Index()
{
return File("~/client/index.html", "text/html");
}
}
But when i navigate to /, I am getting
FileNotFoundException: Could not find file 'ProjectDirectory\bin\Debug\net7.0\client'.
FileNotFoundException: Could not find file 'ProjectDirectory\bin\Debug\net7.0\client'.
How do I do this correctly? Thanks in advance.
24 replies
CC#
Created by kunio_kun on 4/5/2023 in #help
✅ Inserting record with one-to-many relationship EF Core
Hi, I'm trying to insert a record that has one-to-many relationship with other entities which leads to Foregin Key Constraint failed. My current types look like these
using System.ComponentModel.DataAnnotations;

namespace Shared.Models;

public static class TipeCustomer
{
public const string Personal = "Personal";
public const string Perusahaan = "Perusahaan";
}

public class Customer
{
public int Id { get; set; }
[Display(Name = "Tipe")]
public string TipeCustomer { get; set; } = Models.TipeCustomer.Perusahaan;
[Display(Name = "Nama")]
public string Nama { get; set; } = null!;
[Display(Name = "Email")]
public string? Email { get; set; }
[Display(Name = "Nomor Telepon")]
public string? NomorTelepon { get; set; }
[Display(Name = "Kota")]
public string? Kota { get; set; }
[Display(Name = "Keterangan")]
public string? Keterangan { get; set; }
}
using System.ComponentModel.DataAnnotations;

namespace Shared.Models;

public static class TipeCustomer
{
public const string Personal = "Personal";
public const string Perusahaan = "Perusahaan";
}

public class Customer
{
public int Id { get; set; }
[Display(Name = "Tipe")]
public string TipeCustomer { get; set; } = Models.TipeCustomer.Perusahaan;
[Display(Name = "Nama")]
public string Nama { get; set; } = null!;
[Display(Name = "Email")]
public string? Email { get; set; }
[Display(Name = "Nomor Telepon")]
public string? NomorTelepon { get; set; }
[Display(Name = "Kota")]
public string? Kota { get; set; }
[Display(Name = "Keterangan")]
public string? Keterangan { get; set; }
}
34 replies
CC#
Created by kunio_kun on 12/23/2022 in #help
NuGet on Specific TFM in one project
I'm writing a class library that multitargets netstandard 2.1, and .net 6, and the library depends on System.Text.Json, which exisits built in .net 6. Is there a way to include System.Text.Json only for the build for netstandard 2.1?
1 replies
CC#
Created by kunio_kun on 11/23/2022 in #help
❔ Including wwwroot folder
Hi, I started a webapi ASP.NET Core project and now i need views and wwwroot stuff but I realized that wwwroot folder isn't copied on build. I've taken a look at projects with webapp template but i dont see wwwroot included manually in the csproj file. What is the correct way to do this? Thanks in advance
2 replies
CC#
Created by kunio_kun on 11/18/2022 in #help
❔ Check What Generic Type a Class Implements
Hi, I'm trying to find if a class implements ISomething<T> and what T is For example, to find Foo in this class implementing ISomething<T>
public class Something: ISomething<Foo>
public class Something: ISomething<Foo>
Is it possible? Thanks in advance
16 replies
CC#
Created by kunio_kun on 11/4/2022 in #help
ASP.NET Core Logging in EventsViewer Filter
3 replies
CC#
Created by kunio_kun on 10/31/2022 in #help
Update Multiple Entities in One Query EF Core
I'm looking for a way to update multiple entities, something that represents
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
I've googled around and answers seems to execute a select-where query first then update each entity using a loop, then update them one by one. Is there a way for this? Thanks in advance
16 replies
CC#
Created by kunio_kun on 10/28/2022 in #help
❔ ASP.NET Core MVC Put Value to Route on Form Submit
Hi, I have this action on Home controller
public async Task<IActionResult> Index(EventsFilterOptionsViewModel? filters, bool enableFilter)
public async Task<IActionResult> Index(EventsFilterOptionsViewModel? filters, bool enableFilter)
I want index to accept filter options on filters, but I realized that although that I don't enter any values in input fields, the model filters won't ever be null. So I can't check for filters to be null since it is not even null when the route is /. I was thinking maybe i can add that enableFilter into that action, and then have asp-route-enableFilter on my form,
<form asp-controller="Home" asp-action="Index" method="get" asp-route-enableFilter="true">
<form asp-controller="Home" asp-action="Index" method="get" asp-route-enableFilter="true">
but i also realized even if i submit the form, it does not put enableFilter query string on the route. Is there a way to achieve this? Thanks in advance
21 replies
CC#
Created by kunio_kun on 10/24/2022 in #help
ASP NET Core 6 Launch without Debugging doesn't block Background Service but Debug Does
Hi, I'm trying to have a ASP.NET Core BackgroundService run really in background, i mean really don't interrupt with request/response part and I've been trying many stuff
private Thread? _workerThread;

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Logger.LogInformation("Monitoring Event Triggering Start");
_workerThread = new Thread(() =>
{
while (!stoppingToken.IsCancellationRequested)
{
if (EventQueue.Count == 0) continue;
var monitoringEvent = EventQueue.Dequeue();
using (var scope = ServiceProvider.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.MonitoringEvents.Add(monitoringEvent);
db.SaveChanges();
}

// TODO Trigger events, notifications
Logger.LogInformation($"{monitoringEvent.DateTime} Rule {monitoringEvent.Message} triggered");
}
});
_workerThread.IsBackground = true;
_workerThread.Start();
}
private Thread? _workerThread;

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Logger.LogInformation("Monitoring Event Triggering Start");
_workerThread = new Thread(() =>
{
while (!stoppingToken.IsCancellationRequested)
{
if (EventQueue.Count == 0) continue;
var monitoringEvent = EventQueue.Dequeue();
using (var scope = ServiceProvider.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.MonitoringEvents.Add(monitoringEvent);
db.SaveChanges();
}

// TODO Trigger events, notifications
Logger.LogInformation($"{monitoringEvent.DateTime} Rule {monitoringEvent.Message} triggered");
}
});
_workerThread.IsBackground = true;
_workerThread.Start();
}
That is the code i currently have, and one weird thing is if I debug the code, the service blocks the program execution, while if I use run, it doesn't block and continue as expected. Is there any mistakes there? Also, is there other ways to do this? I tried Task.Run(), Task.Factory.StartNew() and they all block. I'm on Jetbrains Rider on Ubuntu 20.04 if that matters for the threading part. Thanks in advance!
155 replies
CC#
Created by kunio_kun on 10/13/2022 in #help
Pattern Matching to Check if 4 uint have the same value
Is there a shorter way so we dont have to check with equality operator for each combination?
15 replies
CC#
Created by kunio_kun on 10/12/2022 in #help
Getting Rid of Nullability Warnings
Hi, I currently have a lot of warnings with nullability enabled. For example, database entities, where I am sure that strings are required in the table, how to I get rid of the warning?
12 replies
CC#
Created by kunio_kun on 10/12/2022 in #help
Dependency Injection in ASP.NET Core and issue with null suppresion
Hi, I'm having some issues with null suppression and dependency injection in ASP.NET Core I have this block of code
builder.Services.AddDbContext<AppDb>();
builder.Services.AddHostedService(provider =>
new DbMigrator(provider.GetService<ILogger<DbMigrator>>()!, provider.GetService<AppDb>()!)
);
builder.Services.AddDbContext<AppDb>();
builder.Services.AddHostedService(provider =>
new DbMigrator(provider.GetService<ILogger<DbMigrator>>()!, provider.GetService<AppDb>()!)
);
And it wasn't able to resolve
Cannot resolve scoped service 'RemoteGatewayManager.Types.Classes.AppDb' from root provider
Cannot resolve scoped service 'RemoteGatewayManager.Types.Classes.AppDb' from root provider
I also tried with DbContext
builder.Services.AddDbContext<AppDb>();
builder.Services.AddHostedService(provider =>
new DbMigrator(provider.GetService<ILogger<DbMigrator>>()!, provider.GetService<AppDb>()!)
);
builder.Services.AddDbContext<AppDb>();
builder.Services.AddHostedService(provider =>
new DbMigrator(provider.GetService<ILogger<DbMigrator>>()!, provider.GetService<AppDb>()!)
);
And the
provider.GetService<AppDb>()!
provider.GetService<AppDb>()!
returns null, but doesn't throw. Isn't it supposed to throw for being null? Also why is it returning null instead of telling that it is not able to resolve service? Edit: AppDb is a class that inherrits from DbContext Thanks in advance
13 replies