示例#1
0
        public AngularCliMiddleware(
            IApplicationBuilder appBuilder,
            string sourcePath,
            string npmScriptName)
        {
            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentException("Cannot be null or empty", nameof(sourcePath));
            }

            if (string.IsNullOrEmpty(npmScriptName))
            {
                throw new ArgumentException("Cannot be null or empty", nameof(npmScriptName));
            }

            _sourcePath = sourcePath;
            _logger     = GetOrCreateLogger(appBuilder);

            // Start Angular CLI and attach to middleware pipeline
            var angularCliServerInfoTask = StartAngularCliServerAsync(npmScriptName);

            // Everything we proxy is hardcoded to target http://localhost because:
            // - the requests are always from the local machine (we're not accepting remote
            //   requests that go directly to the Angular CLI middleware server)
            // - given that, there's no reason to use https, and we couldn't even if we
            //   wanted to, because in general the Angular CLI server has no certificate
            var proxyOptionsTask = angularCliServerInfoTask.ContinueWith(
                task => new ConditionalProxyMiddlewareTarget(
                    "http", "localhost", task.Result.Port.ToString()));

            var applicationStoppingToken = GetStoppingToken(appBuilder);

            // Proxy all requests into the Angular CLI server
            appBuilder.Use(async(context, next) =>
            {
                try
                {
                    var didProxyRequest = await ConditionalProxy.PerformProxyRequest(
                        context, _neverTimeOutHttpClient, proxyOptionsTask, applicationStoppingToken);

                    // Since we are proxying everything, this is the end of the middleware pipeline.
                    // We won't call next().
                    if (!didProxyRequest)
                    {
                        context.Response.StatusCode = 404;
                    }
                }
                catch (AggregateException)
                {
                    ThrowIfTaskCancelled(angularCliServerInfoTask);
                    throw;
                }
                catch (TaskCanceledException)
                {
                    ThrowIfTaskCancelled(angularCliServerInfoTask);
                    throw;
                }
            });
        }
        public AngularCliMiddleware(
            IApplicationBuilder appBuilder,
            string sourcePath,
            SpaDefaultPageMiddleware defaultPageMiddleware)
        {
            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentException("Cannot be null or empty", nameof(sourcePath));
            }

            // Prepare to make calls into Node
            _nodeServices         = CreateNodeServicesInstance(appBuilder, sourcePath);
            _middlewareScriptPath = GetAngularCliMiddlewareScriptPath(appBuilder);

            // Start Angular CLI and attach to middleware pipeline
            var angularCliServerInfoTask = StartAngularCliServerAsync();

            // Everything we proxy is hardcoded to target http://localhost because:
            // - the requests are always from the local machine (we're not accepting remote
            //   requests that go directly to the Angular CLI middleware server)
            // - given that, there's no reason to use https, and we couldn't even if we
            //   wanted to, because in general the Angular CLI server has no certificate
            var proxyOptionsTask = angularCliServerInfoTask.ContinueWith(
                task => new ConditionalProxyMiddlewareTarget(
                    "http", "localhost", task.Result.Port.ToString()));

            var applicationStoppingToken = GetStoppingToken(appBuilder);

            // Proxy all requests into the Angular CLI server
            appBuilder.Use(async(context, next) =>
            {
                var didProxyRequest = await ConditionalProxy.PerformProxyRequest(
                    context, _neverTimeOutHttpClient, proxyOptionsTask, applicationStoppingToken);

                // Since we are proxying everything, this is the end of the middleware pipeline.
                // We won't call next().
                if (!didProxyRequest)
                {
                    context.Response.StatusCode = 404;
                }
            });

            // Advertise the availability of this feature to other SPA middleware
            appBuilder.Properties.Add(AngularCliMiddlewareKey, this);
        }