SignalR .Net Core 3.1 Web应用程序无法在本地IIS上运行 [英] SignalR .Net Core 3.1 Web Application not working on local IIS
问题描述
尝试使用ASP.Net Core 3.1和Angular基于在线示例创建聊天.
以下内容在IIS Express上运行良好,但在本地IIS上效果不佳.
错误消息:WebSocketTransport.js:85到'ws://localhost/MyCoreAPI/ChatHub'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:400
启动:
public void ConfigureServices(IServiceCollection服务){services.AddSignalR();}公共无效配置(IApplicationBuilder应用,IWebHostEnvironment env,ApplicationDbContext上下文){app.UseCors("EnableCORS");app.UseMiddleware< ErrorHandlingMiddleware>();app.UseMiddleware< TokenServiceMiddleware>();app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();app.UseAuthentication();app.UseAuthorization();app.UseEndpoints(route =>{route.MapHub< ChatHub>("/ChatHub");});app.UseMvc(routes =>{route.MapRoute(名称:默认",模板:"{controller = auth}/{action}/");});app.Run(async(c)=>{等待c.Response.WriteAsync("My Core Web API");});}
ChatHub:
公共类ChatHub:集线器{公共任务SendMessageToAll(消息)=>Clients.All.SendAsync("receiveMessage",message);}
客户:
导出类ChatService {receiveMessage =新的EventEmitter< Message>();connectionFounded =新的EventEmitter< Boolean>();私人连接已建立=否;private _hubConnection:HubConnection;Constructor(){this.createConnection();this.registerOnServerEvents();this.startConnection();}异步sendMessage(message:Message){console.log(消息);等待this._hubConnection.invoke('SendMessageToAll',消息);}私人createConnection(){this._hubConnection =新的HubConnectionBuilder().configureLogging(LogLevel.Debug).withUrl('http://localhost/MyCoreAPI/ChatHub',{skipNegotiation:true,运输:HttpTransportType.WebSockets}).建造();}私人startConnection():void {this._hubConnection.开始().then(()=> {this.connectionIsEstablished = true;console.log('Hub连接已开始');this.connectionFounded.emit(true);}).catch(err => {console.log('建立连接时发生错误,正在重试...');setTimeout(function(){this.startConnection();},5000);});}私人registerOnServerEvents():void {this._hubConnection.on('receiveMessage',(data:any)=> {console.log(data);this.receiveMessage.emit(data);});}}}
在IIS Express上,使用' http://localhost:59235/ChatHub 可以正常使用.当我使用' http://localhost/MyCoreAPI/ChatHub 切换到本地IIS时,出现错误./p>
能否检查IIS中是否启用了WebSockets协议.
打开或关闭Windows功能"-> Internet信息服务->万维网服务->应用程序开发功能-> WebSocket协议
Trying to create a chat based on an online example using ASP.Net Core 3.1 and Angular.
The following works perfectly well on IIS Express, but not on local IIS.
Error Message: WebSocketTransport.js:85 WebSocket connection to 'ws://localhost/MyCoreAPI/ChatHub' failed: Error during WebSocket handshake: Unexpected response code: 400
Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env,
ApplicationDbContext context)
{
app.UseCors("EnableCORS");
app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseMiddleware<TokenServiceMiddleware>();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(route =>
{
route.MapHub<ChatHub>("/ChatHub");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=auth}/{action}/");
});
app.Run(async (c) =>
{
await c.Response.WriteAsync("My Core Web API");
});
}
ChatHub:
public class ChatHub : Hub
{
public Task SendMessageToAll(Message message) =>
Clients.All.SendAsync("receiveMessage", message);
}
Client:
export class ChatService {
receiveMessage = new EventEmitter<Message>();
connectionEstablished = new EventEmitter<Boolean>();
private connectionIsEstablished = false;
private _hubConnection: HubConnection;
constructor() {
this.createConnection();
this.registerOnServerEvents();
this.startConnection();
}
async sendMessage(message: Message) {
console.log(message);
await this._hubConnection.invoke('SendMessageToAll', message);
}
private createConnection() {
this._hubConnection = new HubConnectionBuilder()
.configureLogging(LogLevel.Debug)
.withUrl('http://localhost/MyCoreAPI/ChatHub', {
skipNegotiation:true,
transport:HttpTransportType.WebSockets
})
.build();
}
private startConnection(): void {
this._hubConnection
.start()
.then(() => {
this.connectionIsEstablished = true;
console.log('Hub connection started');
this.connectionEstablished.emit(true);
})
.catch(err => {
console.log('Error while establishing connection, retrying...');
setTimeout(function () { this.startConnection(); }, 5000);
});
}
private registerOnServerEvents(): void {
this._hubConnection.on('receiveMessage', (data: any) => {
console.log(data);
this.receiveMessage.emit(data);
});
}
}
}
On IIS Express using 'http://localhost:59235/ChatHub' everything works. When I switch to local IIS using 'http://localhost/MyCoreAPI/ChatHub' I get the error.
Can you check if WebSockets Protocol is enabled in IIS.
"Turn Windows features on or off" -> Internet Information Services ->World Wide Web Services -> Application Development Features -> WebSocket Protocol
这篇关于SignalR .Net Core 3.1 Web应用程序无法在本地IIS上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!