コード例 #1
0
ファイル: Program.cs プロジェクト: xiaowu351/grpc-dotnet
        static async Task <ServerReflectionResponse> SingleRequestAsync(ServerReflection.ServerReflectionClient client, ServerReflectionRequest request)
        {
            var call = client.ServerReflectionInfo();
            await call.RequestStream.WriteAsync(request);

            Debug.Assert(await call.ResponseStream.MoveNext());

            var response = call.ResponseStream.Current;
            await call.RequestStream.CompleteAsync();

            return(response);
        }
コード例 #2
0
        private async Task <ServerReflectionResponse> SingleRequestAsync(ServerReflectionRequest request)
        {
            var call = client.ServerReflectionInfo();
            await call.RequestStream.WriteAsync(request);

            Assert.IsTrue(await call.ResponseStream.MoveNext());

            var response = call.ResponseStream.Current;
            await call.RequestStream.CompleteAsync();

            Assert.IsFalse(await call.ResponseStream.MoveNext());
            return(response);
        }
コード例 #3
0
        public async Task <InfoRS> GetInfo(string address, int port)
        {
            var result  = new InfoRS();
            var channel = new Channel(address, port, ChannelCredentials.Insecure);

            try
            {
                var client = new ServerReflection.ServerReflectionClient(channel);
                var resp   = client.ServerReflectionInfo();
                await resp.RequestStream.WriteAsync(new ServerReflectionRequest()
                {
                    ListServices = ""
                });

                await resp.ResponseStream.ForEachAsync(async res =>
                {
                    switch (res.MessageResponseCase.ToString())
                    {
                    case "ListServicesResponse":
                        await ListServices(resp.RequestStream, res.ListServicesResponse);
                        break;

                    case "FileDescriptorResponse":
                        ListMethods(res.FileDescriptorResponse, result);
                        break;
                    }
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                throw ex;
            }
            finally
            {
                await channel.ShutdownAsync();
            }
            return(result);
        }
コード例 #4
0
        public override async Task <int> ExecuteAsync(CommandContext context, ListSettings settings)
        {
            var address = settings.Address;

            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = $"https://{address}";
            }

            if (address.StartsWith("http://"))
            {
                // This switch must be set before creating the GrpcChannel/HttpClient.
                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            }

            var server  = address;
            var channel = Grpc.Net.Client.GrpcChannel.ForAddress(server);
            var client  = new ServerReflection.ServerReflectionClient(channel);
            var stream  = client.ServerReflectionInfo();

            if (string.IsNullOrEmpty(settings.Service))
            {
                await stream.RequestStream.WriteAsync(new ServerReflectionRequest { ListServices = "ls" });

                await stream.ResponseStream.MoveNext(CancellationToken.None);

                foreach (var service in stream.ResponseStream.Current.ListServicesResponse.Service)
                {
                    Console.WriteLine(service.Name);
                }

                await stream.RequestStream.CompleteAsync();
            }
            else
            {
                await stream.RequestStream.WriteAsync(new ServerReflectionRequest { FileContainingSymbol = settings.Service });

                await stream.ResponseStream.MoveNext(CancellationToken.None);

                var descriptors = FileDescriptor.BuildFromByteStrings(stream.ResponseStream.Current.FileDescriptorResponse.FileDescriptorProto);
                await stream.RequestStream.CompleteAsync();

                var service = descriptors
                              .SelectMany(x => x.Services)
                              .FirstOrDefault(x => string.Equals(x.FullName, settings.Service));

                if (service is object)
                {
                    Console.WriteLine($"filename: {service.File.Name}");
                    Console.WriteLine($"package: {service.File.Package}");
                    Console.WriteLine($"service {service.Name} {{");
                    foreach (var method in service.Methods)
                    {
                        Console.WriteLine($"  {GetMethod(method)}");
                    }
                    Console.WriteLine("}");
                }
                else
                {
                    var method = descriptors
                                 .SelectMany(x => x.Services)
                                 .SelectMany(x => x.Methods)
                                 .FirstOrDefault(x => string.Equals(x.FullName, settings.Service));

                    Console.WriteLine(GetMethod(method));
                }
コード例 #5
0
ファイル: DumpCommand.cs プロジェクト: b25/dotnet-grpc-cli
        public override async Task <int> ExecuteAsync(CommandContext context, DumpSettings settings)
        {
            var address = settings.Address;

            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = $"https://{address}";
            }

            if (address.StartsWith("http://"))
            {
                // This switch must be set before creating the GrpcChannel/HttpClient.
                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            }

            var server  = address;
            var channel = Grpc.Net.Client.GrpcChannel.ForAddress(server);
            var client  = new ServerReflection.ServerReflectionClient(channel);
            var stream  = client.ServerReflectionInfo();

            await stream.RequestStream.WriteAsync(new ServerReflectionRequest { FileContainingSymbol = settings.Service });

            await stream.ResponseStream.MoveNext(CancellationToken.None);

            var descriptors = FileDescriptor.BuildFromByteStrings(stream.ResponseStream.Current.FileDescriptorResponse.FileDescriptorProto);
            await stream.RequestStream.CompleteAsync();

            // Output
            if (!string.IsNullOrWhiteSpace(settings.Output))
            {
                settings.Output = Path.GetFullPath(settings.Output);

                if (!Directory.Exists(settings.Output))
                {
                    Directory.CreateDirectory(settings.Output);
                }
            }

            foreach (var descriptor in descriptors)
            {
                if (IsWellKnownType(descriptor))
                {
                    continue;
                }

                TextWriter writer;

                if (string.IsNullOrWhiteSpace(settings.Output))
                {
                    writer = Console.Out;
                    await writer.WriteLineAsync("---");

                    await writer.WriteAsync("File: ");

                    await writer.WriteLineAsync(descriptor.Name);

                    await writer.WriteLineAsync("---");
                }
                else
                {
                    var path      = Path.Join(settings.Output, descriptor.Name);
                    var directory = Path.GetDirectoryName(path);
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    writer = File.CreateText(path.Replace("/", "\\"));
                }

                await WriteFileDescriptor(descriptor, writer);

                await writer.DisposeAsync();
            }

            return(0);
        }