Ejemplo n.º 1
0
        public NodeInvocationService(
            INodeJSService nodeJsService,
            ReactConfiguration configuration,
            IEmbeddedResourcesService embeddedResourcesService,
            IOptions <NodeJSProcessOptions> nodeJsProcessOptions,
            IServiceScopeFactory serviceScopeFactory)
        {
            _nodeJsService            = nodeJsService;
            _embeddedResourcesService = embeddedResourcesService;

            using (IServiceScope scope = serviceScopeFactory.CreateScope())
            {
                IServiceProvider serviceProvider = scope.ServiceProvider;

                IHostingEnvironment hostingEnvironment = serviceProvider.GetService <IHostingEnvironment>();

                var requireFiles = configuration.ScriptFilesWithoutTransform
                                   .Select(relativePath =>
                {
                    if (relativePath.StartsWith(nodeJsProcessOptions.Value.ProjectPath))
                    {
                        return(relativePath);
                    }

                    relativePath = relativePath.TrimStart('~').TrimStart('/');

                    return(Path.GetFullPath(Path.Combine(hostingEnvironment?.WebRootPath ?? nodeJsProcessOptions.Value.ProjectPath, relativePath)));
                });

                //TODO: do this in configure
                nodeJsProcessOptions.Value.EnvironmentVariables.Add("NODEREACT_REQUIREFILES", string.Join(',', requireFiles));
            }
        }
        public static IServiceCollection AddNodeReact(this IServiceCollection services, Action <ReactConfiguration> configuration = null)
        {
            var config = new ReactConfiguration();

            configuration?.Invoke(config);

            services.AddSingleton(config);

            services.AddSingleton <IComponentNameInvalidator, ComponentNameInvalidator>();
            services.AddSingleton <IReactIdGenerator, ReactIdGenerator>();
            services.AddSingleton <INodeInvocationService, NodeInvocationService>();


            services.AddNodeJS();
            services.Configure <NodeJSProcessOptions>(options =>
            {
                config.ConfigureNodeJSProcessOptions?.Invoke(options);

                options.EnvironmentVariables.Add("NODEREACT_MINWORKERS", config.StartEngines.ToString());
                options.EnvironmentVariables.Add("NODEREACT_MAXWORKERS", config.MaxEngines.ToString());
                options.EnvironmentVariables.Add("NODEREACT_MAXUSAGESPERFWORKER", config.MaxUsagesPerEngine.ToString());
            });
            services.Configure <OutOfProcessNodeJSServiceOptions>(options => config.ConfigureOutOfProcessNodeJSServiceOptions?.Invoke(options));

            services.Replace(new ServiceDescriptor(
                                 typeof(IJsonService),
                                 typeof(CustomJsonService),
                                 ServiceLifetime.Singleton));

            services.AddScoped <IReactScopedContext, ReactScopedContext>();

            services.AddTransient <ReactComponent>();
            services.AddTransient <ReactRouterComponent>();

            return(services);
        }