Ploot
Ploot
CC#
Created by Ploot on 9/4/2024 in #help
✅ AddHttpClient and AddHostedService
I have a hosted service that I register with my DI as so serviceCollection.AddHostedService<ExampleService>() and everything works fine. However I now need this service to use it's own HttpClient. That seemed like no issue, Just add serviceCollection.AddHttpClient<ExampleService>(x => x.Timeout = Timeout.InfiniteTimeSpan) and I should be good to go, or so I thought.. Everything resolved correctly and the program ran, except for the fact that the injected HttpClient did not in fact have infinite timeout. A quick breakpoint check later and i find that the configuration action isn't triggering, or from what I found online, that client isn't used at all and it's instead falling back to the standard HttpClient that AddHttpClient adds for everything that isn't the targeted type. I found changing my hosted service declaration to serviceCollection.AddHostedService(provider => provider.GetRequiredService<ExampleService>()) resolves these issues but I'm honestly kinda at a loss as to why I had to do this and feel like I must have done something wrong to need to do this. Could anyone enlighten me on the cause of this?
29 replies
CC#
Created by Ploot on 9/1/2024 in #help
✅ Per request timeout
Is there any way to control the timeout period of a HttpClient on the request side instead of from the client itself? It feels a little bizarre that a class that's recommended to only be instantiated once and shared across the app controls timeouts from the client
6 replies
CC#
Created by Ploot on 8/30/2024 in #help
Grainy gif output when using `Graphics` on source Bitmap
No description
3 replies
CC#
Created by Ploot on 8/27/2024 in #help
✅ JsonSerializer ignoring encoding options?
This issue has been driving me crazy for the last hour. I have a json object that includes a string with an apostrophe in it, however it keeps getting converted to a unicode code. After some reading I found this was the default behaviour for the serializer and can be overridden with encoder options, however I have been totally unable to override this behaviour for some reason. This is the dotnetfiddle snippet I've been testing with https://dotnetfiddle.net/7VZABE. Am I doing something wrong here?
4 replies
CC#
Created by Ploot on 8/7/2024 in #help
Don't start hosted services on start
I'm using Microsoft.Extensions.Hosting for a project and during startup i register multiple IHostedService services, however all my other services are dependent on a singular service completing a task first, as such I'm looking for a way to have only one specific service start with the framework and have that service wake the others once it's finished
15 replies
CC#
Created by Ploot on 3/21/2024 in #help
✅ [Solved] [Polly] Not all code paths return a value
I have a method here that claims not all paths return a value despite not having an branches?
public async Task<Stream> GetAsync(Uri remoteAddress, string path, CancellationToken cancellationToken = default)
{
ResiliencePipeline<HttpResponseMessage> pipeline = resiliencePipeline.GetPipeline<HttpResponseMessage>("get-pipeline");
UriBuilder endpoint = new(remoteAddress) { Path = path };
HttpResponseMessage reqResp = await pipeline.ExecuteAsync(async token => await _httpClient.GetAsync(endpoint.Uri, token), cancellationToken);
await reqResp.Content.ReadAsStreamAsync(cancellationToken);
}
public async Task<Stream> GetAsync(Uri remoteAddress, string path, CancellationToken cancellationToken = default)
{
ResiliencePipeline<HttpResponseMessage> pipeline = resiliencePipeline.GetPipeline<HttpResponseMessage>("get-pipeline");
UriBuilder endpoint = new(remoteAddress) { Path = path };
HttpResponseMessage reqResp = await pipeline.ExecuteAsync(async token => await _httpClient.GetAsync(endpoint.Uri, token), cancellationToken);
await reqResp.Content.ReadAsStreamAsync(cancellationToken);
}
This is my first time using polly and this has me very confused
5 replies
CC#
Created by Ploot on 2/4/2024 in #help
Cleaner NavigationView implementation (Avalonia)
I'm currently porting an existing project that i didn't write from WinUI3 to Avalonia. The project I'm porting used NavigationMenu to change between pages and as I am trying to keep this project as close to the original as possible I'm using FluentAvalonia's implementation of NavigationMenu, However I wasn't particularly happy with the code behind for switching pages and upon searching it seems every example I could find uses the same method of using a switch statement to navigate to a typeof(<type>) based on the items tag. This feels really clumsy and was wondering if there's a cleaner way of binding each NavigationViewItem to a type/view?
1 replies