Пример #1
0
        public void ChangeScale(int newScaleSize)
        {
            if (newScaleSize <= 0)
            {
                throw new ArgumentOutOfRangeException("newScaleSize", "Must be Greater than Zero");
            }

            ScaleSize = newScaleSize;

            if (!running)
            {
                return;
            }

            lock (lockWorkers)
            {
                // Scale down
                while (workers.Count > ScaleSize)
                {
                    workers[0].Cancel();
                    workers.RemoveAt(0);
                }

                // Scale up
                while (workers.Count < ScaleSize)
                {
                    var worker = new ServiceWorker <TNotification>(this, ServiceConnectionFactory.Create());
                    workers.Add(worker);
                    worker.Start();
                }

                Log.Debug("Scaled Changed to: " + workers.Count);
            }
        }
Пример #2
0
        private void RegisterSecurityServices(string serviceBaseUrl)
        {
            var factory = new ServiceConnectionFactory(serviceBaseUrl);

            _container.RegisterInstance <IServiceConnectionFactory>(factory, new ContainerControlledLifetimeManager());

            _container.RegisterService <ISecurityService>(
                factory.GetConnectionString(SecurityConfiguration.Instance.Connection.ServiceUri),
                SecurityConfiguration.Instance.Connection.WSEndPointName);

            _container.RegisterService <IAuthenticationService>(
                factory.GetConnectionString(SecurityConfiguration.Instance.Authentication.ServiceUri),
                SecurityConfiguration.Instance.Authentication.WSEndPointName);

            var service = _container.Resolve <IAuthenticationService>();

            _container.RegisterInstance <IAuthenticationContext>(new AuthenticationContext(service), new ContainerControlledLifetimeManager());
        }
Пример #3
0
        public async Task<IServiceHubContext> CreateHubContextAsync(string hubName, ILoggerFactory loggerFactory = null, CancellationToken cancellationToken = default)
        {
            loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
            switch (_serviceManagerOptions.ServiceTransportType)
            {
                case ServiceTransportType.Persistent:
                    {
                        var connectionFactory = new ManagementConnectionFactory(_productInfo, new ConnectionFactory(_serverNameProvider, loggerFactory));
                        var serviceProtocol = new ServiceProtocol();
                        var clientConnectionManager = new ClientConnectionManager();
                        var clientConnectionFactory = new ClientConnectionFactory();
                        ConnectionDelegate connectionDelegate = connectionContext => Task.CompletedTask;
                        var serviceConnectionFactory = new ServiceConnectionFactory(serviceProtocol, clientConnectionManager, connectionFactory, loggerFactory, connectionDelegate, clientConnectionFactory);
                        var weakConnectionContainer = new WeakServiceConnectionContainer(
                            serviceConnectionFactory,
                            _serviceManagerOptions.ConnectionCount,
                            new HubServiceEndpoint(hubName, _endpointProvider, _endpoint),
                            loggerFactory?.CreateLogger(nameof(WeakServiceConnectionContainer)) ?? NullLogger.Instance);

                        var serviceCollection = new ServiceCollection();
                        serviceCollection.AddSignalRCore();
                        serviceCollection.AddSingleton<IConfigureOptions<HubOptions>, ManagementHubOptionsSetup>();

                        if (loggerFactory != null)
                        {
                            serviceCollection.AddSingleton(typeof(ILoggerFactory), loggerFactory);
                        }

                        serviceCollection
                            .AddLogging()
                            .AddSingleton(typeof(IConnectionFactory), sp => connectionFactory)
                            .AddSingleton(typeof(HubLifetimeManager<>), typeof(WebSocketsHubLifetimeManager<>))
                            .AddSingleton(typeof(IServiceConnectionManager<>), typeof(ServiceConnectionManager<>))
                            .AddSingleton(typeof(IServiceConnectionContainer), sp => weakConnectionContainer);

                        var success = false;
                        ServiceProvider serviceProvider = null;
                        try
                        {
                            serviceProvider = serviceCollection.BuildServiceProvider();

                            var serviceConnectionManager = serviceProvider.GetRequiredService<IServiceConnectionManager<Hub>>();
                            serviceConnectionManager.SetServiceConnection(weakConnectionContainer);
                            _ = serviceConnectionManager.StartAsync();

                            // wait until service connection established
                            await weakConnectionContainer.ConnectionInitializedTask.OrTimeout(cancellationToken);

                            var webSocketsHubLifetimeManager = (WebSocketsHubLifetimeManager<Hub>)serviceProvider.GetRequiredService<HubLifetimeManager<Hub>>();

                            var hubContext = serviceProvider.GetRequiredService<IHubContext<Hub>>();
                            var serviceHubContext = new ServiceHubContext(hubContext, webSocketsHubLifetimeManager, serviceProvider);
                            success = true;
                            return serviceHubContext;
                        }
                        finally
                        {
                            if (!success)
                            {
                                serviceProvider?.Dispose();
                            }
                        }
                    }
                case ServiceTransportType.Transient:
                    {
                        var serviceCollection = new ServiceCollection();
                        serviceCollection.AddSignalRCore();

                        // remove default hub lifetime manager
                        var serviceDescriptor = serviceCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(HubLifetimeManager<>));
                        serviceCollection.Remove(serviceDescriptor);

                        // add rest hub lifetime manager
                        var restHubLifetimeManager = new RestHubLifetimeManager(_serviceManagerOptions, hubName, _productInfo);
                        serviceCollection.AddSingleton(typeof(HubLifetimeManager<Hub>), sp => restHubLifetimeManager);

                        var serviceProvider = serviceCollection.BuildServiceProvider();
                        var hubContext = serviceProvider.GetRequiredService<IHubContext<Hub>>();
                        return new ServiceHubContext(hubContext, restHubLifetimeManager, serviceProvider);
                    }
                default:
                    throw new ArgumentException("Not supported service transport type.");
            }
        }