我正在尝试在Angle Client和.net核心服务器之间建立信号连接,但在服务器端却遇到以下异常:
失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware [1] 执行请求时发生未处理的异常。 System.InvalidOperationException:未指定authenticationScheme,也没有找到DefaultChallengeScheme。 在Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext上下文,字符串方案,AuthenticationProperties属性) 在Microsoft.AspNetCore.Http.Connections.Internal.AuthorizeHelper.AuthorizeAsync(HttpContext上下文,IList`1策略) 在Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionDispatcher.ExecuteNegotiateAsync(HttpContext上下文,HttpConnectionDispatcherOptions选项) 在Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) 在Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) 在Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext上下文) 在Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext上下文) 在Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext上下文) 在Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext上下文) 在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:错误:执行请求时发生未处理的异常。
我的服务器配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); });
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<StorigiContext>(options =>
options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("StorigiContext")));
services.AddSignalR();
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins("http://localhost:4200");
}));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseCors("CorsPolicy");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}"
);
});
app.UseSignalR(routes =>
{
routes.MapHub<NotificationHub>("/hubs/notify");
});
}
集线器类
public class NotificationHub : Hub
{
private readonly static ConnectionMapper _connections =
new ConnectionMapper();
public Task SendMessage(string message)
{
return Clients.All.SendAsync("sendNotification", message);
}
}
连接是通过有角度的客户端进行的:像这样:
public hubConnection: HubConnection;
public messages: string[] = [];
constructor(private service:AuthenticationServiceService, private router:Router) { }
ngOnInit() {
let builder = new HubConnectionBuilder();
// as per setup in the startup.cs
this.hubConnection = builder.withUrl('/hubs/notify')
.build();
// message coming from the server
this.hubConnection.on("sendNotification", (message) => {
alert(message);
this.messages.push(message);
});
// starting the connection
this.hubConnection.start();
}
我想知道为什么会发生这种异常?