Samuel
Samuel
CC#
Created by Samuel on 7/9/2024 in #help
Embedding a 3D view with OpenGL backend within a WPF view
I'm not looking for detailed steps. I'm just need to know how to render directly to a control within a WPF application. I have played around with Silk.NET which I have loved. Is there a way I can use this with a view within WPF?
7 replies
CC#
Created by Samuel on 10/17/2023 in #help
✅ Using a scoped service within an IHostedService
I have a IHostedService implementation that sends a Mediatr request every hour. The handler uses a DbContext to carry out its work. When sending the request, I get the following exception:
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Cannot resolve 'MediatR.IRequestHandler`1[AuctionData.Application.Features.ProcessAuctionData+ProcessAuctionDataCommand]' from root provider because it requires scoped service 'AuctionData.Application.Data.AuctionDbContext'.'
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Cannot resolve 'MediatR.IRequestHandler`1[AuctionData.Application.Features.ProcessAuctionData+ProcessAuctionDataCommand]' from root provider because it requires scoped service 'AuctionData.Application.Data.AuctionDbContext'.'
I have tried to create an async scope, but I still get the exception.
private async void RequestAuctionDataProcessing(object? state)
{
await using (var scope = _serviceProvider.CreateAsyncScope())
{
await _mediator.Send(new ProcessAuctionDataCommand(1305));
}
}
private async void RequestAuctionDataProcessing(object? state)
{
await using (var scope = _serviceProvider.CreateAsyncScope())
{
await _mediator.Send(new ProcessAuctionDataCommand(1305));
}
}
I have listed the project in GitHub. Here is a link to where the functionality lies https://github.com/Scharps/AuctionData/blob/Scharps/issue15/AuctionData.ApplicationCore/Features/ProcessAuctionData.cs Thank you for reading.
8 replies
CC#
Created by Samuel on 2/24/2023 in #help
❔ ✅ Splitting string without allocation
I'm writing a JsonConverter for Vector3 and I'd like to do this without allocating too much memory. I've written this extension method to split the string as ReadOnlyMemory. I wanted to use ReadOnlySpan but I'm unable to use this type in IEnumerable<T>.
public static IEnumerable<ReadOnlyMemory<T>> Split<T>(this ReadOnlyMemory<T> memory, T separator)
where T: IEquatable<T>
{
int start = 0;
int end = 0;
while (end < memory.Length)
{
if (memory.Span[end].Equals(separator) || end == memory.Length)
{
yield return memory.Slice(start, end - start);
start = end + 1;
}
end++;
}

}
public static IEnumerable<ReadOnlyMemory<T>> Split<T>(this ReadOnlyMemory<T> memory, T separator)
where T: IEquatable<T>
{
int start = 0;
int end = 0;
while (end < memory.Length)
{
if (memory.Span[end].Equals(separator) || end == memory.Length)
{
yield return memory.Slice(start, end - start);
start = end + 1;
}
end++;
}

}
This is my implementation of Read for JsonConverter<Vector3>
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
ReadOnlyMemory<char> vectorString = reader.GetString().AsMemory();
var vectorValues = vectorString.Split(',').Select(s => float.Parse(s)).ToArray(); // Unable to parse `s` as it's not a string. What do?
return new Vector3 { X = vectorValues[0], Y = vectorValues[1], Z = vectorValues[2] };
}
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
ReadOnlyMemory<char> vectorString = reader.GetString().AsMemory();
var vectorValues = vectorString.Split(',').Select(s => float.Parse(s)).ToArray(); // Unable to parse `s` as it's not a string. What do?
return new Vector3 { X = vectorValues[0], Y = vectorValues[1], Z = vectorValues[2] };
}
I'd like to know if there are any ways to make my Split method better, and to somehow get my Read method to work. Thanks for reading
31 replies