C# 与信号集线器的连接失败,协商404(未找到)
我已经研究这个问题3天了,但找不到解决方案。我用以下代码创建了一个信号集线器:C# 与信号集线器的连接失败,协商404(未找到),c#,asp.net-core,xamarin.android,visual-studio-2017,signalr,C#,Asp.net Core,Xamarin.android,Visual Studio 2017,Signalr,我已经研究这个问题3天了,但找不到解决方案。我用以下代码创建了一个信号集线器: using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; namespace Messenger.Hubs { public class MessengerHub : Hub { public async
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace Messenger.Hubs
{
public class MessengerHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
很简单,启动代码是:
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 Messenger.Hubs;
namespace Messenger
{
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
}
// 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.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSignalR(routes =>
{
routes.MapHub<ChatHub>("c:/myapp/Messenger");
});
app.UseMvc();
}
}
}
尝试连接到集线器会产生以下错误:
{System.Net.Http.HttpRequestException: 404 (Not Found)
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x0002a] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection+<NegotiateAsync>d__44.MoveNext () [0x00226] in <843c441fa9954906b53e3710152bebb9>:0
--- End of stack trace from previous location where exception was thrown
--- at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection+<GetNegotiationResponseAsync>d__51.MoveNext () [0x00077] in <843c441fa9954906b53e3710152bebb9>:0
--- End of stack trace from previous location where exception was thrown
---
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection+<SelectAndStartTransport>d__43.MoveNext () [0x00169] in <843c441fa9954906b53e3710152bebb9>:0
--- End of stack trace from previous location where exception was thrown
---
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection+<StartAsyncCore>d__40.MoveNext () [0x00118] in <843c441fa9954906b53e3710152bebb9>:0
--- End of stack trace from previous location where exception was thrown
--- at System.Threading.Tasks.ForceAsyncAwaiter.GetResult () [0x0000c] in <843c441fa9954906b53e3710152bebb9>:0
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection+<StartAsync>d__39.MoveNext () [0x0008b] in <843c441fa9954906b53e3710152bebb9>:0
--- End of stack trace from previous location where exception was thrown
--- at Microsoft.AspNetCore.SignalR.Client.HttpConnectionFactory+<ConnectAsync>d__3.MoveNext () [0x0009d] in <d50de232736c4c8f910083ea0cb358a8>:0
--- End of stack trace from previous location where exception was thrown
---
at Microsoft.AspNetCore.SignalR.Client.HttpConnectionFactory+<ConnectAsync>d__3.MoveNext () [0x00142] in <d50de232736c4c8f910083ea0cb358a8>:0
--- End of stack trace from previous location where exception was thrown
---
at Microsoft.AspNetCore.SignalR.Client.HubConnection+<StartAsyncCore>d__47.MoveNext () [0x00130] in <f381011e9b214489bcb373743f31ed9d>:0
--- End of stack trace from previous location where exception was thrown
---
at System.Threading.Tasks.ForceAsyncAwaiter.GetResult () [0x0000c] in <f381011e9b214489bcb373743f31ed9d>:0
at Microsoft.AspNetCore.SignalR.Client.HubConnection+<StartAsync>d__39.MoveNext () [0x00091] in <f381011e9b214489bcb373743f31ed9d>:0
--- End of stack trace from previous location where exception was thrown
---
at SignalR.Client.Hubs.SampleHub+<ConnectAsync>d__6.MoveNext () [0x00037] in C:\Users\<username>\Documents\Visual Studio 2017\Projects\signalr_client\signalr_client\Resources\layout\Client.cs:28 }
我不知道下一步该怎么做。我今天遇到了这个错误,在浪费了一些时间之后,我忘了添加:
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<FooHub>("/FooHub");
});
app.UseEndpoints(端点=>
{
endpoints.MapHub(“/FooHub”);
});
在.NET 5 ASP.NET应用程序中配置(…)到Startup.cs>>public void
自最初发布以来,情况发生了一些变化(我确实看到了一些Xamarin),但希望这能给将来的人提供一些指导。我对SignalR NetCore 5也有同样的问题,Blazor webassembly项目在部署到IIS时,在开发机器上的localhost中运行良好 解决方案是在集线器连接中使用完整url: 首先在Startup.cs中包括集线器端点
endpoints.MapHub<BroadcastHub>("/broadcastHub");
使用完整url:
hubConnection = new HubConnectionBuilder()
.WithUrl("http://myserver.com/bcwa/broadcastHub")
.Build();
bcwa是我的IIS部署文件夹您在服务器中有
chatHub
,但正在尝试在客户端中使用MessengerHub
。只需一个类名,就更改了它。。。没有区别,完全相同的问题。如果您有这样声明的hub:public-class-ChatHub:hub,那么您必须确保使用camel-case引用:routes.MapHub(“/ChatHub”);(这会奏效)。但是,这将不起作用:routes.MapHub(“/chathub”);routes.MapHub(“/ChatHub”);不确定您在上面更改了什么,因为您没有显示更改。显然问题在于服务器没有找到Signal,我在routes.MapHub中放置了什么并不重要,服务器正在routes.MapHub中定义的路径上查找名为协商
的文件夹,该路径在配置中不存在。不知道这是从哪里来的,也不知道如何解决。仅供参考。。。我从中复制了集线器代码,在IIS Express中运行,显然不是IIS 8.5。
"POST /MessengerHub/negotiate HTTP/1.1" 303 446
"GET /MessengerHub/negotiate HTTP/1.1" 404 1509
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<FooHub>("/FooHub");
});
endpoints.MapHub<BroadcastHub>("/broadcastHub");
hubConnection = new HubConnectionBuilder()
.WithUrl(navigationManager.ToAbsoluteUri("/broadcastHub")
.Build();
hubConnection = new HubConnectionBuilder()
.WithUrl("http://myserver.com/bcwa/broadcastHub")
.Build();