Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JSON.NET's JsonTextReader to read from a NetworkStream asynchronously?

I implemented my own NetworkStream port for Silverlight which only allows asynchronous calls.
I would like to read some JSON-RPC messages that I am getting from a server so I figured I'd use JSON.NET JsonTextReader so I ended up with the following code:

reader = new JsonTextReader(new StreamReader(new NetworkStream(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))));  
// ...
reader.Read();  

My problem is that it will attempt to perform a synchronous operation which in turn just throw UnsupportedException.
Is there an asynchronous StreamReader that I can feed the JsonTextReader with?
Should I take another approach?

like image 355
the_drow Avatar asked Nov 05 '22 21:11

the_drow


1 Answers

I think you should. I don't think you can force JsonTextReader to use async approach, but you could modify the entire method used for getting data to behave asynchronously. Also, use

using(var io = new StreamReader())
{ 
    io.Read(); 
} 

syntax.

like image 154
omittones Avatar answered Nov 14 '22 17:11

omittones