Exemple #1
0
        public static async Task <T> AssertReadMessageAsync <T>(Stream stream, string compressionEncoding = null, List <ICompressionProvider> compressionProviders = null) where T : IMessage, new()
        {
            compressionProviders = compressionProviders ?? new List <ICompressionProvider>
            {
                new GzipCompressionProvider(CompressionLevel.Fastest)
            };

            var pipeReader = new StreamPipeReader(stream);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Headers[GrpcProtocolConstants.MessageEncodingHeader] = compressionEncoding;

            var serverCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(
                httpContext: httpContext,
                serviceOptions: new GrpcServiceOptions
            {
                ResponseCompressionAlgorithm = compressionEncoding,
                CompressionProviders         = compressionProviders
            });

            var messageData = await pipeReader.ReadSingleMessageAsync(serverCallContext);

            var message = new T();

            message.MergeFrom(messageData);

            return(message);
        }
Exemple #2
0
        public static async Task <T> AssertReadStreamMessageAsync <T>(PipeReader pipeReader, string compressionEncoding = null, List <ICompressionProvider> compressionProviders = null) where T : IMessage, new()
        {
            compressionProviders = compressionProviders ?? new List <ICompressionProvider>
            {
                new GzipCompressionProvider(CompressionLevel.Fastest)
            };

            var serverCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(serviceOptions: new GrpcServiceOptions
            {
                ResponseCompressionAlgorithm = compressionEncoding,
                CompressionProviders         = compressionProviders
            });

            var messageData = await pipeReader.ReadStreamMessageAsync(serverCallContext);

            if (messageData == null)
            {
                return(default);