Dusty
Dusty
CC#
Created by Dusty on 7/26/2023 in #help
❔ Markdown RegEx substituation
Hey, I need some help making a RegEx to convert StackExchange markdown to discord or rather CommonMark markdown. From:
<!-- language: lang-js -->
let a = null;

function doSomething() {
// abc
}

<!-- language: lang-html -->

<p>abc</p>
<!-- language: lang-js -->
let a = null;

function doSomething() {
// abc
}

<!-- language: lang-html -->

<p>abc</p>
To:
´´´js
let a = null;

function doSomething() {
// abc
}
´´´

´´´html
<p>abc</p>
´´´
´´´js
let a = null;

function doSomething() {
// abc
}
´´´

´´´html
<p>abc</p>
´´´
This is what I tried so far however I can't get it to work: https://regex101.com/r/mxeeiW/1
17 replies
CC#
Created by Dusty on 4/10/2023 in #help
✅ Refit / RestLess API SDK Structure
Hey! How do I structure an SDK for my API? Let's say I have 2 resources: Users and Groups. I could use Refit or RestLess to create an interface which wraps the CRUD operations on both resources. However if I add more resources (which I do have) to the interface it gets messy. Is there a way to structure my API SDK like this?
public interface IMyApi
{
public IMyUsersApi Users { get; }
public IMyGroupsApi Groups { get; }
}
public interface IMyApi
{
public IMyUsersApi Users { get; }
public IMyGroupsApi Groups { get; }
}
Where IMyUsersApi and IMyGroupsApi would contain all CRUD operations for their resource.
12 replies
CC#
Created by Dusty on 2/23/2023 in #help
✅ Set TimeZone for request duration in ASP.NET Core
Hey, I am generating PDFs on the server with DateTime objects. The problem is that my clients come from several different time zones and want the PDF to be generated in their own time zone. Question: How do I convert the DateTime object to a string using the time zone of the client? Should I just send the browsers time zone as query parameter or are there better approaches?
8 replies
CC#
Created by Dusty on 2/11/2023 in #help
✅ Soft Deletions with EF Core
Hey, instead of deleting the entry from the DB directly I am setting a IsDeleted property to true. Generic saving etc. works already. Now to my problem: I don't want to call .Where(x => !x.IsDeleted) every time I query an entity as it's very error prone. Is there any good design pattern you'd recommend to add this to (almost) every call? What I've tried so far are global query filters. The issue with that is that several entities depend on the entity that has this global query filter. If I remove the global query filter it works fine but then I do remove my tenant logic. So global query filters are not an option.
102 replies
CC#
Created by Dusty on 1/17/2023 in #help
✅ Is managed pointer (ref) or raw pointer (void*) null?
How can I check if a managed pointer or raw pointer points to an already garbage collected address? ```cs var sample = new Sample(); ref var refToSample = ref Unsafe.AsRef(in sample); sample = null; // How to check if sample got garbage collected? // this is false bool isNullRef = Unsafe.IsNullRef(ref refToSample); // How to check if the value the pointer is pointing at is garbage collected? void* ptr = Unsafe.AsPointer(ref refToSample);
20 replies
CC#
Created by Dusty on 1/16/2023 in #help
✅ Modify/prepend method implementation on build
Hey I was wondering if it is possible to modify a methods implementation. My first guess were source generators but they can only add code. So for example this
[ModifyImpl]
void DoStuff(string text)
{
Console.WriteLine(text);
}
[ModifyImpl]
void DoStuff(string text)
{
Console.WriteLine(text);
}
would be turned into
void DoStuff(string text)
{
// Some custom implementation before the users implementation e.g. an if clause

if (string.IsEmptyOrNull(text)
return;

Console.WriteLine(text);
}
void DoStuff(string text)
{
// Some custom implementation before the users implementation e.g. an if clause

if (string.IsEmptyOrNull(text)
return;

Console.WriteLine(text);
}
Any ideas?
12 replies
CC#
Created by Dusty on 12/31/2022 in #help
❔ Authorization handling in asp.net core
Hey, in asp.net core you can use Authorization Handlers and Type Filters to authorize a request. The first one needs a policy defined, the other one a custom attribute to authorize a request. When to use which and what are the pros and cons?
5 replies
CC#
Created by Dusty on 9/13/2022 in #help
Blazor WASM Authentication with separate APIs
I have a Blazor WASM app and an API for that client app. Now I'd like to decouple things and extract the authentication layer into it's own project an API. So I'd have 4 projects (in reality there're also Domain, Infrastructre etc. projects but for the sake of this example I wanna keep it simple): - MyProject.BlazorClient - MyProject.HostApi - MyProject.Shared (would contain Models, DBContext etc.) - MyProject.Authentication.Api (should basically act like auth0 and manage authentication) Purpose/use case: I would like to have a centralized authentication API like accounts.myproject.tld which could be used for other services on that domain like service1.myproject.tld and service2.myproject.tld without the need of 2 separate authentication providers. So like I said, basically a "self hosted auth0". Questions: 1. Are there any examples/tutorials/references of this available? Can't seem to find any and I don't want to reinvent the wheel 😅 2. How can I secure my HostApi with the authentication server? 3. How can I implement authentication with this pattern in blazor? Do I need to configure the Blazor WASM app to use one API for authentication and another one for data retrieval? If so, how?
5 replies
CC#
Created by Dusty on 9/6/2022 in #help
TaskCompletionSource.SetResult() executes on the same thread as the waiting one [Answered]
Hey, as the title already says I've found a deadlock in my code and would like to understand it. To give you an example:
// Called from Thread Id 1
await SomeMethodAsync();
// Continues on Thread Id 5

// await Task.Yield() - fixes the issue

Console.ReadKey(); // example to block the thread

class Sample
{

Task SomeMethodAsync()
{
var tcs = new TaskCompletionSource();
queue.Enqueue(tcs);
return tcs.Task;
}

void OnMessage()
{
// Called from Thread Id 5
var tcs = queue.Dequeue();
tcs.TrySetResult(); // Blocks the whole event loop

// Do some other stuff
}

}
// Called from Thread Id 1
await SomeMethodAsync();
// Continues on Thread Id 5

// await Task.Yield() - fixes the issue

Console.ReadKey(); // example to block the thread

class Sample
{

Task SomeMethodAsync()
{
var tcs = new TaskCompletionSource();
queue.Enqueue(tcs);
return tcs.Task;
}

void OnMessage()
{
// Called from Thread Id 5
var tcs = queue.Dequeue();
tcs.TrySetResult(); // Blocks the whole event loop

// Do some other stuff
}

}
So why is the awaited method continuing on the thread that called TrySetResult ?
29 replies
CC#
Created by Dusty on 9/5/2022 in #help
Generating random numbers (int32) [Answered]
As you may know Guid.NewGuid() returns a 128bit random "number" which is very unlikely to get duplicates of. I need the same behaviour for 32 bit numbers, what's the best way to achieve that? To just use plain old Random#Next() ? Or use Guid#GetHashCode() ?
33 replies
CC#
Created by Dusty on 8/31/2022 in #help
EF Core + SQLite order by decimal value [Answered]
Hey I tried to sort a query via a decimal column but I get this exception: System.NotSupportedException: SQLite does not support expressions of type 'decimal' in ORDER BY clauses. Convert the values to a supported type, or use LINQ to Objects to order the results on the client side. I searched a bit an people were speaking about using custom collations. Sadly I haven't found anything else about this yet. I also tried client evaluation but this just takes too long as my table has several million entries :/ Any ideas on this?
4 replies
CC#
Created by Dusty on 8/25/2022 in #help
License Key System
Hey, how would you implement a licensing system? I basically need those 3 things: - Online key validation (e.g. HTTP endpoint) - Offline key validation fallback - Blacklisting license keys I know that this doesn't protect piracy in any way but I don't need that anyway. The basic license check control flow would be the following: 1. Startup 2. Check license key 3. Continue startup or terminate app I know that IdentityServer uses some kind of licensing system aswell but looking at the source it kinda looks quite complicated. I also found this library: https://github.com/appsoftwareltd/dotnet-licence-key-generator Not sure if this is good though. Tl;dr: Is there already a library which fullfills my goals and/or how would I implement offline license validation?
2 replies