Relevant
Relevant
CC#
Created by Relevant on 5/5/2024 in #help
Long running process in Blazor Server
I have a Blazor Server app, and I have a page that will kick off a long running process. I am calling an async method without an await in order to get the response back quickly, and it is starting the processes fine. But it seems like whenever there is a long running syncronous task running in the background, it will lock the UI thread. How can I make this process run in a way that doesn't block the UI thread? Here's a sample project to show what I mean:
public class TestService
{
public void StartLongRunningJob()
{
LongRunningJob();
}

private async Task LongRunningJob()
{
while (true)
{
Thread.Sleep(5000); //Simulation of something that locks UI thread
await Task.Delay(1);
}
}
}
public class TestService
{
public void StartLongRunningJob()
{
LongRunningJob();
}

private async Task LongRunningJob()
{
while (true)
{
Thread.Sleep(5000); //Simulation of something that locks UI thread
await Task.Delay(1);
}
}
}
@inject TestService TestService

<button class="btn btn-primary" @onclick="HandleButtonClick">Start Long Running Job</button>

@code{
private void HandleButtonClick()
{
TestService.StartLongRunningJob();
}
}
@inject TestService TestService

<button class="btn btn-primary" @onclick="HandleButtonClick">Start Long Running Job</button>

@code{
private void HandleButtonClick()
{
TestService.StartLongRunningJob();
}
}
64 replies
CC#
Created by Relevant on 2/27/2024 in #help
StreamReader is always EndOfStream=true even before reading
//var response = httpClient.GetAsync(url);

using (var fs = new FileStream("000000_0.deflate", FileMode.Open))
{
//await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
//var response = httpClient.GetAsync(url);

using (var fs = new FileStream("000000_0.deflate", FileMode.Open))
{
//await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
So this code works when I run locally with a file name hard coded in. When I instead download the the file and load it into a FileStream, it's always at EndOfStream. Am I doing something wrong? Here's what the code would look like in the way it's NOT working:
var url = $"http://url.to.download/{fileName}";
var response = httpClient.GetAsync(url);

using (var fs = new FileStream(_fileDir + fileName, FileMode.Create))
{
await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
var url = $"http://url.to.download/{fileName}";
var response = httpClient.GetAsync(url);

using (var fs = new FileStream(_fileDir + fileName, FileMode.Create))
{
await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
18 replies
CC#
Created by Relevant on 2/21/2024 in #help
SSH.NET hide terminal junk
This might be a long shot, but is there anyone here that's familiar with SSH.NET and know if there's some sort of terminal mode that shows cleaner output, so I don't have to filter out things like serverName /some/place $: my -command?
3 replies
CC#
Created by Relevant on 1/18/2024 in #help
How to know if Serilog fails
I'm assuming that Serilog fails silently if there are issues. I'm trying to log to elasticsearch and I don't see them coming through. I'm assuming it's an issue with authentication, but I'm not seeing any error. Anyone know how I can have the logging thrown an exception or show errors in some way?
25 replies
CC#
Created by Relevant on 1/17/2024 in #help
SFTP through a proxy server
I'm using SSH.NET and since I'm unable to connect to production servers directly, I have to first SSH into an intermediary server (using a private key), which I can than use to SSH into the prod servers. It's working-ish, but I'm doing everything through shell streams and using regular expressions to filter the linux output. It's a bit of a mess. I'd really like to use SFTP to check files, but I haven't yet figured out a way to do this through another server using a private key. I know it's a long shot, since this is a pretty specific need, but figured I'd check here to see if anyone has any ideas.
6 replies
CC#
Created by Relevant on 12/4/2023 in #help
enum HasFlag
I would've thought this would return true, but it's false. How can I fix this logic to return true for any overlaps? (Colors.Red | Colors.Orange).HasKey(Colors.Red | Colors.Purple)
4 replies
CC#
Created by Relevant on 10/4/2023 in #help
❔ Improving performance for Process.Start
I have an application that will run a python script using Process.Start. At the end, I'm reading the output with process.StandardOutput.ReadToEnd(). If I run the script on it's own, it runs in about 1 second. But running it this way takes about 2 seconds. Is there any way I could reduce some overhead? Is reading output time consuming?
14 replies
CC#
Created by Relevant on 5/17/2023 in #help
❔ ✅ Register instance of List as singleton
This feels like a dumb question, but I'm trying to keep a singleton instance of a List that can be added to by different areas of the application. This doesn't seem to work:
builder.Services.AddSingleton<List<MyThing>>();
builder.Services.AddSingleton<List<MyThing>>();
or
List<MyThing> myThings = new();
builder.Services.AddSingleton(myThings);
List<MyThing> myThings = new();
builder.Services.AddSingleton(myThings);
6 replies
CC#
Created by Relevant on 4/20/2023 in #help
❔ Mock linux server
I'm trying to write some unit tests for an application I created that will ssh into several servers and check some configurations, etc. Is there a good way to mock this? Could I do some dummy server somehow, or mock a SshClient? I'm certainly not super experienced when it comes to unit testing. Would it be fine to just use Moq and give a SshClient/ShellStream a Setup and Returns to just return some dummy data back from the calls?
7 replies
CC#
Created by Relevant on 4/14/2023 in #help
❔ How to restart a BackgroundService
Searching the internets, I seem to be finding answers that don't really seem right, and not good even if they are right. Is there a way to get the instance of a hosted service that may have stopped so that I can restart it? I tried this, but service ended up being null in this case:
using (var scope = serviceScopeFactory.CreateScope())
{
var service = scope.ServiceProvider.GetService<MyBackgroundService>();

if (service != null)
{
try
{
await service.StopAsync(default);
await service.StartAsync(default);
}
catch (Exception ex)
{
Log.Error(ex, ex.Message);
}
}

}
using (var scope = serviceScopeFactory.CreateScope())
{
var service = scope.ServiceProvider.GetService<MyBackgroundService>();

if (service != null)
{
try
{
await service.StopAsync(default);
await service.StartAsync(default);
}
catch (Exception ex)
{
Log.Error(ex, ex.Message);
}
}

}
11 replies
CC#
Created by Relevant on 3/29/2023 in #help
❔ ✅ Choosing an appropriate data structure
So I have a list of objects and I want one in the list to be active and the rest to be inactive. I was thinking of using a Dictionary<Thing, bool> where the bool is if it's active or not. I guess this would work, but then I'd have to do something like myDict.FirstOrDefault(d => d.Value == true).Key which feels a little clunky
22 replies
CC#
Created by Relevant on 3/16/2023 in #help
❔ ✅ Best practice for tracking progress for long running method
I am creating a Blazor Server application. I have a method that is compiling some data from a few servers. I'd like to show some sort of progress bar to show the user approximately how much is left. Currently the method just returns a List of all of the objects that was compiled during the process. This is being done in a service class, so I wouldn't have access to the UI elements from the method. So is there a way to send progress information back to the caller periodically? I could refactor the method to do 1 server at a time, that way I'll know, for example, that it's 3 of 8 completed, etc. And I could break it down even further to split apart each section of processing to get even more granular. But that seems like it could be more work than it's worth for just a UI nicety. Is there some super neat way of doing this better?
5 replies
CC#
Created by Relevant on 2/14/2023 in #help
❔ Transferring data to python script
I'm building a C# app that can run python scripts. I'm doing the database queries on the C# side, seems to be more efficient. I'm currently just throwing the data into a text file to be read by the python script. It works, but wondering if I'd be able to shave a few milliseconds by using some other mechanism for transferring data. Any ideas?
10 replies