首页 > 解决方案 > .NET Core 2.2 + Angular 7“xhr_streaming”永无止境

问题描述

我正在设置一个 .NET Core Web API 后端,前端有一个 Angular 7 SPA。我正在使用 Microsoft 的 SpaServices 和 StaticFiles 包将两者链接到一个项目中。

当我运行 Web 应用程序时,控制台选项卡显示 sockjs WebSocket 警告,而网络选项卡显示来自 sockjs 的名为“xhr_streaming”的东西,它永远保持打开状态。

这种行为正常吗?还是我没有正确设置/配置某些东西?

除了使用 Rider 生成 Web API 和使用 Angular CLI 生成 Angular 7 应用程序之外,我没有对项目做任何事情。我似乎无法在网上找到与此问题相关的很多内容,但我可能没有正确搜索。

这是我在 Web API 中的启动。我只关注我为将其与 Angular 链接所做的配置。

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // Rest of ConfigureServices are unrelated

            services.AddSpaStaticFiles(configuration =>
            {
                // RootPath = "ClientApp/dist"
                configuration.RootPath = serviceProvider.GetService<IOptions<SpaConfig>>().Value.RootPath;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            // Rest of Configure is irrelevant

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    "default",
                    "{controller}/{action}/{id?}");
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer("start");
                }
            });
        }
    }

Angular 方面确实没有什么可以显示的,因为在使用ng new.

基本上我只是想知道这种行为是否是预期的,或者我是否做错了什么。

以下是警告和网络阅读的一些截图: 网络阅读

和警告: 警告

标签: angularwebsocket.net-coresingle-page-applicationsockjs

解决方案


当我调用从服务的构造函数返回承诺的函数时发生在我身上。函数调用包含一个 api 获取请求。一旦我删除了函数调用,xhr_streaming 就不再是一个永无止境的请求了。


推荐阅读