IApplicationBuilder 不包含定义 MapSignalR()。 app.MapSignalR() 无法在 ASP.NET 核心上运行

IApplicaionBuilder dos not contain a definition MapSignalR(). app.MapSignalR() not workin on ASP.NET CORE

我想将 SignalR 合并到我的项目中,但我无法在启动 class 中添加行 app.MapSignalR() 时出现错误。这是启动 class:

 public class Startup
  {
    public Startup(IConfiguration configuration{...}

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {...}

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


        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }


        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.MapSignalR(); // ERROR - IApplicaionBuilder dos not contain a definition MapSignalR() and the best extension method overload ' OwinExtensios.MapSignalR(IAppBuilder)' requires a receiver of type 'IAppBuilder'


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

我已经添加了 'using Owin;' 但它仍然不起作用。我该怎么办?

这样使用。这是 .net 核心 3.0+

public void ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<ChatHub>("/chatHub");
            });
        }

阅读更多:https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.1&tabs=visual-studio

如果.net core 2.2

使用下面的
app.UseSignalR(routes =>
{
  routes.MapHub<ChatHub>("/chatHub");
});

而不是

app.UseEndpoints(endpoints =>
{
   endpoints.MapHub<ChatHub>("/chatHub");
});