SignalR подключить клиента к концентратору с ошибкой 404 - PullRequest
0 голосов
/ 11 ноября 2019

Я пытаюсь отправить сообщение из моего основного приложения ASP.NET клиенту, который является консольным приложением ac #. Когда я пытаюсь подключить консольное приложение к концентратору, я всегда получаю исключение 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; }
        }
    }
}

1 Ответ

0 голосов
/ 11 ноября 2019

Предполагая, что ваш сервер не работает на https, вам нужно всего лишь изменить ваш запрос с https на http как :

var connection = new HubConnection("http://localhost:44384/hub/");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...