Skip to content

Commit 291ddf3

Browse files
authored
Add code-first sample (#1142)
1 parent 543ee70 commit 291ddf3

File tree

16 files changed

+382
-0
lines changed

16 files changed

+382
-0
lines changed

build/dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<NunitPackageVersion>3.12.0</NunitPackageVersion>
2121
<Nunit3TestAdapterPackageVersion>3.16.1</Nunit3TestAdapterPackageVersion>
2222
<OpenTelemetryPackageVersion>0.6.0-beta.1</OpenTelemetryPackageVersion>
23+
<ProtobufNetGrpcPackageVersion>1.0.140</ProtobufNetGrpcPackageVersion>
2324
<SystemCommandLinePackageVersion>2.0.0-beta1.20214.1</SystemCommandLinePackageVersion>
2425
<SystemCommandLineRenderingPackageVersion>0.3.0-alpha.20214.1</SystemCommandLineRenderingPackageVersion>
2526
<SystemDiagnosticsDiagnosticSourcePackageVersion>4.5.1</SystemDiagnosticsDiagnosticSourcePackageVersion>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\Shared\Shared.csproj" />
10+
11+
<PackageReference Include="Grpc.Net.Client" Version="$(GrpcDotNetPackageVersion)" />
12+
</ItemGroup>
13+
</Project>

examples/Coder/Client/Program.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System;
20+
using System.Threading.Tasks;
21+
using Grpc.Net.Client;
22+
using ProtoBuf.Grpc.Client;
23+
using Shared;
24+
25+
namespace Client
26+
{
27+
public class Program
28+
{
29+
static async Task Main(string[] args)
30+
{
31+
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
32+
var client = channel.CreateGrpcService<IGreeterService>();
33+
34+
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
35+
Console.WriteLine("Greeting: " + reply.Message);
36+
37+
Console.WriteLine("Shutting down");
38+
Console.WriteLine("Press any key to exit...");
39+
Console.ReadKey();
40+
}
41+
}
42+
}

examples/Coder/Coder.sln

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29230.61
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{9E978075-E00D-4FA9-9765-18CB51C5F5EE}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{9E978075-E00D-4FA9-9765-18CB51C5F5EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{9E978075-E00D-4FA9-9765-18CB51C5F5EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{9E978075-E00D-4FA9-9765-18CB51C5F5EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{9E978075-E00D-4FA9-9765-18CB51C5F5EE}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {D22B3129-3BFB-41FA-9FCE-E45EBEF8C2DD}
36+
EndGlobalSection
37+
EndGlobal

examples/Coder/Server/Program.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Microsoft.AspNetCore.Hosting;
20+
using Microsoft.Extensions.Hosting;
21+
22+
namespace Server
23+
{
24+
public class Program
25+
{
26+
public static void Main(string[] args)
27+
{
28+
CreateHostBuilder(args).Build().Run();
29+
}
30+
31+
public static IHostBuilder CreateHostBuilder(string[] args) =>
32+
Host.CreateDefaultBuilder(args)
33+
.ConfigureWebHostDefaults(webBuilder =>
34+
{
35+
webBuilder.UseStartup<Startup>();
36+
});
37+
}
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\Shared\Shared.csproj" />
9+
10+
<PackageReference Include="protobuf-net.Grpc.AspNetCore" Version="$(ProtobufNetGrpcPackageVersion)" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System.Threading.Tasks;
20+
using Microsoft.Extensions.Logging;
21+
using ProtoBuf.Grpc;
22+
using Shared;
23+
24+
namespace Server
25+
{
26+
public class GreeterService : IGreeterService
27+
{
28+
private readonly ILogger _logger;
29+
30+
public GreeterService(ILoggerFactory loggerFactory)
31+
{
32+
_logger = loggerFactory.CreateLogger<GreeterService>();
33+
}
34+
35+
public Task<HelloReply> SayHelloAsync(HelloRequest request, CallContext context = default)
36+
{
37+
_logger.LogInformation($"Sending hello to {request.Name}");
38+
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
39+
}
40+
}
41+
}

examples/Coder/Server/Startup.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Microsoft.AspNetCore.Builder;
20+
using Microsoft.AspNetCore.Hosting;
21+
using Microsoft.Extensions.DependencyInjection;
22+
using Microsoft.Extensions.Hosting;
23+
using ProtoBuf.Grpc.Server;
24+
25+
namespace Server
26+
{
27+
public class Startup
28+
{
29+
public void ConfigureServices(IServiceCollection services)
30+
{
31+
services.AddGrpc();
32+
services.AddCodeFirstGrpc();
33+
}
34+
35+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
36+
{
37+
if (env.IsDevelopment())
38+
{
39+
app.UseDeveloperExceptionPage();
40+
}
41+
42+
app.UseRouting();
43+
44+
app.UseEndpoints(endpoints =>
45+
{
46+
endpoints.MapGrpcService<GreeterService>();
47+
});
48+
}
49+
}
50+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Grpc": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)