예제 #1
0
        /// <summary>
        /// Internal to support testing. Initializes <see cref="IHostedService"/> instances in the service,
        /// and registers them for their preferred service type
        /// </summary>
        internal static void InitializeHostedServices(RegisteredServiceProvider provider, IProtocolEndpoint host)
        {
            // Pre-register all services before initializing. This ensures that if one service wishes to reference
            // another one during initialization, it will be able to safely do so
            foreach (IHostedService service in provider.GetServices <IHostedService>())
            {
                provider.RegisterSingleService(service.ServiceType, service);
            }

            ServiceHost serviceHost = host as ServiceHost;

            foreach (IHostedService service in provider.GetServices <IHostedService>())
            {
                // Initialize all hosted services, and register them in the service provider for their requested
                // service type. This ensures that when searching for the ConnectionService you can get it without
                // searching for an IHostedService of type ConnectionService
                service.InitializeService(host);

                IDisposable disposable = service as IDisposable;
                if (serviceHost != null && disposable != null)
                {
                    serviceHost.RegisterShutdownTask(async(shutdownParams, shutdownRequestContext) =>
                    {
                        disposable.Dispose();
                        await Task.FromResult(0);
                    });
                }
            }
        }
        public void GetServicesShouldReturnEmptyIfNoServicesRegistered()
        {
            // Given no service regisstered
            // When I call GetService
            var services = provider.GetServices <MyProviderService>();

            // Then I expect empty enumerable to be returned
            Assert.NotNull(services);
            Assert.Equal(0, services.Count());
        }
예제 #3
0
        /// <summary>
        /// Internal to support testing. Initializes <see cref="IHostedService"/> instances in the service,
        /// and registers them for their preferred service type
        /// </summary>
        internal static void InitializeHostedServices(RegisteredServiceProvider provider, IProtocolEndpoint host)
        {
            // Pre-register all services before initializing. This ensures that if one service wishes to reference
            // another one during initialization, it will be able to safely do so
            foreach (IHostedService service in provider.GetServices <IHostedService>())
            {
                provider.RegisterSingleService(service.ServiceType, service);
            }

            foreach (IHostedService service in provider.GetServices <IHostedService>())
            {
                // Initialize all hosted services, and register them in the service provider for their requested
                // service type. This ensures that when searching for the ConnectionService you can get it without
                // searching for an IHostedService of type ConnectionService
                service.InitializeService(host);
            }
        }
        public ExtensibleServiceHost(RegisteredServiceProvider provider, ChannelBase protocolChannel)
            : base(protocolChannel)
        {
            Validate.IsNotNull(nameof(provider), provider);

            provider.RegisterSingleService <IServiceHost>(this);
            provider.RegisterHostedServices();

            // Initialize all hosted services
            foreach (IHostedService service in provider.GetServices <IHostedService>())
            {
                service.InitializeService(this);
            }

            serviceProvider = provider;
        }