public async Task DeviceServiceTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <IThermostatService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new ThermostatServiceImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; GrpcConnection connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <IThermostatServiceClient>(objectId); var acoId = clientService.DeviceAcoId; var baseClientService = (IDeviceServiceClient)clientService; var acoId2 = baseClientService.DeviceAcoId; Assert.AreEqual(acoId, acoId2); } } finally { await host.ShutdownAsync(); } }
public async Task SimpleObjectServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <ISimpleService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new TestSimpleServiceImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <ISimpleService>(objectId); int res = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); } } finally { await host.ShutdownAsync(); } }
public async Task ServiceProviderServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <IServiceProviderService>() .RegisterService <ISimpleService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new ServiceProviderServiceImpl(host.ServicePublisher); using (var publishScope = host.PublishSingleton <IServiceProviderService>(serviceImpl)) { var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceSingleton <IServiceProviderServiceClient>(); var serviceRef = await clientService.GetSimpleServiceAsync(); var simpleService = connection.GetServiceInstance(serviceRef); int res = await simpleService.AddAsync(8, 9); Assert.AreEqual(8 + 9, res); } } finally { await host.ShutdownAsync(); } }
public async Task EventHandlersTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder.RegisterService <ISimpleServiceWithEvents>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new TestServiceWithEventsImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <ISimpleServiceWithEvents>(objectId); TaskCompletionSource <ValueChangedEventArgs> detailedTcs = new TaskCompletionSource <ValueChangedEventArgs>(); EventHandler <ValueChangedEventArgs> detailedHandler = (s, e) => { detailedTcs.SetResult(e); }; clientService.DetailedValueChanged += detailedHandler; await((IRpcProxy)clientService).WaitForPendingEventHandlersAsync(); clientService.SetValueAsync(12).Forget(); var completedTask = await Task.WhenAny(detailedTcs.Task, Task.Delay(1000)); Assert.AreEqual(detailedTcs.Task, completedTask); Assert.IsTrue(completedTask.IsCompletedSuccessfully()); var detailedArgs = detailedTcs.Task.Result; clientService.DetailedValueChanged -= detailedHandler; await((IRpcProxy)clientService).WaitForPendingEventHandlersAsync(); clientService.SetValueAsync(13).Forget(); await Task.Delay(200); Assert.IsFalse(serviceImpl.HasDetailedValueChangedHandler); Assert.IsFalse(serviceImpl.HasValueChangedHandler); Assert.AreEqual(12, detailedArgs.NewValue); Assert.AreEqual(0, detailedArgs.OldValue); } } finally { await host.ShutdownAsync(); } }
public void Dispose() { CloseServerAction.Invoke(); GrpcServer.ShutdownAsync().Wait(); var port = GrpcServer.Ports.FirstOrDefault(); Logger.Info(message: LoggerMessages.ServerClosedMessage(port.Host, port.Port)); }
public static async Task Main(string[] args) { // This example shows how to explicitly setup a gRPC RPC server and a Pipelines RPC server // with a common service publisher. // // In a real scenario, it is probably more suitable to use the .NET generic host // (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host). // // The NetGrpcServer example shows what a host setup could look like. Console.WriteLine("Initializing gRPC server and pipelines server."); var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); var definitionsBuilder = new RpcServiceDefinitionBuilder(); var rpcPublisher = new RpcServicePublisher(definitionsBuilder); RegisterServiceDefinitions(definitionsBuilder); PublishServices(rpcPublisher); var serializer = new ProtobufSerializer(); var grpcServer = new GrpcServer(rpcPublisher, serviceProvider, serializer); grpcServer.AllowAutoPublish = true; grpcServer.AddEndPoint(CreateGrpcEndPoint(50051)); var pipelinesServer = new RpcPipelinesServer(rpcPublisher, serviceProvider, serializer); pipelinesServer.AllowAutoPublish = true; pipelinesServer.AddEndPoint(new TcpPipelinesEndPoint("127.0.0.1", 50052, false)); Console.WriteLine("Starting gRPC server and pipelines server."); // Once the gRPC server is started, it is no longer possible to register service definitions. grpcServer.Start(); pipelinesServer.Start(); Console.WriteLine("gRPC server and pipelines server are running, press any key to shutdown."); Console.ReadKey(); Console.WriteLine("Shutting down servers."); await Task.WhenAll(grpcServer.ShutdownAsync(), pipelinesServer.ShutdownAsync()); Console.WriteLine("Ended"); }
public async Task BlockingServiceCallTest() { var serverBuilder = new RpcServiceDefinitionsBuilder(); serverBuilder .RegisterService <IBlockingService>() .RegisterService <ISimpleService>(); var host = new GrpcServer(serverBuilder, null, this.options); host.AddEndPoint(CreateEndPoint()); host.Start(); try { var serviceImpl = new TestBlockingSimpleServiceImpl(); using (var publishScope = host.PublishInstance(serviceImpl)) { var objectId = publishScope.Value.ObjectId; var connection = this.CreateGrpcConnection(); var clientService = connection.GetServiceInstance <IBlockingServiceClient>(objectId); int blockingRes = clientService.Add(12, 13); Assert.AreEqual(12 + 13, blockingRes); int asyncRes = await clientService.AddAsync(8, 9); Assert.AreEqual(8 + 9, asyncRes); clientService.Value = 123.45; Assert.AreEqual(123.45, await clientService.GetValueAsync()); await clientService.SetValueAsync(543.21); Assert.AreEqual(543.21, clientService.Value); } } finally { await host.ShutdownAsync(); } }
private void OnStopping() { _logger.LogInformation("OnStopping has been called.", null); _server.ShutdownAsync().ConfigureAwait(false).GetAwaiter().GetResult(); }
public void Stop() { GrpcServer.ShutdownAsync().Wait(); ServerStarted = false; }