SignalR 将客户端连接到集线器显示 404 错误

SignalR connect Client to Hub showing 404 error

我正在尝试从我的 ASP.NET 核心应用程序向客户端发送消息,该客户端是一个 c# 控制台应用程序。当我尝试将控制台应用程序连接到集线器时,我总是收到 404 未找到异常。任何人都可以帮我存档吗?

这是我目前拥有的:

服务器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using CoreSignalRServer.Hubs;

namespace CoreSignalRServer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = 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)
        {
            services.AddSignalR();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();

            // register middleware for SignalR
            app.UseSignalR(routes =>
            {
                // the url most start with lower letter
                routes.MapHub<TestHub>("/hub");
            });

        }
    }
}

C# 控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.AspNet.SignalR.Client;

namespace SignalRClientApp
{
    class Program
    {
    static void Main(string[] args)
        {
            Console.WriteLine("Client Started!");

            var connection = new HubConnection("https://localhost:44384/hub/");
            var myHub = connection.CreateHubProxy("TestHub");


            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                        task.Exception.GetBaseException());
                }
                Console.Write("Enter your message:");
                while (true)
                {
                    var message = Console.ReadLine();
                    myHub.Invoke("Send", "Console Client1", message).ContinueWith(_task =>
                    {
                        if (_task.IsFaulted)
                        {
                            Console.WriteLine("There was an error calling send: {0}", _task.Exception.GetBaseException());
                        }
                    });

                    myHub.On<HubMes>("newMessage", mes =>
                    {
                        Console.WriteLine("{0}: {1}", mes.name, mes.message);
                    });
                }
            }).Wait();

            connection.Stop();

            Console.ReadLine();
        }

        public class HubMes
        {
            public HubMes(string _name, string _mes)
            {
                name = _name;
                message = _mes;
            }
            public string name { get; set; }
            public string message { get; set; }
        }
    }
}

假设您的服务器不在 https 上 运行 您只需要将您的请求从 https 更改为 http like

var connection = new HubConnection("http://localhost:44384/hub/");