.NET Core 3.0 gRPC code
using Greet; | |
using Grpc.Core; | |
namespace ClientApp | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var channel = new Channel("localhost:50051", | |
ChannelCredentials.Insecure); | |
var client = new GreeterService.GreeterServiceClient(channel); | |
var greeting = await client.SayHello(new HelloRequest | |
{ | |
Name = "Alice" | |
}); | |
Console.WriteLine(greeting.Message); | |
await channel.ShutdownAsync(); | |
} | |
} | |
} |
public partial class DummyService : ProtoDummyService.ProtoDummyServiceBase | |
{ | |
public override async Task GetDataStream(GetDataStreamRequest request, IServerStreamWriter<CompositeType> responseStream, ServerCallContext context) | |
{ | |
foreach (var item in GetDataStreamImpl(request.Min, request.Max)) | |
{ | |
await responseStream.WriteAsync(item); | |
} | |
} | |
} |
public partial class DummyService | |
{ | |
// Copied from ServiceContract implementation | |
public IEnumerable<CompositeType> GetDataStreamImpl(int min, int max) | |
{ | |
for (int i = min; i < max; i++) | |
{ | |
yield return new CompositeType {IntValue = i, StringValue = i.ToString() }; | |
} | |
} | |
} |
public class DummyService : IDummyService | |
{ | |
public IEnumerable<CompositeType> GetDataStream(int min, int max) | |
{ | |
for (int i = min; i < max; i++) | |
{ | |
yield return new CompositeType {IntValue = i, StringValue = i.ToString() }; | |
} | |
} | |
} |
syntax = "proto3"; | |
package Greet; | |
// The greeting service definition. | |
service Greeter { | |
// Sends a greeting | |
rpc SayHello (HelloRequest) returns (HelloReply) {} | |
} | |
// The request message containing the user's name. | |
message HelloRequest { | |
string name = 1; | |
} | |
// The response message containing the greetings. | |
message HelloReply { | |
string message = 1; | |
} |
using Greet; | |
using Grpc.Core; | |
namespace GrpcDemo | |
{ | |
public class GreeterService : Greeter.GreeterBase | |
{ | |
public override Task<HelloReply> SayHello(HelloRequest request, | |
ServerCallContext context) | |
{ | |
return Task.FromResult(new HelloReply | |
{ | |
Message = "Hello " + request.Name | |
}); | |
} | |
} | |
} |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddGrpc(); | |
} | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
app.UseRouting(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapGrpcService<GreeterService>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment