开发者

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 Sock开发者_如何学Cet(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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜