Skip to content

Commit 44b243f

Browse files
JamesNKaka-nse
andauthored
MapGrpcService with service definition refactor (#2634)
Co-authored-by: aka-nse <[email protected]>
1 parent 66df240 commit 44b243f

38 files changed

+607
-79
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Client" Link="Protos\greet.proto" />
10+
11+
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
12+
<PackageReference Include="Grpc.Tools" Version="$(GrpcToolsPackageVersion)" PrivateAssets="All" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\..\src\Grpc.Net.Client\Grpc.Net.Client.csproj" />
17+
</ItemGroup>
18+
</Project>

examples/Definer/Client/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 Greet;
20+
using Grpc.Net.Client;
21+
22+
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
23+
var client = new Greeter.GreeterClient(channel);
24+
25+
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
26+
Console.WriteLine("Greeting: " + reply.Message);
27+
28+
Console.WriteLine("Shutting down");
29+
Console.WriteLine("Press any key to exit...");
30+
Console.ReadKey();

examples/Definer/Definer.slnx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Solution>
2+
<Folder Name="/ref/">
3+
<Project Path="../../src/Grpc.AspNetCore.Server.ClientFactory/Grpc.AspNetCore.Server.ClientFactory.csproj" />
4+
<Project Path="../../src/Grpc.AspNetCore.Server/Grpc.AspNetCore.Server.csproj" />
5+
<Project Path="../../src/Grpc.AspNetCore/Grpc.AspNetCore.csproj" />
6+
<Project Path="../../src/Grpc.Core.Api/Grpc.Core.Api.csproj" />
7+
<Project Path="../../src/Grpc.Net.Client/Grpc.Net.Client.csproj" />
8+
<Project Path="../../src/Grpc.Net.ClientFactory/Grpc.Net.ClientFactory.csproj" />
9+
<Project Path="../../src/Grpc.Net.Common/Grpc.Net.Common.csproj" />
10+
</Folder>
11+
<Project Path="Client/Client.csproj" />
12+
<Project Path="Server/Server.csproj" />
13+
</Solution>

examples/Definer/Proto/greet.proto

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2019 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package greet;
18+
19+
// The greeting service definition.
20+
service Greeter {
21+
// Sends a greeting
22+
rpc SayHello (HelloRequest) returns (HelloReply);
23+
}
24+
25+
// The request message containing the user's name.
26+
message HelloRequest {
27+
string name = 1;
28+
}
29+
30+
// The response message containing the greetings
31+
message HelloReply {
32+
string message = 1;
33+
}

testassets/FunctionalTestsWebsite/Infrastructure/DynamicServiceModelProvider.cs renamed to examples/Definer/Server/Program.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -16,19 +16,20 @@
1616

1717
#endregion
1818

19-
using System.Diagnostics;
20-
using Grpc.AspNetCore.Server.Model;
19+
using Grpc.Core;
20+
using Microsoft.AspNetCore.Builder;
21+
using Microsoft.Extensions.DependencyInjection;
22+
using Server;
2123

22-
namespace FunctionalTestsWebsite.Infrastructure;
24+
var builder = WebApplication.CreateBuilder(args);
25+
builder.Services.AddGrpc();
2326

24-
public class DynamicServiceModelProvider : IServiceMethodProvider<DynamicService>
27+
var app = builder.Build();
28+
app.MapGrpcService(services =>
2529
{
26-
public Action<ServiceMethodProviderContext<DynamicService>>? CreateMethod { get; set; }
30+
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
31+
var service = new GreeterService(loggerFactory);
32+
return Greet.Greeter.BindService(service);
33+
});
2734

28-
public void OnServiceMethodDiscovery(ServiceMethodProviderContext<DynamicService> context)
29-
{
30-
Debug.Assert(CreateMethod != null);
31-
32-
CreateMethod(context);
33-
}
34-
}
35+
app.Run();
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\src\Grpc.AspNetCore\Grpc.AspNetCore.csproj" />
13+
<ProjectReference Include="..\..\..\src\Grpc.AspNetCore.Server\Grpc.AspNetCore.Server.csproj" />
14+
</ItemGroup>
15+
16+
</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 Greet;
21+
using Grpc.Core;
22+
using Microsoft.Extensions.Logging;
23+
24+
namespace Server
25+
{
26+
public class GreeterService : Greeter.GreeterBase
27+
{
28+
private readonly ILogger _logger;
29+
30+
public GreeterService(ILoggerFactory loggerFactory)
31+
{
32+
_logger = loggerFactory.CreateLogger<GreeterService>();
33+
}
34+
35+
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
36+
{
37+
_logger.LogInformation($"Sending hello to {request.Name}");
38+
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
39+
}
40+
}
41+
}
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
"AllowedHosts": "*",
8+
"Kestrel": {
9+
"EndpointDefaults": {
10+
"Protocols": "Http2"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)