Last active
June 7, 2019 18:56
-
-
Save markrendle/58b30c96cdf2ee457cc8c40169f3b74b to your computer and use it in GitHub Desktop.
.NET Core 3.0 gRPC code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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