Skip to content

Commit

Permalink
Initial project with Grpc Client and Server
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentkempe committed Sep 7, 2019
0 parents commit 796382a
Show file tree
Hide file tree
Showing 13 changed files with 718 additions and 0 deletions.
433 changes: 433 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions grpcAsyncStream.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "grpcAsyncStreamServer", "grpcAsyncStreamServer\grpcAsyncStreamServer.csproj", "{F2C728C7-9708-447E-920A-99479984E76D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "grpcAsyncStreamClient", "grpcAsyncStreamClient\grpcAsyncStreamClient.csproj", "{A2A11C84-4C9D-4990-B603-04A846DE2DB5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F2C728C7-9708-447E-920A-99479984E76D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Debug|x64.ActiveCfg = Debug|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Debug|x64.Build.0 = Debug|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Debug|x86.ActiveCfg = Debug|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Debug|x86.Build.0 = Debug|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Release|Any CPU.Build.0 = Release|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Release|x64.ActiveCfg = Release|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Release|x64.Build.0 = Release|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Release|x86.ActiveCfg = Release|Any CPU
{F2C728C7-9708-447E-920A-99479984E76D}.Release|x86.Build.0 = Release|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Debug|x64.ActiveCfg = Debug|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Debug|x64.Build.0 = Debug|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Debug|x86.ActiveCfg = Debug|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Debug|x86.Build.0 = Debug|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Release|Any CPU.Build.0 = Release|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Release|x64.ActiveCfg = Release|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Release|x64.Build.0 = Release|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Release|x86.ActiveCfg = Release|Any CPU
{A2A11C84-4C9D-4990-B603-04A846DE2DB5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
20 changes: 20 additions & 0 deletions grpcAsyncStreamClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Threading.Tasks;
using Grpc.Net.Client;
using grpcAsyncStreamServer;

namespace grpcAsyncStreamClient
{
class Program
{
static async Task Main(string[] args)
{
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);

var reply = await client.SayHelloAsync(new HelloRequest { Name = "Laurent" });

Console.WriteLine(reply.Message);
}
}
}
21 changes: 21 additions & 0 deletions grpcAsyncStreamClient/Protos/greet.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

option csharp_namespace = "grpcAsyncStreamServer";

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;
}
27 changes: 27 additions & 0 deletions grpcAsyncStreamClient/grpcAsyncStreamClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.9.1" />
<PackageReference Include="Grpc.Core" Version="2.23.0" />
<PackageReference Include="Grpc.Net.Client" Version="0.2.23-pre2" />
<PackageReference Include="Grpc.Tools" Version="2.23.0" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="Protos\greet.proto">
<GrpcServices>Client</GrpcServices>
<Access>Public</Access>
<ProtoCompile>True</ProtoCompile>
<ProtoRoot></ProtoRoot>
<CompileOutputs>True</CompileOutputs>
<OutputDir>obj\Debug\netcoreapp3.0\</OutputDir>
<Generator>MSBuild:Compile</Generator>
</Protobuf>
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions grpcAsyncStreamServer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace grpcAsyncStreamServer
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
12 changes: 12 additions & 0 deletions grpcAsyncStreamServer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"grpcAsyncStream": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
21 changes: 21 additions & 0 deletions grpcAsyncStreamServer/Protos/greet.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

option csharp_namespace = "grpcAsyncStreamServer";

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;
}
26 changes: 26 additions & 0 deletions grpcAsyncStreamServer/Services/GreeterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;

namespace grpcAsyncStreamServer
{
public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}

public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}
}
43 changes: 43 additions & 0 deletions grpcAsyncStreamServer/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace grpcAsyncStreamServer
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();

endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
});
});
}
}
}
10 changes: 10 additions & 0 deletions grpcAsyncStreamServer/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Grpc": "Information",
"Microsoft": "Information"
}
}
}
14 changes: 14 additions & 0 deletions grpcAsyncStreamServer/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
}
}
}
16 changes: 16 additions & 0 deletions grpcAsyncStreamServer/grpcAsyncStreamServer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RootNamespace>grpcAsyncStream</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="0.2.23-pre2" />
</ItemGroup>

</Project>

0 comments on commit 796382a

Please sign in to comment.