internal static INodeJSService INodeJSServiceFactory(IServiceProvider serviceProvider)
        {
            OutOfProcessNodeJSServiceOptions outOfProcessNodeJSServiceOptions = serviceProvider.GetRequiredService <IOptions <OutOfProcessNodeJSServiceOptions> >().Value;
            IEnvironmentService environmentService = serviceProvider.GetRequiredService <IEnvironmentService>();

            int concurrencyDegree = outOfProcessNodeJSServiceOptions.ConcurrencyDegree;
            int processorCount    = environmentService.ProcessorCount; // TODO to be safe we should ensure that this is >= 1

            if (outOfProcessNodeJSServiceOptions.Concurrency == Concurrency.None ||
                concurrencyDegree == 1 ||                      // MultiProcess mode but only 1 process
                concurrencyDegree <= 0 && processorCount == 1) // Machine has only 1 logical processor
            {
                return(ActivatorUtilities.CreateInstance <HttpNodeJSService>(serviceProvider));
            }
            else
            {
                if (concurrencyDegree <= 0)
                {
                    concurrencyDegree = processorCount;
                }

                var httpNodeJSServices = new HttpNodeJSService[concurrencyDegree];
                for (int i = 0; i < concurrencyDegree; i++)
                {
                    httpNodeJSServices[i] = ActivatorUtilities.CreateInstance <HttpNodeJSService>(serviceProvider);
                }

                return(new HttpNodeJSPoolService(new ReadOnlyCollection <HttpNodeJSService>(httpNodeJSServices)));
            }
        }
Esempio n. 2
0
        internal static IHttpClientService IHttpClientServiceFactory(IServiceProvider serviceProvider)
        {
            OutOfProcessNodeJSServiceOptions outOfProcessNodeJSServiceOptions = serviceProvider.GetRequiredService <IOptions <OutOfProcessNodeJSServiceOptions> >().Value;

            return(new HttpClientService
            {
                Timeout = outOfProcessNodeJSServiceOptions.TimeoutMS == -1 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(outOfProcessNodeJSServiceOptions.TimeoutMS + 1000)
            });
        }
        internal static IHttpClientService IHttpClientServiceFactory(IServiceProvider serviceProvider)
        {
            OutOfProcessNodeJSServiceOptions outOfProcessNodeJSServiceOptions = serviceProvider.GetRequiredService <IOptions <OutOfProcessNodeJSServiceOptions> >().Value;

            // TODO consider making PooledConnectionIdleTimeout infinite - https://www.stevejgordon.co.uk/httpclient-connection-pooling-in-dotnet-core
            return(new HttpClientService
            {
                Timeout = outOfProcessNodeJSServiceOptions.TimeoutMS == -1 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(outOfProcessNodeJSServiceOptions.TimeoutMS + 1000)
            });
        }
Esempio n. 4
0
 /// <summary>
 /// Creates an<see cref="OutOfProcessNodeJSService"/> instance.
 /// </summary>
 /// <param name="nodeProcessFactory"></param>
 /// <param name="logger"></param>
 /// <param name="optionsAccessor"></param>
 /// <param name="embeddedResourcesService"></param>
 /// <param name="serverScriptAssembly"></param>
 /// <param name="serverScriptName"></param>
 protected OutOfProcessNodeJSService(INodeJSProcessFactory nodeProcessFactory,
                                     ILogger logger,
                                     IOptions <OutOfProcessNodeJSServiceOptions> optionsAccessor,
                                     IEmbeddedResourcesService embeddedResourcesService,
                                     Assembly serverScriptAssembly,
                                     string serverScriptName)
 {
     _nodeProcessFactory = nodeProcessFactory;
     Logger   = logger;
     _options = optionsAccessor?.Value ?? new OutOfProcessNodeJSServiceOptions();
     _embeddedResourcesService = embeddedResourcesService;
     _serverScriptName         = serverScriptName;
     _serverScriptAssembly     = serverScriptAssembly;
 }
        /// <summary>
        /// Creates an<see cref="OutOfProcessNodeJSService"/> instance.
        /// </summary>
        /// <param name="nodeProcessFactory"></param>
        /// <param name="logger"></param>
        /// <param name="optionsAccessor"></param>
        /// <param name="embeddedResourcesService"></param>
        /// <param name="serverScriptAssembly"></param>
        /// <param name="serverScriptName"></param>
        protected OutOfProcessNodeJSService(INodeJSProcessFactory nodeProcessFactory,
                                            ILogger logger,
                                            IOptions <OutOfProcessNodeJSServiceOptions> optionsAccessor,
                                            IEmbeddedResourcesService embeddedResourcesService,
                                            Assembly serverScriptAssembly,
                                            string serverScriptName)
        {
            _nodeProcessFactory       = nodeProcessFactory;
            _options                  = optionsAccessor?.Value ?? new OutOfProcessNodeJSServiceOptions();
            _embeddedResourcesService = embeddedResourcesService;
            _serverScriptName         = serverScriptName;
            _serverScriptAssembly     = serverScriptAssembly;
            Logger = logger;

            _debugLoggingEnabled   = Logger.IsEnabled(LogLevel.Debug);
            _warningLoggingEnabled = Logger.IsEnabled(LogLevel.Warning);
        }
        // DO NOT DELETE - keep for backward compatibility.
        /// <summary>
        /// <para>Creates an <see cref="OutOfProcessNodeJSService"/> instance.</para>
        /// <para>If this constructor is used, file watching is disabled.</para>
        /// </summary>
        /// <param name="nodeProcessFactory"></param>
        /// <param name="logger"></param>
        /// <param name="optionsAccessor"></param>
        /// <param name="embeddedResourcesService"></param>
        /// <param name="serverScriptAssembly"></param>
        /// <param name="serverScriptName"></param>
        protected OutOfProcessNodeJSService(INodeJSProcessFactory nodeProcessFactory,
                                            ILogger logger,
                                            IOptions <OutOfProcessNodeJSServiceOptions> optionsAccessor,
                                            IEmbeddedResourcesService embeddedResourcesService,
                                            Assembly serverScriptAssembly,
                                            string serverScriptName)
        {
            _nodeProcessFactory       = nodeProcessFactory;
            _options                  = optionsAccessor?.Value ?? new OutOfProcessNodeJSServiceOptions();
            _embeddedResourcesService = embeddedResourcesService;
            _serverScriptName         = serverScriptName;
            _serverScriptAssembly     = serverScriptAssembly;
            Logger = logger;

            _debugLoggingEnabled   = Logger.IsEnabled(LogLevel.Debug);
            _warningLoggingEnabled = Logger.IsEnabled(LogLevel.Warning);
            _infoLoggingEnabled    = Logger.IsEnabled(LogLevel.Information);

            _numRetries           = _options.NumRetries;
            _numProcessRetries    = _options.NumProcessRetries;
            _numConnectionRetries = _options.NumConnectionRetries;
            _timeoutMS            = _options.TimeoutMS;
        }
        /// <summary>
        /// Adds NodeJS services to the an <see cref="IServiceCollection"/>.
        /// </summary>
        /// <param name="services">The target <see cref="IServiceCollection"/>.</param>
        public static void AddNodeJS(this IServiceCollection services)
        {
            // Third party services
            services.AddLogging();
            services.AddOptions();
            services.TryAddSingleton(typeof(IHttpClientService), serviceProvider =>
            {
                OutOfProcessNodeJSServiceOptions outOfProcessNodeJSServiceOptions = serviceProvider.GetRequiredService <IOptions <OutOfProcessNodeJSServiceOptions> >().Value;

                return(new HttpClientService
                {
                    Timeout = outOfProcessNodeJSServiceOptions.TimeoutMS == -1 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(outOfProcessNodeJSServiceOptions.TimeoutMS + 1000)
                });
            });

            // Services defined in this project
            services.AddSingleton <IConfigureOptions <NodeJSProcessOptions>, ConfigureNodeJSProcessOptions>();
            services.AddSingleton <IHttpContentFactory, InvocationContentFactory>();
            services.AddSingleton <IJsonService, JsonService>();
            services.AddSingleton <IEmbeddedResourcesService, EmbeddedResourcesService>();
            services.AddSingleton <INodeJSProcessFactory, NodeJSProcessFactory>();
            services.AddSingleton <INodeJSService, HttpNodeJSService>();
        }