public async Task ListenChat(ChatMessageRequest request, IServerStreamWriter<ChatMessage> responseStream, ServerCallContext context) { Log(context); using (IAsyncEnumerator<ChatMessage> enumerator = _chatMessageRepository.ListenAccountChatAsync(request.AccountId).GetEnumerator()) { // Custom reponse header await context.WriteResponseHeadersAsync(new Metadata { new Metadata.Entry("Some-response-header-key", "Some-response-header-value") }); // Async enumerator while (await enumerator.MoveNext()) { ChatMessage chatMessage = enumerator.Current; await responseStream.WriteAsync(chatMessage); } // Custom response trailer context.ResponseTrailers.Add(new Metadata.Entry("Some-response-tailer-key", "Some-response-trailer-value")); } }
private static async Task EnsureEchoMetadataAsync(ServerCallContext context) { var echoInitialList = context.RequestHeaders.Where((entry) => entry.Key == "x-grpc-test-echo-initial").ToList(); if (echoInitialList.Any()) { var entry = echoInitialList.Single(); await context.WriteResponseHeadersAsync(new Metadata { entry }); } var echoTrailingList = context.RequestHeaders.Where((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ToList(); if (echoTrailingList.Any()) { context.ResponseTrailers.Add(echoTrailingList.Single()); } }