Skip to content

Commit 2d56901

Browse files
committed
fix #106
1 parent 6a9dc94 commit 2d56901

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

docs/releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- add `[SimpleRpcExceptions]` (which can be applied at service contract, service type, or service method levels), and `SimpleRpcExceptionsInterceptor` (which can be applied to any service registration) - which expose server exceptions more conveniently (#75)
66
- use linker-friendly metadata inspection (#90)
7+
- add non-generic `AddCodeFirst` native server overload (#106)
78

89
## 1.0.90
910

src/protobuf-net.Grpc.Native/Server/ServiceDefinitionCollectionExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,24 @@ public static int AddCodeFirst<TService>(this ServiceDefinitionCollection servic
3030
TextWriter? log = null,
3131
IEnumerable<Interceptor>? interceptors = null)
3232
where TService : class
33+
=> AddCodeFirstImpl(services, service, typeof(TService), binderConfiguration, log, interceptors);
34+
35+
/// <summary>
36+
/// Adds a code-first service to the available services
37+
/// </summary>
38+
public static int AddCodeFirst(this ServiceDefinitionCollection services, object service,
39+
BinderConfiguration? binderConfiguration = null,
40+
TextWriter? log = null,
41+
IEnumerable<Interceptor>? interceptors = null)
42+
=> AddCodeFirstImpl(services, service, service?.GetType() ?? throw new ArgumentNullException(nameof(service)), binderConfiguration, log, interceptors);
43+
44+
private static int AddCodeFirstImpl(ServiceDefinitionCollection services, object service, Type serviceType,
45+
BinderConfiguration? binderConfiguration,
46+
TextWriter? log,
47+
IEnumerable<Interceptor>? interceptors)
3348
{
3449
var builder = ServerServiceDefinition.CreateBuilder();
35-
int count = Binder.Create(log).Bind<TService>(builder, binderConfiguration, service);
50+
int count = Binder.Create(log).Bind(builder, serviceType, binderConfiguration, service);
3651
var serverServiceDefinition = builder.Build();
3752

3853
if (interceptors is object)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public GrpcServiceFixture()
114114
{
115115
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
116116
};
117-
_server.Services.AddCodeFirst(new ApplyServices());
117+
object nonGeneric = new ApplyServices();
118+
_server.Services.AddCodeFirst(nonGeneric);
118119
_server.Services.AddCodeFirst(new AdhocService(), AdhocConfig.ClientFactory);
119120
_server.Services.AddCodeFirst(new InterceptedService(), interceptors: new[] { _interceptor });
120121
_server.Start();

tests/protobuf-net.Grpc.Test/AttributeDetection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public void CheckTypeAttributes(Type type, bool inherit, string expectedFoo, str
6868
=> CheckResults(AttributeHelper.For(type, inherit), expectedFoo, expectedBar);
6969
private static void CheckResults(AttributeHelper attribs, string expectedFoo, string expectedBar)
7070
{
71-
var fooResult = attribs.TryGetAnyNonWhitespaceString(typeof(FooAttribute).FullName, "Name", out var actualFoo);
72-
var barResult = attribs.TryGetAnyNonWhitespaceString(typeof(BarAttribute).FullName, "Name", out var actualBar);
71+
var fooResult = attribs.TryGetAnyNonWhitespaceString(typeof(FooAttribute).FullName!, "Name", out var actualFoo);
72+
var barResult = attribs.TryGetAnyNonWhitespaceString(typeof(BarAttribute).FullName!, "Name", out var actualBar);
7373

7474
if (string.IsNullOrWhiteSpace(expectedFoo))
7575
{
@@ -120,7 +120,7 @@ private static void CheckResults(AttributeHelper attribs, string expectedFoo, st
120120
[InlineData(typeof(SomeLeafType), "D", false, null, null)]
121121

122122
public void CheckMethodAttributes(Type type, string method, bool inherit, string expectedFoo, string expectedBar)
123-
=> CheckResults(AttributeHelper.For(type.GetMethod(method), inherit), expectedFoo, expectedBar);
123+
=> CheckResults(AttributeHelper.For(type.GetMethod(method)!, inherit), expectedFoo, expectedBar);
124124

125125
[Foo("base")]
126126
[Bar("base bar")]

0 commit comments

Comments
 (0)