public GrpcServer(RpcServicePublisher servicePublisher, IServiceProvider?serviceProvider = null, RpcServerOptions?options = null) : this(servicePublisher ?? throw new ArgumentNullException(nameof(servicePublisher)), servicePublisher, servicePublisher.DefinitionsProvider, serviceProvider, options) { }
public void FailUnpublishedServiceProviderStubTest() { var binder = new TestLightweightMethodBinder(); var definitionsProviderMock = new Mock <IRpcServiceDefinitionsProvider>(MockBehavior.Strict); definitionsProviderMock.Setup(p => p.IsServiceRegistered(It.IsAny <Type>())).Returns(true); definitionsProviderMock.Setup(p => p.GetServiceOptions(It.IsAny <Type>())).Returns((RpcServerOptions)null); RpcServicePublisher servicePublisher = new RpcServicePublisher(definitionsProviderMock.Object); var serviceImpl = new AutoPublishServiceProviderServiceImpl(); var publishedServiceScope = servicePublisher.PublishInstance(serviceImpl); CreateSimpleServiceStub <IImplicitServiceProviderService>(servicePublisher, binder, false); LightweightMethodStub getServiceStub = binder.GetHandler <RpcObjectRequest <int>, RpcResponse <RpcObjectRef> >( "SciTech.Rpc.Tests.ImplicitServiceProviderService.GetSimpleService"); Assert.NotNull(getServiceStub); var objectId = publishedServiceScope.Value.ObjectId; Assert.ThrowsAsync <RpcFailureException>(async() => await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest <int>, RpcResponse <RpcObjectRef <ISimpleService> > >( getServiceStub, new RpcObjectRequest <int>(objectId, 1), DefaultSerializer)); }
public async Task GenerateImplicitServiceProviderStubTest() { var binder = new TestLightweightMethodBinder(); var definitionsProviderMock = new Mock <IRpcServiceDefinitionsProvider>(MockBehavior.Strict); definitionsProviderMock.Setup(p => p.IsServiceRegistered(It.IsAny <Type>())).Returns(true); definitionsProviderMock.Setup(p => p.GetServiceOptions(It.IsAny <Type>())).Returns((RpcServerOptions)null); RpcServicePublisher servicePublisher = new RpcServicePublisher(definitionsProviderMock.Object); var serviceImpl = new ImplicitServiceProviderServiceImpl(servicePublisher); var publishedServiceScope = servicePublisher.PublishInstance(serviceImpl); CreateSimpleServiceStub <IImplicitServiceProviderService>(servicePublisher, binder, false); var objectId = publishedServiceScope.Value.ObjectId; LightweightMethodStub getServiceStub = binder.GetHandler <RpcObjectRequest <int>, RpcResponse <RpcObjectRef> >( "SciTech.Rpc.Tests.ImplicitServiceProviderService.GetSimpleService"); Assert.NotNull(getServiceStub); var getServiceResponse = await LightweightStubHelper.SendReceiveAsync <RpcObjectRequest <int>, RpcResponse <RpcObjectRef> >( getServiceStub, new RpcObjectRequest <int>(objectId, 1), DefaultSerializer); Assert.NotNull(getServiceResponse.Result); var actualServiceRef = servicePublisher.GetPublishedInstance(serviceImpl.GetSimpleService(1)); Assert.AreEqual(actualServiceRef, getServiceResponse.Result); }
private static void PublishServices(RpcServicePublisher publisher) { // Publishing RPC services will automatically register the service interfaces, // unless the gRPC server has already been started. // If the server has already been started and an unregistered interface is published, // then an exception will be thrown. publisher.PublishSingleton <GreeterServiceImpl, IGreeterService>(); publisher.PublishSingleton <MailBoxManager, IMailBoxManagerService>(); }
/// <summary> /// /// </summary> /// <param name="servicePublisher"></param> public LightweightRpcServer( RpcServicePublisher servicePublisher, IServiceProvider?serviceProvider = null, IRpcServerOptions?options = null, LightweightOptions?lightweightOptions = null, ILoggerFactory?loggerFactory = null) : this(servicePublisher ?? throw new ArgumentNullException(nameof(servicePublisher)), servicePublisher, servicePublisher.DefinitionsProvider, serviceProvider, options, lightweightOptions, loggerFactory) { }
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"); }
private void CreateSimpleServiceStub <TService>(RpcServicePublisher servicePublisher, ILightweightMethodBinder methodBinder, bool allowAutoPublish) where TService : class { var builder = new LightweightServiceStubBuilder <TService>(new RpcServiceOptions <TService> { Serializer = DefaultSerializer }); var hostMock = new Mock <IRpcServerCore>(MockBehavior.Strict); hostMock.Setup(h => h.ServicePublisher).Returns(servicePublisher); hostMock.Setup(h => h.ServiceActivator).Returns(servicePublisher); hostMock.Setup(h => h.ServiceDefinitionsProvider).Returns(servicePublisher.DefinitionsProvider); hostMock.Setup(h => h.AllowAutoPublish).Returns(allowAutoPublish); hostMock.Setup(h => h.Serializer).Returns(DefaultSerializer); hostMock.Setup(h => h.CustomFaultHandler).Returns((RpcServerFaultHandler)null); hostMock.Setup(h => h.HandleCallException(It.IsAny <Exception>(), It.IsAny <IRpcSerializer>())); hostMock.Setup(p => p.CallInterceptors).Returns(ImmutableArrayList <RpcServerCallInterceptor> .Empty); hostMock.Setup(p => p.HasContextAccessor).Returns(false); hostMock.Setup(p => p.LoggerFactory).Returns(NullLoggerFactory.Instance); builder.GenerateOperationHandlers(hostMock.Object, methodBinder); }
public NetGrpcServer(RpcServicePublisher servicePublisher, IOptions <RpcServerOptions> options, ILoggerFactory?loggerFactory) : this(servicePublisher, servicePublisher, servicePublisher.DefinitionsProvider, options.Value, loggerFactory) { }