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

时间:2019-11-11 08:57:36

标签: c# asp.net-core signalr console-application

我正尝试将消息从ASP.NET Core应用程序发送到客户端,该客户端是c#控制台应用程序。当我尝试将控制台应用程序连接到集线器时,我总是收到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");
            });

        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

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

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