-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StdIn / Stdout Example #1
Comments
yea that would be very interesting and helpful indeed! |
It seems there is a helpful sample in the vs code extensions documentation. However, for some reason, it still does not work: @AArnott - any hint would be greatly appreciated! :-) |
OGM thank you so much! I would never have come to that solution! |
@gentledepp It's hard for me to read your source code in your animated gif. What was it you were doing wrong? No bug jumped out at me. |
Also: is there any additional API or documentation that might have made this easier before I updated the sample? |
I do not know. I'll attach the minimized source code that I came up with. After my sample was not working, I tried to copy yours step by step. I simply do not see what is going wrong there... it seems though, like my "worker.exe" somehow is not initialized as I cannot seem to attach to it and resolve breakpoints (CTRL+ALT+P) Maybe you can spot the difference? Also: I would really love to have some basic documentation on
|
Your code was almost fine. You just had two extra lines: static async Task Main(string[] args)
{
- Console.WriteLine("Worker starting up");
- Console.WriteLine($"Startup args: {string.Join(" ", args)}");
var stream = FullDuplexStream.Splice(Console.OpenStandardInput(),
Console.OpenStandardOutput());
var server = new MyJsonRpcServer();
using var jsonRpc = JsonRpc.Attach(stream, server);
await jsonRpc.Completion;
} You were polluting the JSON-RPC stream coming out of STDOUT of your console application. Notice in my application I use
StreamJsonRpc works over any .NET Stream. Half vs. full duplex is a distinction I incorrectly used in the past. If you see documentation that mentions half-duplex please point me to it so I can correct it. (Full) Duplex means the stream is bidirectional. Simplex (which I used to incorrectly call half duplex) means a stream that goes only one direction.
Splice takes two simplex streams and creates one duplex stream. It isn't necessary though -- you could have called the JsonRpc constructor and passed stdin/stdout directly. |
Thanks man! That is great information! Only one question remains, though:
I am generally more interested in the stdin/stdout approach as it is the one taken by vs code to communicate with its extensions. So my suggestions:
|
You can do what I did, and use Console.Error as I already explained.
Remember you spawned the child process and redirected its stdin/stdout. So the child process cannot ask the user anything via the console anyway. It's effectively an invisible process. It could pop a WPF dialog if you really wanted, but generally if you have something that needs to interact with the user, I would avoid using the stdio approach streams for other business.
These streams auto-flush. But if the parent process wants to ignore the first few lines of emitted text for purposes of JSON-RPC, that's perfectly fine. Do whatever you need to with the streams before attaching JSON-RPC. But be aware that if you use APIs like
I think that's an over-simplification. Most VS Code extensions don't have to communicate like this at all. They're just activated in another process and they're invoked via an RPC mechanism they know nothing about.
The updated StreamJsonRpc.Sample already does:
I'll think about adding it to the StreamJsonRpc docs itself too. I suppose as a potentially common case we can mention it.
I'll consider it, but it makes 1 line become at least three so it actually makes the sample more complex.
Ah, thanks for the pointer to the docs. I'll definitely get that fixed.
Thanks! I don't drink, but I do accept donations. :) |
Beside the 'NamedPipeServerStream' I would love to see a jsonrpc sample like your's but based on StandardIO Streams.
Also I would like to see a
public static async Task Main(string[] args)
as the main entry point in your sample.
The text was updated successfully, but these errors were encountered: