Beispiel #1
0
    public static void Run()
    {
        {
            // The echo service often returns back the same item that it received. We should configure it to enable shared strings
            // and other optimizations.
            SerializerSettings settings = new()
            {
                // Memory copy serialization enables fast re-serialization for already-deserialized flatbuffer items.
                // Since the echo service is simple, this is a good optimization to enable.
                EnableMemoryCopySerialization = true,

                // Turn on writing shared strings.
                SharedStringWriterFactory = () => new SharedStringWriter(),
            };

            // Update the serializers used for the echo service.
            EchoService.Serializer <SingleMessage> .Value = EchoService.Serializer <SingleMessage> .Value.WithSettings(settings);

            EchoService.Serializer <MultiMessage> .Value = EchoService.Serializer <MultiMessage> .Value.WithSettings(settings);
        }

        Server server = new Server();

        server.Ports.Add("127.0.0.1", 50001, ServerCredentials.Insecure);
        server.Services.Add(EchoService.BindService(new ServerImpl()));
        server.Start();

        Thread.Sleep(1000);

        EchoService.EchoServiceClient client = new(new Channel("127.0.0.1", 50001, ChannelCredentials.Insecure));

        // We can use the client in two different ways:
        // 1) as a traditional gRPC client:
        GrpcOperations(client).GetAwaiter().GetResult();

        // 2) As an async interface with Channel<T>
        InterfaceOperations((IEchoService)client).GetAwaiter().GetResult();

        Thread.Sleep(1000);

        server.ShutdownAsync().GetAwaiter().GetResult();
    }
Beispiel #2
0
    private async Task EchoTest(Func <EchoService.EchoServiceClient, Task> callback)
    {
        Server server = new Server();

        try
        {
            server.Services.Add(EchoService.BindService(new EchoServer()));
            server.Ports.Add(new ServerPort("127.0.0.1", 0, ServerCredentials.Insecure));
            server.Start();

            int port = server.Ports.Single().BoundPort;

            EchoService.EchoServiceClient client = new EchoService.EchoServiceClient(new Channel("127.0.0.1", port, ChannelCredentials.Insecure));
            await callback(client);
        }
        finally
        {
            await server.KillAsync();
        }
    }