Skip to content

Not receiving successful responses with NewLineDelimitedMessageHandler #2

@travispaul

Description

@travispaul

After running the client/server examples successfully, I started working with a modified version of the StreamJsonRpc.Sample and to test things out I am attempting to pipe a JSON RPC message into it like so:

./ConsoleApp1 < example.json

example.json:

{"jsonrpc":"2.0","method":"Add","params":[1,2],"id":1}

example.json (hexdump to show CR LF ending):

┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 7b 22 6a 73 6f 6e 72 70 ┊ 63 22 3a 22 32 2e 30 22 │{"jsonrp┊c":"2.0"│
│00000010│ 2c 22 6d 65 74 68 6f 64 ┊ 22 3a 22 41 64 64 22 2c │,"method┊":"Add",│
│00000020│ 22 70 61 72 61 6d 73 22 ┊ 3a 5b 31 2c 32 5d 2c 22 │"params"┊:[1,2],"│
│00000030│ 69 64 22 3a 31 7d 0d 0a ┊                         │id":1}__┊        │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘

The result is:

Connection request #0 received. Spinning off an async Task to cater to requests.
JSON-RPC listener attached to #0. Waiting for requests...
Received request: 1 + 2
Connection #0 terminated.

The message was received and logged to STDERR but no response was returned to STDOUT.

However, If I supply a payload that results in an error, I receive a response:

./ConsoleApp1 < faulty.json
Connection request #0 received. Spinning off an async Task to cater to requests.
JSON-RPC listener attached to #0. Waiting for requests...
{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"No method by the name 'Nowhere' is found.","data":null}}
Connection #0 terminated.

faulty.json:

{"jsonrpc":"2.0","method":"Nowhere","params":[1,2],"id":1}

faulty.json (hexdump):

┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 7b 22 6a 73 6f 6e 72 70 ┊ 63 22 3a 22 32 2e 30 22 │{"jsonrp┊c":"2.0"│
│00000010│ 2c 22 6d 65 74 68 6f 64 ┊ 22 3a 22 4e 6f 77 68 65 │,"method┊":"Nowhe│
│00000020│ 72 65 22 2c 22 70 61 72 ┊ 61 6d 73 22 3a 5b 31 2c │re","par┊ams":[1,│
│00000030│ 32 5d 2c 22 69 64 22 3a ┊ 31 7d 0d 0a             │2],"id":┊1}__    │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘

Am I doing something wrong? The modified example I'm using:

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading.Tasks;
using Nerdbank.Streams;
using StreamJsonRpc;

internal class Server
{
    public int Add(int a, int b)
    {
        Console.Error.WriteLine($"Received request: {a} + {b}");
        return a + b;
    }
}

class Program
{
    static async Task<int> Main(string[] args)
    {
        int clientId = 0;

        await Console.Error.WriteLineAsync($"Connection request #{clientId} received. Spinning off an async Task to cater to requests.");

        var nl = new NewLineDelimitedMessageHandler(Console.OpenStandardOutput(), Console.OpenStandardInput(), new JsonMessageFormatter());

        var jsonRpc = new JsonRpc(nl, new Server());

        await Console.Error.WriteLineAsync($"JSON-RPC listener attached to #{clientId}. Waiting for requests...");

        jsonRpc.StartListening();

        await jsonRpc.Completion;

        await Console.Error.WriteLineAsync($"Connection #{clientId} terminated.");

        return 0;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions