Beispiel #1
0
        public GrpcServer(
            IRpcServicePublisher servicePublisher,
            IRpcServiceActivator serviceImplProvider,
            IRpcServiceDefinitionsProvider serviceDefinitionsProvider,
            IServiceProvider?serviceProvider,
            RpcServerOptions?options = null)
            : base(servicePublisher, serviceImplProvider, serviceDefinitionsProvider, options)
        {
            this.ServiceProvider = serviceProvider;

            List <GrpcCore.ChannelOption> channelOptions = new List <GrpcCore.ChannelOption>();

            var maxReceiveMessageSize = options?.ReceiveMaxMessageSize;

            if (maxReceiveMessageSize != null)
            {
                channelOptions.Add(new GrpcCore.ChannelOption(GrpcCore.ChannelOptions.MaxReceiveMessageLength, maxReceiveMessageSize.Value));
            }

            var maxSendMessageSize = options?.SendMaxMessageSize;

            if (maxSendMessageSize != null)
            {
                channelOptions.Add(new GrpcCore.ChannelOption(GrpcCore.ChannelOptions.MaxSendMessageLength, maxSendMessageSize.Value));
            }

            this.grpcServer = new GrpcCore.Server(channelOptions);
        }
Beispiel #2
0
 /// <summary>
 /// Only intended for testing.
 /// </summary>
 /// <param name="servicePublisher"></param>
 /// <param name="serviceImplProvider"></param>
 /// <param name="definitionsProvider"></param>
 protected RpcServerHostBase(
     IRpcServicePublisher servicePublisher, IRpcServiceActivator serviceImplProvider,
     IRpcServiceDefinitionsProvider definitionsProvider, IRpcServerOptions?options,
     ILoggerFactory?loggerFactory = null)
     : base(servicePublisher, serviceImplProvider, definitionsProvider, options, loggerFactory)
 {
 }
 public ImplicitServiceProviderServiceImpl(IRpcServicePublisher publisher)
 {
     this.simpleServiceScopes = Array.ConvertAll(this.serviceImpls,
                                                 s => publisher.PublishInstance <ISimpleService>(s));
     this.blockingServiceScopes = Array.ConvertAll(this.blockingServiceImpls,
                                                   s => publisher.PublishInstance <IBlockingService>(s));
 }
        protected RpcServerBase(
            IRpcServicePublisher servicePublisher, IRpcServiceActivator serviceActivator,
            IRpcServiceDefinitionsProvider definitionsProvider, IRpcServerOptions?options,
            ILoggerFactory?loggerFactory)
        {
            this.ServicePublisher           = servicePublisher ?? throw new ArgumentNullException(nameof(servicePublisher));
            this.ServiceActivator           = serviceActivator ?? throw new ArgumentNullException(nameof(serviceActivator));
            this.ServiceDefinitionsProvider = definitionsProvider ?? throw new ArgumentNullException(nameof(definitionsProvider));
            this.LoggerFactory = loggerFactory;
            this.Logger        = loggerFactory?.CreateLogger(this.GetType()) ?? RpcLogger.CreateLogger(this.GetType());

            if (options != null)
            {
                this.serializer       = options.Serializer;
                this.AllowDiscovery   = options.AllowDiscovery ?? true;
                this.AllowAutoPublish = options.AllowAutoPublish ?? false;

                this.CallInterceptors    = options.Interceptors.ToImmutableArrayList();
                this.ExceptionConverters = options.ExceptionConverters.ToImmutableArrayList();
            }

            if (this.ExceptionConverters.Count > 0)
            {
                this.CustomFaultHandler = new RpcServerFaultHandler(null, this.ExceptionConverters, null);
            }
            else
            {
                this.CustomFaultHandler = RpcServerFaultHandler.Default;
            }
        }
        public static IOwned <RpcObjectRef <TService> > PublishInstance <TService>(this IRpcServicePublisher servicePublisher, TService serviceInstance, bool takeOwnership = false) where TService : class
        {
            if (servicePublisher is null)
            {
                throw new ArgumentNullException(nameof(servicePublisher));
            }

            return(servicePublisher.PublishInstance(takeOwnership ? OwnedObject.Create(serviceInstance) : OwnedObject.CreateUnowned(serviceInstance)));
        }
        public static void UnpublishSingleton <TService>(this IRpcServicePublisher publisher) where TService : class
        {
            if (publisher is null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }

            publisher.UnpublishSingletonAsync <TService>().AsTask().AwaiterResult();
        }
        public static void UnpublishInstance(this IRpcServicePublisher publisher, RpcObjectId serviceInstanceId)
        {
            if (publisher is null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }

            publisher.UnpublishInstanceAsync(serviceInstanceId).AsTask().AwaiterResult();
        }
Beispiel #8
0
 internal NetGrpcServer(
     IRpcServicePublisher servicePublisher,
     IRpcServiceActivator serviceImplProvider,
     IRpcServiceDefinitionsProvider serviceDefinitionsProvider,
     //ServiceMethodProviderContext<NetGrpcServiceActivator>? context,
     RpcServerOptions?options,
     ILoggerFactory?loggerFactory)
     : base(servicePublisher, serviceImplProvider, serviceDefinitionsProvider, options, loggerFactory)
 {
 }
        /// <summary>
        /// Publishes an RPC service instance using an instance factory.
        /// </summary>
        /// <typeparam name="TService">The type of the published instance.</typeparam>
        /// <param name="factory">A factory function that should create the service instance specified by the <see cref="RpcObjectId"/>. If the created
        /// instance implements <see cref="IDisposable"/> the instance will be disposed when the RPC call has finished.
        /// </param>
        /// <returns>A scoped object including the <see cref="RpcObjectRef"/> identifying the published instance. The scoped object will unpublish
        /// the service instance when disposed.</returns>
        public static IOwned <RpcObjectRef <TService> > PublishInstance <TService>(this IRpcServicePublisher servicePublisher, Func <RpcObjectId, TService> factory)
            where TService : class
        {
            if (servicePublisher is null)
            {
                throw new ArgumentNullException(nameof(servicePublisher));
            }

            IOwned <TService> CreateActivatedService(IServiceProvider?services, RpcObjectId objectId)
            {
                return(OwnedObject.Create(factory(objectId)));
            }

            return(servicePublisher.PublishInstance(CreateActivatedService));
        }
        /// <summary>
        /// Internal to allow testing.
        /// </summary>
        internal LightweightRpcServer(
            IRpcServicePublisher servicePublisher,
            IRpcServiceActivator serviceImplProvider,
            IRpcServiceDefinitionsProvider definitionsProvider,
            IServiceProvider?serviceProvider,
            IRpcServerOptions?options,
            LightweightOptions?lightweightOptions = null,
            ILoggerFactory?loggerFactory          = null)
            : base(servicePublisher, serviceImplProvider, definitionsProvider,
                   options,
                   loggerFactory)
        {
            this.ServiceProvider = serviceProvider;
            this.MaxRequestSize  = options?.ReceiveMaxMessageSize ?? DefaultMaxRequestMessageSize;
            this.MaxResponseSize = options?.SendMaxMessageSize ?? DefaultMaxResponseMessageSize;

            this.KeepSizeLimitedConnectionAlive = lightweightOptions?.KeepSizeLimitedConnectionAlive ?? true;
        }
        /// <summary>
        /// Publishes an RPC service instance with the help of a service provider factory.
        /// </summary>
        /// <typeparam name="TService">The type of the published instance.</typeparam>
        /// <param name="factory">A factory function that should create the service instance specified by the <see cref="RpcObjectId"/>
        /// with the help of the provided <see cref="IServiceProvider"/>.</param>
        /// <returns>A scoped object including the <see cref="RpcObjectRef"/> identifying the published instance. The scoped object will unpublish
        /// the service instance when disposed.</returns>
        public static IOwned <RpcObjectRef <TService> > PublishInstance <TService>(this IRpcServicePublisher servicePublisher, Func <IServiceProvider, RpcObjectId, TService> factory)
            where TService : class
        {
            if (servicePublisher is null)
            {
                throw new ArgumentNullException(nameof(servicePublisher));
            }

            IOwned <TService> CreateActivatedService(IServiceProvider?services, RpcObjectId objectId)
            {
                if (services == null)
                {
                    throw new RpcDefinitionException("An IServiceProvider must be supplied when services are published using IServiceProvider factories.");
                }

                return(OwnedObject.CreateUnowned(factory(services, objectId)));
            }

            return(servicePublisher.PublishInstance(CreateActivatedService));
        }
 /// <summary>
 /// Publishes an RPC singleton under the service name of the <typeparamref name="TService"/> RPC interface.
 /// The service instance will be created using the <see cref="IServiceProvider"/> associated with the RPC call.
 /// </summary>
 /// <typeparam name="TService">The interface of the service type. Must be an interface type with the <see cref="RpcServiceAttribute"/> (or <c>ServiceContractAttribute</c>)
 /// applied.</typeparam>
 /// <returns>A scoped object including the <see cref="RpcSingletonRef{TService}"/> identifying the published singleton. The scoped object will unpublish
 /// the service singleton when disposed.</returns>
 public static IOwned <RpcSingletonRef <TService> > PublishSingleton <TService>(this IRpcServicePublisher publisher)
     where TService : class
 => PublishSingleton <TService, TService>(publisher);
        /// <summary>
        /// Publishes an RPC singleton under the service name of the <typeparamref name="TService"/> RPC interface,
        /// using <typeparamref name="TServiceImpl"/> as the implementation.
        /// The service instance will be created using the <see cref="IServiceProvider"/> associated with the RPC call.
        /// </summary>
        /// <typeparam name="TService">The interface of the service type. Must be an interface type with the <see cref="RpcServiceAttribute"/> (or <c>ServiceContractAttribute</c>)
        /// applied.</typeparam>
        /// <typeparam name="TServiceImpl">The type that implements the service interface.</typeparam>
        /// <returns>A scoped object including the <see cref="RpcSingletonRef{TService}"/> identifying the published singleton. The scoped object will unpublish
        /// the service singleton when disposed.</returns>
        public static IOwned <RpcSingletonRef <TService> > PublishSingleton <TService, TServiceImpl>(this IRpcServicePublisher publisher)
            where TServiceImpl : class, TService
            where TService : class
        {
            if (publisher is null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }

            return(publisher.PublishSingleton(ServiceActivator <TService, TServiceImpl> .CreateActivatedService));
        }
        public static IList <RpcObjectRef <TService>?> GetPublishedServiceInstances <TService>(this IRpcServicePublisher servicePublisher,
                                                                                               IReadOnlyList <TService> serviceInstances, bool allowUnpublished) where TService : class
        {
            if (servicePublisher is null)
            {
                throw new ArgumentNullException(nameof(servicePublisher));
            }

            if (serviceInstances == null)
            {
                return(null !);
            }

            var publishedServices = new List <RpcObjectRef <TService>?>();

            foreach (var s in serviceInstances)
            {
                if (s != null)
                {
                    var publishedService = s != null?servicePublisher.GetPublishedInstance(s) : null;

                    if (publishedService != null)
                    {
                        publishedServices.Add(publishedService);
                    }
                    else if (!allowUnpublished)
                    {
                        throw new InvalidOperationException("Service has not been published.");
                    }
                }
                else
                {
                    publishedServices.Add(null);
                }
            }

            return(publishedServices);
        }
        public static IOwned <RpcSingletonRef <TService> > PublishSingleton <TService>(this IRpcServicePublisher servicePublisher, Func <TService> factory)
            where TService : class
        {
            if (servicePublisher is null)
            {
                throw new ArgumentNullException(nameof(servicePublisher));
            }

            IOwned <TService> CreateActivatedService(IServiceProvider?_) => OwnedObject.Create(factory());

            return(servicePublisher.PublishSingleton(CreateActivatedService));
        }
 public ServiceProviderServiceImpl(IRpcServicePublisher publisher)
 {
     this.simpleServiceScope = publisher.PublishInstance <ISimpleService>(new TestSimpleServiceImpl());
 }