|
19 | 19 | using Grpc.AspNetCore.Server.Internal;
|
20 | 20 | using Grpc.Core.Interceptors;
|
21 | 21 | using Grpc.Tests.Shared;
|
| 22 | +using Microsoft.Extensions.DependencyInjection; |
22 | 23 | using Moq;
|
23 | 24 | using NUnit.Framework;
|
24 | 25 |
|
@@ -49,6 +50,23 @@ public GrpcIntArgumentInterceptor(int x)
|
49 | 50 | public void Dispose() => Disposed = true;
|
50 | 51 | }
|
51 | 52 |
|
| 53 | +#if NET8_0_OR_GREATER |
| 54 | + public class GrpcKeyServiceArgumentInterceptor : Interceptor |
| 55 | + { |
| 56 | + public GrpcKeyServiceArgumentInterceptor([FromKeyedServices("test")] KeyedClass c) |
| 57 | + { |
| 58 | + C = c; |
| 59 | + } |
| 60 | + |
| 61 | + public KeyedClass C { get; } |
| 62 | + } |
| 63 | + |
| 64 | + public class KeyedClass |
| 65 | + { |
| 66 | + public required string Key { get; init; } |
| 67 | + } |
| 68 | +#endif |
| 69 | + |
52 | 70 | public class GrpcIntMutexArgumentInterceptor : Interceptor
|
53 | 71 | {
|
54 | 72 | public GrpcIntMutexArgumentInterceptor(int x, Mutex mutex)
|
@@ -94,6 +112,25 @@ public void Create_NotResolvedFromServiceProvider_CreatedByActivator()
|
94 | 112 | Assert.IsTrue(handle.Created);
|
95 | 113 | }
|
96 | 114 |
|
| 115 | +#if NET8_0_OR_GREATER |
| 116 | + [Test] |
| 117 | + public void Create_KeyService_CreatedByActivator() |
| 118 | + { |
| 119 | + // Arrange |
| 120 | + var activator = new DefaultGrpcInterceptorActivator<GrpcKeyServiceArgumentInterceptor>(); |
| 121 | + var services = new ServiceCollection(); |
| 122 | + services.AddKeyedSingleton("test", new KeyedClass { Key = "test" }); |
| 123 | + var serviceProvider = services.BuildServiceProvider(); |
| 124 | + |
| 125 | + // Act |
| 126 | + var handle = activator.Create(serviceProvider, CreateRegistration<GrpcKeyServiceArgumentInterceptor>()); |
| 127 | + |
| 128 | + // Assert |
| 129 | + var interceptor = (GrpcKeyServiceArgumentInterceptor)handle.Instance; |
| 130 | + Assert.AreEqual("test", interceptor.C.Key); |
| 131 | + } |
| 132 | +#endif |
| 133 | + |
97 | 134 | [Test]
|
98 | 135 | public void Create_ResolvedFromServiceProvider_NotCreatedByActivator()
|
99 | 136 | {
|
|
0 commit comments