Skip to content

Commit 7c5f625

Browse files
committed
Merge branch 'main' of github.com:protobuf-net/protobuf-net.Grpc into main
2 parents 3052d9f + 651b5a1 commit 7c5f625

File tree

5 files changed

+73
-10
lines changed

5 files changed

+73
-10
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [Build Tools ("code-first" focus)](https://protobuf-net.github.io/protobuf-net/build_tools)
77
- [Build Tools ("contract-first" focus)](https://protobuf-net.github.io/protobuf-net/contract_first)
88
- [Create Proto File](createProtoFile)
9+
- [Register Client Service in Startup.cs](registerClientService)
910

1011
Other Content
1112

docs/registerClientService.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Register Client Service
2+
3+
## What is it?
4+
5+
> Client service lets you to inject Grpc client dependency to your controllers.
6+
7+
## Register client service
8+
9+
``` c#
10+
public void ConfigureServices(IServiceCollection services)
11+
{
12+
// other service setup above ...
13+
14+
//this registers ICalculator Grpc client service
15+
services.AddCodeFirstGrpcClient<ICalculator>(o => {
16+
17+
// Address of grpc server
18+
o.Address = new Uri(Configuration.GetValue<string>("https://localhost:5001"));
19+
20+
// another channel options (based on best practices docs on https://docs.microsoft.com/en-us/aspnet/core/grpc/performance?view=aspnetcore-6.0)
21+
o.ChannelOptionsActions.Add(options =>
22+
{
23+
options.HttpHandler = new SocketsHttpHandler()
24+
{
25+
// keeps connection alive
26+
PooledConnectionIdleTimeout = Timeout.InfiniteTimeSpan,
27+
KeepAlivePingDelay = TimeSpan.FromSeconds(60),
28+
KeepAlivePingTimeout = TimeSpan.FromSeconds(30),
29+
30+
// allows channel to add additional HTTP/2 connections
31+
EnableMultipleHttp2Connections = true
32+
};
33+
});
34+
});
35+
}
36+
```
37+
38+
## How use it?
39+
40+
Then in your controller you can use the registered dependency.
41+
42+
``` c#
43+
public class GrpcController : Controller
44+
{
45+
private readonly ICalculator _calculatorClient;
46+
public GrpcController(ICalculator calculatorClient)
47+
{
48+
_calculatorClient = calculatorClient;
49+
}
50+
51+
//otestovat
52+
public async Task<int> Multiply(int x, int y)
53+
{
54+
55+
var result = await _calculatorClient.MultiplyAsync(new MultiplyRequest { X = x, Y = y });
56+
57+
return result;
58+
}
59+
}
60+
```

nuget.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
55
<clear />
66
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
7-
<add key="myget" value="https://www.myget.org/F/protobuf-net/api/v3/index.json " />
7+
<add key="myget" value="https://www.myget.org/F/protobuf-net/api/v3/index.json" />
88
</packageSources>
99
</configuration>

src/protobuf-net.Grpc/Configuration/ServiceBinder.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using ProtoBuf.Grpc.Internal;
2+
using System;
3+
using System.Collections.Concurrent;
24
using System.Collections.Generic;
35
using System.Reflection;
4-
using ProtoBuf.Grpc.Internal;
56

67
namespace ProtoBuf.Grpc.Configuration
78
{
@@ -19,13 +20,14 @@ public class ServiceBinder
1920
/// </summary>
2021
protected ServiceBinder() { }
2122

22-
private readonly Dictionary<Type, InterfaceMapping> _map = new Dictionary<Type, InterfaceMapping>();
23-
private InterfaceMapping GetMap(Type contractType, Type serviceType)
23+
private static readonly ConcurrentDictionary<Type, InterfaceMapping> s_map = new ConcurrentDictionary<Type, InterfaceMapping>();
24+
private static InterfaceMapping GetMap(Type contractType, Type serviceType)
2425
{
25-
if (!_map.TryGetValue(contractType, out var interfaceMapping))
26-
{
26+
if (!s_map.TryGetValue(contractType, out var interfaceMapping))
27+
{ // note: it doesn't matter if this ends up getting called more than once
28+
// in a race condition - we don't need to block etc (the result will be compatible)
2729
interfaceMapping = serviceType.GetInterfaceMap(contractType);
28-
_map[contractType] = interfaceMapping;
30+
s_map[contractType] = interfaceMapping;
2931
}
3032
return interfaceMapping;
3133
}

tests/protobuf-net.Grpc.Test.Integration/GrpcServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void CanCallAllApplyServicesTypedUnarySync()
291291
}
292292

293293
[Fact]
294-
public void CanCallAdocService()
294+
public void CanCallAdhocService()
295295
{
296296
GrpcClientFactory.AllowUnencryptedHttp2 = true;
297297
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
@@ -318,4 +318,4 @@ public async Task CanCallInterceptedService()
318318
}
319319
}
320320
}
321-
#endif
321+
#endif

0 commit comments

Comments
 (0)