public async Task CreateClient_LoggingSetup_ClientLogsToTestSink() { // Arrange var testSink = new TestSink(); var services = new ServiceCollection(); HttpContextHelpers.SetupHttpContext(services); var clientBuilder = services.AddGrpcClient <TestGreeterClient>("contoso", options => { options.BaseAddress = new Uri("http://contoso"); }).AddHttpMessageHandler(() => new TestDelegatingHandler()); services.AddLogging(configure => configure.SetMinimumLevel(LogLevel.Trace)); services.TryAddEnumerable(ServiceDescriptor.Singleton <ILoggerProvider, TestLoggerProvider>(s => new TestLoggerProvider(testSink, true))); var provider = services.BuildServiceProvider(); // Act var clientFactory = provider.GetRequiredService <GrpcClientFactory>(); var contosoClient = clientFactory.CreateClient <TestGreeterClient>("contoso"); var response = await contosoClient.SayHelloAsync(new HelloRequest()); // Assert Assert.AreEqual("http://contoso", contosoClient.GetCallInvoker().BaseAddress.OriginalString); Assert.IsTrue(testSink.Writes.Any(w => w.EventId.Name == "StartingCall")); }
public void ResolveDefaultAndNamedClients_ClientsUseCorrectConfiguration() { // Arrange var services = new ServiceCollection(); HttpContextHelpers.SetupHttpContext(services); services.AddGrpcClient <TestGreeterClient>(options => { options.BaseAddress = new Uri("http://testgreeterclient"); }); services.AddGrpcClient <TestSecondGreeterClient>("contoso", options => { options.BaseAddress = new Uri("http://contoso"); }); services.AddGrpcClient <TestSecondGreeterClient>(options => { options.BaseAddress = new Uri("http://testsecondgreeterclient"); }); var provider = services.BuildServiceProvider(); // Act var client = provider.GetRequiredService <TestGreeterClient>(); var secondClient = provider.GetRequiredService <TestSecondGreeterClient>(); var factory = provider.GetRequiredService <GrpcClientFactory>(); var contosoClient = factory.CreateClient <TestSecondGreeterClient>("contoso"); // Assert Assert.AreEqual("http://testgreeterclient", client.GetCallInvoker().BaseAddress.OriginalString); Assert.AreEqual("http://testsecondgreeterclient", secondClient.GetCallInvoker().BaseAddress.OriginalString); Assert.AreEqual("http://contoso", contosoClient.GetCallInvoker().BaseAddress.OriginalString); }
public void CreateClient_MultipleNamedClients_ReturnMatchingClient() { // Arrange var services = new ServiceCollection(); HttpContextHelpers.SetupHttpContext(services); services.AddGrpcClient <TestGreeterClient>("contoso", options => { options.BaseAddress = new Uri("http://contoso"); }); services.AddGrpcClient <TestGreeterClient>("adventureworks", options => { options.BaseAddress = new Uri("http://adventureworks"); }); var provider = services.BuildServiceProvider(); // Act var clientFactory = provider.GetRequiredService <GrpcClientFactory>(); var contosoClient = clientFactory.CreateClient <TestGreeterClient>("contoso"); var adventureworksClient = clientFactory.CreateClient <TestGreeterClient>("adventureworks"); // Assert Assert.AreEqual("http://contoso", contosoClient.GetCallInvoker().BaseAddress.OriginalString); Assert.AreEqual("http://adventureworks", adventureworksClient.GetCallInvoker().BaseAddress.OriginalString); }
public void CreateClient_UnmatchedName_ThrowError() { // Arrange var services = new ServiceCollection(); HttpContextHelpers.SetupHttpContext(services); services.AddGrpcClient <TestGreeterClient>(options => { options.BaseAddress = new Uri("http://contoso"); }); var provider = services.BuildServiceProvider(); var clientFactory = provider.GetRequiredService <GrpcClientFactory>(); // Act var ex = Assert.Throws <InvalidOperationException>(() => clientFactory.CreateClient <TestGreeterClient>("DOES_NOT_EXIST")); // Assert Assert.AreEqual("No gRPC client configured with name 'DOES_NOT_EXIST'.", ex.Message); }
public void UseRequestCancellationTokenIsTrue_HasHttpContext_UseRequestToken() { // Arrange var cts = new CancellationTokenSource(); var services = new ServiceCollection(); HttpContextHelpers.SetupHttpContext(services, cts.Token); services.AddGrpcClient <TestGreeterClient>(options => { options.PropagateCancellationToken = true; }); var provider = services.BuildServiceProvider(); // Act var client = provider.GetRequiredService <TestGreeterClient>(); // Assert Assert.AreEqual(cts.Token, client.GetCallInvoker().CancellationToken); }