C# 信号器连接客户端到集线器显示404错误

C# 信号器连接客户端到集线器显示404错误,c#,asp.net-core,signalr,console-application,C#,Asp.net Core,Signalr,Console Application,我正试图从ASP.NET核心应用程序向一个c#console应用程序客户端发送一条消息。当我尝试将控制台应用程序连接到集线器时,我总是收到404 not found异常。有人能帮我把这个存档吗 这就是我到目前为止所做的: 服务器: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; u

我正试图从ASP.NET核心应用程序向一个c#console应用程序客户端发送一条消息。当我尝试将控制台应用程序连接到集线器时,我总是收到404 not found异常。有人能帮我把这个存档吗

这就是我到目前为止所做的:

服务器:

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");
            });

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用Microsoft.Extensions.Options;
使用CoreSignalServer.Hubs;
名称空间CoreSignalServer
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
//信号机注册中间件
app.usesigner(路由=>
{
//url大多数以小写字母开头
routes.MapHub(“/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; }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.AspNet.signal.Client;
名称空间SignalClientApp
{
班级计划
{
静态void Main(字符串[]参数)
{
WriteLine(“客户端已启动!”);
var连接=新的HUB连接(“https://localhost:44384/hub/");
var myHub=connection.createhubbroxy(“TestHub”);
connection.Start().ContinueWith(任务=>
{
if(task.IsFaulted)
{
WriteLine(“打开连接时出错:{0}”,
task.Exception.GetBaseException());
}
控制台。写(“输入您的消息:”);
while(true)
{
var message=Console.ReadLine();
调用(“发送”,“控制台客户端1”,消息)。继续(\u任务=>
{
如果(_task.IsFaulted)
{
WriteLine(“调用send:{0}”时出错,_task.Exception.GetBaseException());
}
});
myHub.On(“新消息”,mes=>
{
Console.WriteLine(“{0}:{1}”,mes.name,mes.message);
});
}
}).Wait();
连接。停止();
Console.ReadLine();
}
公共类HubMes
{
公共HubMes(字符串名称、字符串名称)
{
名称=_名称;
消息=_mes;
}
公共字符串名称{get;set;}
公共字符串消息{get;set;}
}
}
}

假设您的服务器未在
https
上运行,您只需将请求从
https
更改为
http
,如

var连接=新的轮毂连接(“http://localhost:44384/hub/");

您的服务器是否正在运行https?如果没有,请将https更改为http。好的,谢谢。我不再收到任何错误了。但我不太确定客户端是否连接正确。我如何向所有客户发送消息?这将是另一个问题。如果你不在评论中提出所有问题,那就更好了。我将添加我的回答,您可以接受它,然后创建另一个问题。