Ploxi
Ploxi
CC#
Created by Ploxi on 5/30/2024 in #help
asp - Calling another api endpoint
Hey, i have a stream containing a raw request and would like to call the asp.net core request pipeline with it. Has anyone got an idea of archiving that without doing the obvious http request? I would like to avoid the network overhead.
6 replies
CC#
Created by Ploxi on 4/29/2024 in #help
Best Practices for Instantiating Objects in C# Source Generators
I am writing a sourcegen. We need a way of instantiating SomeArgs in SomeService. Currently the sourcegen is just newing up SomeArgs.
[CreateConfigurableTask]
public class SomeArgs
{
public string SomeStuff { get; set; }
public string SomeStuff1 { get; set; }

public SomeArgs WithSomeStuff(string someStuff)
{
SomeStuff = someStuff;
return this;
}

public async Task<SomeArgs> SomeAsyncOperation(string myParam)
{
await Task.Delay(10);
return this;
}
}

public partial class SomeService
{
private Task<string> DoSomething(SomeArgs data)
{
return Task.FromResult($"Doing something with {data.SomeStuff} and {data.SomeStuff1}");
}
}
[CreateConfigurableTask]
public class SomeArgs
{
public string SomeStuff { get; set; }
public string SomeStuff1 { get; set; }

public SomeArgs WithSomeStuff(string someStuff)
{
SomeStuff = someStuff;
return this;
}

public async Task<SomeArgs> SomeAsyncOperation(string myParam)
{
await Task.Delay(10);
return this;
}
}

public partial class SomeService
{
private Task<string> DoSomething(SomeArgs data)
{
return Task.FromResult($"Doing something with {data.SomeStuff} and {data.SomeStuff1}");
}
}
Here are some ideas: - Mark a method with an attribute that generates SomeArgs like that:
[ConfigurableTaskArgsFactory]
public SomeArgs CreateSomeArgs()
{
return new SomeArgs();
}
[ConfigurableTaskArgsFactory]
public SomeArgs CreateSomeArgs()
{
return new SomeArgs();
}
- The sourcegen simply using IServiceProvider to get the instance of SomeArgs behind the scenes. Could be hard to see through what is happening. - Use a delegate in the generated partial class to create SomeArgs like that:
public partial class SomeService
{
private Func<SomeArgs> _createSomeArgs = () => new SomeArgs();
}
public partial class SomeService
{
private Func<SomeArgs> _createSomeArgs = () => new SomeArgs();
}
What are your thoughts on this?
7 replies
CC#
Created by Ploxi on 2/7/2024 in #help
Threadsafety and Interlocked.CompareExchange
Hey, i have written a method that updates a dictionary without locks. Of course i have written a test for it and it succeeded for 15 runs. But today i executed all tests and suddendly the test failed, which i cannot reproduce a second time. Can you guys spot what i might have done wrong?
class UpdateQueueStatus
{
private static Dictionary<string, UpdateQueueStatus> _mappings = new(StringComparer.OrdinalIgnoreCase);

public static readonly UpdateQueueStatus None = Create(0);
public static readonly UpdateQueueStatus Pending = Create(1);
public static readonly UpdateQueueStatus Queued = Create(0);
public static readonly UpdateQueueStatus Enqueued = Create(1);
public static readonly UpdateQueueStatus Caching = Create(2);
public static readonly UpdateQueueStatus Cached = Create(3);
public static readonly UpdateQueueStatus Applying = Create(4);
public static readonly UpdateQueueStatus Applied = Create(5);
public static readonly UpdateQueueStatus Completed = Create(6);
public static readonly UpdateQueueStatus Failed = Create(7);
public static readonly UpdateQueueStatus Cancelled = Create(8);
public static readonly UpdateQueueStatus Downloading = Create(2);

public string Value { get; }

public UpdateQueueStatus(string value)
{
Value = value;
}


public static UpdateQueueStatus Parse(string value)
{
if (_mappings.TryGetValue(value, out var status))
{
return status;
}

var result = new UpdateQueueStatus(value);

while (true)
{
var currentMap = _mappings;

var newMap = new Dictionary<string, UpdateQueueStatus>(currentMap, StringComparer.OrdinalIgnoreCase);
newMap.TryAdd(value, result);
if (Interlocked.CompareExchange(ref _mappings, newMap, currentMap) == currentMap)
{
break;
}
}

return result;
}

private static UpdateQueueStatus Create(int number, [CallerMemberName] string? name = null)
{
var result = new UpdateQueueStatus(name ?? "None");
_mappings.TryAdd(number.ToString(), result);
_mappings.TryAdd(result.Value, result);
_intMappings.Add((result.Value, number));
return result;
}
}

// Test:

public class Test
{
[Fact]
public void Should_Be_Threadsafe()
{
var count = UpdateQueueStatus.GetMappings().Count;

var tasks = new List<Task>();
for (int i = 0; i < 100; i++)
{
var task = Task.Run(() =>
{
for (int j = 0; j < 100; j++)
{
UpdateQueueStatus.Parse("value" + j);
}
});
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());

//Console.WriteLine("Number of keys in the dictionary: " + UpdateQueueStatus.GetMappings());
Assert.Equal(count + 100, UpdateQueueStatus.GetMappings().Count);
}
}
class UpdateQueueStatus
{
private static Dictionary<string, UpdateQueueStatus> _mappings = new(StringComparer.OrdinalIgnoreCase);

public static readonly UpdateQueueStatus None = Create(0);
public static readonly UpdateQueueStatus Pending = Create(1);
public static readonly UpdateQueueStatus Queued = Create(0);
public static readonly UpdateQueueStatus Enqueued = Create(1);
public static readonly UpdateQueueStatus Caching = Create(2);
public static readonly UpdateQueueStatus Cached = Create(3);
public static readonly UpdateQueueStatus Applying = Create(4);
public static readonly UpdateQueueStatus Applied = Create(5);
public static readonly UpdateQueueStatus Completed = Create(6);
public static readonly UpdateQueueStatus Failed = Create(7);
public static readonly UpdateQueueStatus Cancelled = Create(8);
public static readonly UpdateQueueStatus Downloading = Create(2);

public string Value { get; }

public UpdateQueueStatus(string value)
{
Value = value;
}


public static UpdateQueueStatus Parse(string value)
{
if (_mappings.TryGetValue(value, out var status))
{
return status;
}

var result = new UpdateQueueStatus(value);

while (true)
{
var currentMap = _mappings;

var newMap = new Dictionary<string, UpdateQueueStatus>(currentMap, StringComparer.OrdinalIgnoreCase);
newMap.TryAdd(value, result);
if (Interlocked.CompareExchange(ref _mappings, newMap, currentMap) == currentMap)
{
break;
}
}

return result;
}

private static UpdateQueueStatus Create(int number, [CallerMemberName] string? name = null)
{
var result = new UpdateQueueStatus(name ?? "None");
_mappings.TryAdd(number.ToString(), result);
_mappings.TryAdd(result.Value, result);
_intMappings.Add((result.Value, number));
return result;
}
}

// Test:

public class Test
{
[Fact]
public void Should_Be_Threadsafe()
{
var count = UpdateQueueStatus.GetMappings().Count;

var tasks = new List<Task>();
for (int i = 0; i < 100; i++)
{
var task = Task.Run(() =>
{
for (int j = 0; j < 100; j++)
{
UpdateQueueStatus.Parse("value" + j);
}
});
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());

//Console.WriteLine("Number of keys in the dictionary: " + UpdateQueueStatus.GetMappings());
Assert.Equal(count + 100, UpdateQueueStatus.GetMappings().Count);
}
}
Test failure:
The test failed:
Message: 
Assert.Equal() Failure
Expected: 125
Actual: 121
The test failed:
Message: 
Assert.Equal() Failure
Expected: 125
Actual: 121
53 replies
CC#
Created by Ploxi on 2/1/2024 in #help
✅ [EF Core] ValueObject Mapping thinks ValueObject is a normal Entity
No description
3 replies
CC#
Created by Ploxi on 9/30/2023 in #help
❔ How to Package Multiple Versions of a Library Targeting Different Dependency Versions into a Nuget
I am working on a C# library (mylib) which has a dependency on another library (thatlib). Now, the problem is, that thatlib has breaking changes between versions 1.0 and 2.0, and I need to support both versions of thatlib in my library. I need to create two different versions of mylib, each targeting different versions of thatlib, and package them into a single NuGet package. Here are the version ranges I am targeting: 1. mylib_v1 targeting thatlib versions [1.0,2.0) 2. mylib_v2 targeting thatlib versions [2.0,5.0) I want to achieve this ideally using a single .csproj file without creating separate projects for each version. My goal is to ensure that consumers of my library get the correct version of mylib based on the version of thatlib they are using.
155 replies
CC#
Created by Ploxi on 8/30/2023 in #help
✅ [VsCode] Control Over Suggestion Priority in YAML Files
74 replies
CC#
Created by Ploxi on 1/16/2023 in #help
❔ [System.Text.Json] When to close the stream
I have a very large json file (500 mb or more), so naturally i would use System.Text.Json in combination with IAsyncEnumerable<FileMetaDataDto>. Now i want to open a stream to a file using the following:
public static async Task<T?> ReadAsJson<T>(this IContentProvider contentProvider, Uri uri, JsonSerializerOptions? options = null)
{
using var stream = await contentProvider.OpenContentStream(uri);
return await JsonSerializer.DeserializeAsync<T>(stream, options);
}
public static async Task<T?> ReadAsJson<T>(this IContentProvider contentProvider, Uri uri, JsonSerializerOptions? options = null)
{
using var stream = await contentProvider.OpenContentStream(uri);
return await JsonSerializer.DeserializeAsync<T>(stream, options);
}
But what if the serializer needs the stream afterwards? If i leave it open, the stream would be dangling in limbo until the GC decides that it really wouldnt neeed that referencing memory anymore....
2 replies
CC#
Created by Ploxi on 12/6/2022 in #help
✅ Package native libraries with nuget
Hey, is there a good guide how to package and deliver native assemblies with nuget? I kinda managed to get them in the nuget but they wont unpack in the target location where the managed dll is...
6 replies