/// <summary>
    /// Configures the application to forward incoming requests to a local Single Page
    /// Application (SPA) development server. This is only intended to be used during
    /// development. Do not enable this middleware in production applications.
    /// </summary>
    /// <param name="spaBuilder">The <see cref="ISpaBuilder"/>.</param>
    /// <param name="baseUriTaskFactory">A callback that will be invoked on each request to supply a <see cref="Task"/> that resolves with the target base URI to which requests should be proxied.</param>
    public static void UseProxyToSpaDevelopmentServer(
        this ISpaBuilder spaBuilder,
        Func <Task <Uri> > baseUriTaskFactory)
    {
        var applicationBuilder       = spaBuilder.ApplicationBuilder;
        var applicationStoppingToken = GetStoppingToken(applicationBuilder);

        // Since we might want to proxy WebSockets requests (e.g., by default, AngularCliMiddleware
        // requires it), enable it for the app
        applicationBuilder.UseWebSockets();

        // It's important not to time out the requests, as some of them might be to
        // server-sent event endpoints or similar, where it's expected that the response
        // takes an unlimited time and never actually completes
        var neverTimeOutHttpClient =
            SpaProxy.CreateHttpClientForProxy(Timeout.InfiniteTimeSpan);

        // Proxy all requests to the SPA development server
        applicationBuilder.Run(async(context) =>
        {
            var didProxyRequest = await SpaProxy.PerformProxyRequest(
                context, neverTimeOutHttpClient, baseUriTaskFactory(), applicationStoppingToken,
                proxy404s: true);
        });
    }
Example #2
0
    private async Task InvokeCore(HttpContext context)
    {
        var didProxyRequest = await SpaProxy.PerformProxyRequest(
            context, _httpClient, _baseUriTask, _applicationStoppingToken, proxy404s : false);

        if (didProxyRequest)
        {
            return;
        }

        // Not a request we can proxy
        await _next.Invoke(context);
    }
        /// <summary>
        /// Configures the application to forward incoming requests to a local Single Page
        /// Application (SPA) development server. This is only intended to be used during
        /// development. Do not enable this middleware in production applications.
        /// </summary>
        /// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="baseUriTask">A <see cref="Task"/> that resolves with the target base URI to which requests should be proxied.</param>
        public static void UseProxyToSpaDevelopmentServer(
            this IApplicationBuilder applicationBuilder,
            Task <Uri> baseUriTask)
        {
            var applicationStoppingToken = GetStoppingToken(applicationBuilder);

            // It's important not to time out the requests, as some of them might be to
            // server-sent event endpoints or similar, where it's expected that the response
            // takes an unlimited time and never actually completes
            var neverTimeOutHttpClient =
                SpaProxy.CreateHttpClientForProxy(Timeout.InfiniteTimeSpan);

            // Proxy all requests into the Angular CLI server
            applicationBuilder.Use(async(context, next) =>
            {
                var didProxyRequest = await SpaProxy.PerformProxyRequest(
                    context, neverTimeOutHttpClient, baseUriTask, applicationStoppingToken,
                    proxy404s: true);
            });
        }
        public static void UseProxyToSpaDevelopmentServer(
            this ISeedSpaBuilder spaBuilder,
            Func <Task <Uri> > baseUriTaskFactory)
        {
            var applicationBuilder       = spaBuilder.ApplicationBuilder;
            var applicationStoppingToken = GetStoppingToken(applicationBuilder);
            var assemblyName             = spaBuilder.Assembly.GetName().Name;

            if (!SeedSpaBuilderExtensions.UrlHash.ContainsKey(assemblyName))
            {
                SeedSpaBuilderExtensions.UrlHash.Add(assemblyName, baseUriTaskFactory);
            }

            applicationBuilder.UseWebSockets();

            var neverTimeOutHttpClient =
                SpaProxy.CreateHttpClientForProxy(Timeout.InfiniteTimeSpan);

            applicationBuilder.Use(async(context, next) =>
            {
                if (context.GetEndpoint() == null &&
                    context.Request.Path.Value.StartsWith($"/{assemblyName}"))
                {
                    await SpaProxy.PerformProxyRequest(
                        context,
                        neverTimeOutHttpClient,
                        baseUriTaskFactory(),
                        applicationStoppingToken,
                        proxy404s: true);

                    return;
                }

                await next();
            });
        }