Learning C#! Looking for a "conventional" way to accomplish persistent network connections.
Hi! I'm learning C#, been at it for a couple days, I have a lot of experience with Python but I want to try something new.
Looking for help on good practice!
My goal is to create a Win UI 3 app that communicates with a server over ssh with the SSH.NET library. I want the connection to remain open and ready for new requests without going through the slow initialization process unless it's being opened for the first time or the connection is interrupted due to network problems. Through the ssh connection I wish to communicate with an HTTP server that is not available publicly. And also run shell commands and capture their output. The library offers facilities for both of these use cases, but I'm searching for a way to cleanly manage them.
I'm a bit lost with C# conventions in general! In my "scratchpad" code I have set a private variable in the Program object, thinking that storing it here would be the best way to go about this.
Here is some very simple code I have written that works outside of a GUI (haven't gotten that far yet). I wish to avoid calling the Connect method in future code and just want a managed connection I can always call methods on and get new data or communicate to the HTTP server.
https://git.tilde.town/nebula/sshnet/src/branch/main/sshnet/Program.cs
I am wondering if this is conventional, or what a better strategy may be. I will also need to have ports forwarded and kept open. How should I store these and maintain the connection in a way that works well with a GUI app that is kept open?
tilde.town git repositories
sshnet/Program.cs at main
5 Replies
Well that's alright. For console apps you usually keep the program open by keeping the main thread running using a Console.ReadLine();
For GUI apps, this is usually handled for you so you only need to call Connect(); on startup and you can process everything. Another way would be to write a wrapper that handles the SshClient where it lazy connects to the ssh server therefore it only opens a connection if there is no connection on a request.
As for the command line displaying you can use a MultiLineTextBox or just even a simple TextBlock is good enough. The way that you capture and structure it cleanly is to use wrapper libraries/systems that you inject via dependency injection into ViewModels that then get passed to the View which renders the output.
So instead of going straight into designing the backend, you should understand how the standard GUI app is created
Okay, so I should focus on getting the hang of how a WinUI app really works before stressing about this too much. I'd like to start by getting these objects i am deserializing into a list of UI elements.
I can deal with optimizing that connection later
thanks! ill check back later and see what I can do in the meantime to get more familiar
if you have any links you can share that can help, that would be awesome, but I think there's enough docs online that I can figure it out
These 2 are very useful when creating a GUI app.
https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/
Dependency injection - .NET
Learn how to use dependency injection within your .NET apps. Discover how to registration services, define service lifetimes, and express dependencies in C#.
Introduction to the MVVM Toolkit - Community Toolkits for .NET
An overview of how to get started with the MVVM Toolkit and to the APIs it contains
You don't necessarily need them but they are an absolute must to know about them
Thanks! I'll check it out right now