3

I want to incorporate SignalR on my project but I can't I have an error when I add the line app.MapSignalR() in the Startup class. Here is the StartUp 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?}");
        });
    }
}

I already added 'using Owin;' and it still doesn't work. What should I do?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Teresa Alves
  • 483
  • 1
  • 7
  • 16
  • You might have installed the wrong nuget package – Beingnin Jan 02 '20 at 16:36
  • I installed the OWIN IAppBuilder startup interface. Isn't that one? – Teresa Alves Jan 02 '20 at 16:42
  • You need to install signalr too – Beingnin Jan 02 '20 at 16:47
  • Microsoft.AspNetCore.SignalR 1.1.0 try this nuget package – Beingnin Jan 02 '20 at 16:47
  • already installed – Teresa Alves Jan 02 '20 at 16:52
  • `app.MapSignalR();` is for [ASP.NET SignalR application](https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#how-to-register-signalr-middleware). To configure and add SignalR functionality to your ASP.NET Core 2.2 app (or 2.1), please check [this document "Configure SignalR hubs"](https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-2.2#configure-signalr-hubs). – Fei Han Jan 03 '20 at 02:26

1 Answers1

2

Use like this. This is for .net core 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");
            });
        }

Read more : https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.1&tabs=visual-studio

Use below if .net core 2.2

app.UseSignalR(routes =>
{
  routes.MapHub<ChatHub>("/chatHub");
});

instead of

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