Signalr - не удается подключиться к серверу с клиента
Привет всем, кто может мне помочь, почему я не могу подключиться к серверу со стороны клиента? продолжайте показывать "код состояния ответа не указывает на успех: 404 (не найден)"
Что я уже пробовал:
автозагрузки.в CS
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; 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 WebApplication10.Hubs; namespace WebApplication10 { 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 ConfigurationServices(IServiceCollection services) { services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSignalR(); } //This method gets called by the runtime. Use this method to configure the HTTP requerst pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseSignalR(routes => { routes.MapHub<TestHub>("/testHub"); }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Authorization}/{action=HelloWorld}") }); } } }
TestHub.в CS
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; namespace WebApplication10.Hubs { public class TestHub : Hub { public override bool Equals(object obj) { return base.Equals(obj); } public override int GetHashCode() { return base.GetHashCode(); } public override Task OnConnectedAsync() { Clients.Caller.SendAsync("Connected", Context.ConnectionId); return base.OnConnectedAsync(); } public override Task OnDisconnectedAsync(Exception exception) { return base.OnDisconnectedAsync(exception); } public override string ToString() { return base.ToString(); } protected override void Dispose(bool disposing) { base.Dispose(disposing); } } }
ValuesController.в CS
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using WebApplication10.Hubs; namespace WebApplication10.Controllers { [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { IHubContext<TestHub> _hubContext; public ValuesController(IHubContext<TestHub> hubContext) { _hubContext = hubContext; } //Get api/values [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "value1", "value2" }; } //Get api/values/5 [HttpGet("{id}")] public ActionResult<string> Get(int id) { return "value"; } //POST api/values [HttpPost] public void Post([FromBody] string value) { _hubContext.Clients.All.SendAsync("Posted", value); } //PUT api/value/5 [HttpPut("{id}")] public void Put(int id, [FromBody] string value) { } //DELETE api/values/5 [HttpDelete("{id}")] public void Delete(int id) { } } }
Файл MainWindow.язык XAML.в CS
using Microsoft.AspNetCore.SignalR.Client; using System; using System.Threading.Tasks; using System.Windows; namespace WpfApp1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private HubConnection _connection; public MainWindow() { InitializeComponent(); _connection = new HubConnectionBuilder() .WithUrl("http://localhost:8081/api/") .Build(); _connection.Closed += async (error) => { await Task.Delay(new Random().Next(0, 5) * 1000); await _connection.StartAsync(); }; } private async void BtnConnect_Click(object sender, RoutedEventArgs e) { _connection.On<string>("Connected", (connectionid) => { //MessageBox.Show(connectionid); tbMain.Text = connectionid; }); _connection.On<string>("Posted", (value) => { Dispatcher.BeginInvoke((Action)(() => { messageList.Items.Add(value); })); }); try { await _connection.StartAsync(); messageList.Items.Add("Connection started"); btnConnect.IsEnabled = false; } catch(Exception ex) { messageList.Items.Add(ex.Message); } } } }