コード例 #1
0
        protected override void AddEventHandlerDefinition <TEventArgs>(
            RpcEventInfo eventInfo,
            Func <RpcObjectRequest, IServiceProvider?, IRpcAsyncStreamWriter <TEventArgs>, IRpcContext, ValueTask> beginEventProducer,
            RpcStub <TService> serviceStub,
            IGrpcMethodBinder binder)
        {
            GrpcCore.ServerStreamingServerMethod <RpcObjectRequest, TEventArgs> handler = (request, responseStream, context) =>
            {
                using (var scope = CreateServiceScope(serviceStub))
                {
                    return(beginEventProducer(request,
                                              scope?.ServiceProvider,
                                              new GrpcAsyncStreamWriter <TEventArgs>(responseStream),
                                              new GrpcCallContext(context)).AsTask());
                }
            };

            var beginEventProducerName = $"Begin{eventInfo.Name}";

            binder.AddMethod(
                GrpcMethodDefinition.Create <RpcObjectRequest, TEventArgs>(
                    GrpcCore.MethodType.ServerStreaming,
                    eventInfo.FullServiceName,
                    beginEventProducerName,
                    serviceStub.Serializer),
                handler);
        }
コード例 #2
0
        protected override void AddCallbackMethodCore <TRequest, TReturn, TResponseReturn>(
            Func <TService, TRequest, Action <TReturn>, CancellationToken, Task> serviceCaller,
            Func <TReturn, TResponseReturn>?responseConverter, RpcServerFaultHandler faultHandler,
            RpcStub <TService> serviceStub, RpcOperationInfo operationInfo, IGrpcMethodBinder binder)
            where TResponseReturn : class
        {
            var serializer = serviceStub.Serializer;

            GrpcCore.ServerStreamingServerMethod <TRequest, TResponseReturn> handler = (request, responseStream, context) =>
            {
                using (var serviceScope = CreateServiceScope(serviceStub))
                {
                    return(serviceStub.CallCallbackMethod(
                               request,
                               serviceScope?.ServiceProvider,
                               new GrpcCallContext(context),
                               new GrpcAsyncStreamWriter <TResponseReturn>(responseStream),
                               serviceCaller,
                               responseConverter,
                               faultHandler,
                               serializer).AsTask());
                }
            };

            binder.AddMethod(
                GrpcMethodDefinition.Create <TRequest, TResponseReturn>(
                    GrpcCore.MethodType.ServerStreaming,
                    operationInfo.FullServiceName,
                    operationInfo.Name,
                    serviceStub.Serializer),
                handler);
        }
コード例 #3
0
        protected override void AddGenericBlockingMethodCore <TRequest, TReturn, TResponseReturn>(
            Func <TService, TRequest, CancellationToken, TReturn> serviceCaller,
            Func <TReturn, TResponseReturn>?responseConverter,
            RpcServerFaultHandler faultHandler,
            RpcStub <TService> serviceStub,
            RpcOperationInfo operationInfo,
            IGrpcMethodBinder binder)
        {
            var serializer = serviceStub.Serializer;

            Task <RpcResponse <TResponseReturn> > handler(TRequest request, GrpcCore.ServerCallContext context)
            {
                using (var serviceScope = CreateServiceScope(serviceStub))
                {
                    return(serviceStub.CallBlockingMethod(
                               request, new GrpcCallContext(context), serviceCaller, responseConverter,
                               faultHandler, serializer, serviceScope?.ServiceProvider).AsTask());
                }
            }

            binder.AddMethod(
                GrpcMethodDefinition.Create <TRequest, RpcResponse <TResponseReturn> >(GrpcCore.MethodType.Unary,
                                                                                       operationInfo.FullServiceName, operationInfo.Name, serializer),
                handler);
        }
コード例 #4
0
        //private static void CreateSimpleServiceStub<TService>(TService serviceImpl, IGrpcMethodBinder methodBinder) where TService : class
        //{
        //    var builder = new GrpcServiceStubBuilder<TService>(new ProtobufSerializer());
        //    var hostMock = new Mock<IRpcServiceHostImpl>(MockBehavior.Strict);
        //    var serviceImplProviderMock = new Mock<IServiceImplProvider>();
        //    serviceImplProviderMock.Setup(p => p.GetServiceImpl<TService>(It.IsAny<RpcObjectId>())).Returns(serviceImpl);
        //    hostMock.Setup(p => p.ServiceImplProvider).Returns(serviceImplProviderMock.Object);
        //    hostMock.Setup(p => p.CallInterceptors).Returns(ImmutableArray<RpcHostCallInterceptor>.Empty);
        //    builder.GenerateOperationHandlers(hostMock.Object, methodBinder);
        //}

        private static void CreateSimpleServiceStub <TService>(TService serviceImpl, IGrpcMethodBinder methodBinder) where TService : class
        {
            var builder = new GrpcServiceStubBuilder <TService>(new RpcServiceOptions <TService> {
                Serializer = new ProtobufRpcSerializer()
            });

            var hostMock = new Mock <IRpcServerCore>(MockBehavior.Strict);

            var servicePublisherMock           = new Mock <IRpcServicePublisher>(MockBehavior.Strict);
            var serviceDefinitionsProviderMock = new Mock <IRpcServiceDefinitionsProvider>(MockBehavior.Strict);

            serviceDefinitionsProviderMock.Setup(p => p.GetServiceOptions(It.IsAny <Type>())).Returns((RpcServerOptions)null);

            var serviceImplProviderMock = new Mock <IRpcServiceActivator>(MockBehavior.Strict);

            serviceImplProviderMock.Setup(p => p.GetActivatedService <TService>(It.IsAny <IServiceProvider>(), It.IsAny <RpcObjectId>())).Returns(new ActivatedService <TService>(serviceImpl, null));


            hostMock.Setup(h => h.ServicePublisher).Returns(servicePublisherMock.Object);
            hostMock.Setup(h => h.ServiceDefinitionsProvider).Returns(serviceDefinitionsProviderMock.Object);
            hostMock.Setup(h => h.ServiceActivator).Returns(serviceImplProviderMock.Object);
            hostMock.Setup(h => h.CallInterceptors).Returns(ImmutableArrayList <RpcServerCallInterceptor> .Empty);
            hostMock.Setup(h => h.ServiceProvider).Returns((IServiceProvider)null);
            hostMock.Setup(h => h.AllowAutoPublish).Returns(false);
            hostMock.Setup(h => h.Serializer).Returns(DefaultSerializer);
            hostMock.Setup(h => h.CustomFaultHandler).Returns((RpcServerFaultHandler)null);
            hostMock.Setup(p => p.HasContextAccessor).Returns(false);
            hostMock.Setup(p => p.LoggerFactory).Returns(NullLoggerFactory.Instance);

            builder.GenerateOperationHandlers(hostMock.Object, methodBinder);
        }
コード例 #5
0
        protected override void AddGenericVoidBlockingMethodCore <TRequest>(
            Action <TService, TRequest, CancellationToken> serviceCaller,
            RpcServerFaultHandler faultHandler,
            RpcStub <TService> serviceStub,
            RpcOperationInfo operationInfo,
            IGrpcMethodBinder binder)
        {
            var serializer = serviceStub.Serializer;

            GrpcCore.UnaryServerMethod <TRequest, RpcResponse> handler = (request, context) =>
            {
                using (var serviceScope = CreateServiceScope(serviceStub))
                {
                    return(serviceStub.CallVoidBlockingMethod(
                               request, serviceScope?.ServiceProvider, new GrpcCallContext(context), serviceCaller,
                               faultHandler, serializer).AsTask());
                }
            };

            binder.AddMethod(
                GrpcMethodDefinition.Create <TRequest, RpcResponse>(GrpcCore.MethodType.Unary,
                                                                    operationInfo.FullServiceName, operationInfo.Name, serializer),
                handler);
        }