Grpc C# Question on Reading from Response Stream

2,111 views
Skip to first unread message

naman....@gmail.com

unread,
Oct 4, 2016, 2:42:49 PM10/4/16
to grpc.io
Hello,

I am trying to work on a grpc C# client that initiates a subscription type call with a grpc server. I believe I sent the subscription correctly but am not able to read from the response stream correctly. I try to await on the AsyncEnumerator.MoveNext() task but I get a null reference exception on the below code.

var asyncCall = client.Subscribe();
var responseStream = asyncCall.ResponseStream;

await asyncCall.RequestStream.CompleteAsync();

while (await responseStream.MoveNext())
{  
    var tresp = asyncCall.ResponseStream.Current;
    // print tresp message.
}

After having sent an RPC call, does the ResponseStream get initialized immediately ? My guide for this has been https://github.com/grpc/grpc/blob/v1.0.0/examples/csharp/route_guide/RouteGuideClient/Program.cs.

Thanks,
Naman


Jan Tattermusch

unread,
Oct 6, 2016, 2:12:40 AM10/6/16
to naman....@gmail.com, grpc.io
Hi,

I skimmed through your code and it looks alright (perhaps I missed something?). What line does throw in your case?

To answer your questions:

The response stream is ready immediately after starting your call and you don't even need to send any requests nor close the request stream to be able to read from responseStream (so your code really should work). 
Also, if you need to just subscribe for server updates, perhaps the bi-streaming call is an overkill - you could start a server-streaming call, where you send a unary request (with you subscription details) and then keep reading from the response stream (but your example should work).

Jan

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/3c54b44d-bdeb-48c3-a0c3-c804dca2ca87%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Naman Muley

unread,
Oct 18, 2016, 7:55:35 PM10/18/16
to grpc.io, naman....@gmail.com
Jan,

Thanks for the reply. Apologies in delayed acknowledge. You are right. The problem was in my asynchronous calls to this particular piece of code.
The above code works fine.

Thanks,
Naman


On Thursday, October 6, 2016 at 2:12:40 AM UTC-4, Jan Tattermusch wrote:
Hi,

I skimmed through your code and it looks alright (perhaps I missed something?). What line does throw in your case?

To answer your questions:

The response stream is ready immediately after starting your call and you don't even need to send any requests nor close the request stream to be able to read from responseStream (so your code really should work). 
Also, if you need to just subscribe for server updates, perhaps the bi-streaming call is an overkill - you could start a server-streaming call, where you send a unary request (with you subscription details) and then keep reading from the response stream (but your example should work).

Jan
On Tue, Oct 4, 2016 at 8:42 PM, <naman....@gmail.com> wrote:
Hello,

I am trying to work on a grpc C# client that initiates a subscription type call with a grpc server. I believe I sent the subscription correctly but am not able to read from the response stream correctly. I try to await on the AsyncEnumerator.MoveNext() task but I get a null reference exception on the below code.

var asyncCall = client.Subscribe();
var responseStream = asyncCall.ResponseStream;

await asyncCall.RequestStream.CompleteAsync();

while (await responseStream.MoveNext())
{  
    var tresp = asyncCall.ResponseStream.Current;
    // print tresp message.
}

After having sent an RPC call, does the ResponseStream get initialized immediately ? My guide for this has been https://github.com/grpc/grpc/blob/v1.0.0/examples/csharp/route_guide/RouteGuideClient/Program.cs.

Thanks,
Naman


--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages