コード例 #1
0
        /// <summary>
        /// Registers a ServiceModel.Grpc service (one instance for all calls) in the <see cref="Server.ServiceDefinitionCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The implementation type of ServiceModel.Grpc service.</typeparam>
        /// <param name="services">The <see cref="Server.ServiceDefinitionCollection"/>.</param>
        /// <param name="service">The service instance.</param>
        /// <param name="configure">The optional configuration action to provide a configuration the service.</param>
        /// <returns><see cref="Server.ServiceDefinitionCollection"/>.</returns>
        public static Server.ServiceDefinitionCollection AddServiceModelSingleton <TService>(
            this Server.ServiceDefinitionCollection services,
            TService service,
            Action <ServiceModelGrpcServiceOptions>?configure = default)
        {
            services.AssertNotNull(nameof(services));

            BindService(services, () => service, null, configure);
            return(services);
        }
コード例 #2
0
        /// <summary>
        /// Registers a ServiceModel.Grpc service (one instance per-call) in the <see cref="Server.ServiceDefinitionCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The implementation type of ServiceModel.Grpc service.</typeparam>
        /// <param name="services">The <see cref="Server.ServiceDefinitionCollection"/>.</param>
        /// <param name="serviceFactory">Method which creates a service instance.</param>
        /// <param name="configure">The optional configuration action to provide a configuration the service.</param>
        /// <returns><see cref="Server.ServiceDefinitionCollection"/>.</returns>
        public static Server.ServiceDefinitionCollection AddServiceModelTransient <TService>(
            this Server.ServiceDefinitionCollection services,
            Func <TService> serviceFactory,
            Action <ServiceModelGrpcServiceOptions>?configure = default)
        {
            services.AssertNotNull(nameof(services));
            serviceFactory.AssertNotNull(nameof(serviceFactory));

            BindService(services, serviceFactory, null, configure);
            return(services);
        }
コード例 #3
0
        /// <summary>
        /// Registers a ServiceModel.Grpc service in the <see cref="Server.ServiceDefinitionCollection"/>.
        /// </summary>
        /// <typeparam name="TService">The implementation type of ServiceModel.Grpc service.</typeparam>
        /// <param name="services">The <see cref="Server.ServiceDefinitionCollection"/>.</param>
        /// <param name="serviceProvider">See <see cref="IServiceProvider"/>.</param>
        /// <param name="configure">The optional configuration action to provide a configuration the service.</param>
        /// <returns><see cref="Server.ServiceDefinitionCollection"/>.</returns>
        public static Server.ServiceDefinitionCollection AddServiceModel <TService>(
            this Server.ServiceDefinitionCollection services,
            IServiceProvider serviceProvider,
            Action <ServiceModelGrpcServiceOptions>?configure = default)
        {
            services.AssertNotNull(nameof(services));
            serviceProvider.AssertNotNull(nameof(serviceProvider));

            Func <TService> serviceFactory = serviceProvider.GetServiceRequired <TService>;
            var             options        = new ServiceModelGrpcServiceOptions
            {
                ServiceProvider = serviceProvider
            };

            BindService(services, serviceFactory, null, configure, options);
            return(services);
        }
コード例 #4
0
        private static void BindService <TService>(
            Server.ServiceDefinitionCollection services,
            Func <TService> serviceFactory,
            IServiceEndpointBinder <TService>?endpointBinder,
            Action <ServiceModelGrpcServiceOptions>?configure)
        {
            ValidateServiceType(typeof(TService));

            ServiceModelGrpcServiceOptions?options = null;

            if (configure != null)
            {
                options = new ServiceModelGrpcServiceOptions();
                configure(options);
            }

            var definition = new ServerServiceDefinitionProvider <TService>(serviceFactory, endpointBinder, options).CreateDefinition();

            services.Add(definition);
        }