部署到 IIS 时 signalR Hub 协商失败
signalR Hub negotiate fails when deploying to IIS
当我将我的 signalR Hub 部署到本地计算机的 IIS 时,它似乎 运行 在 http://localhost:5527
下没问题 - 但是我的前端无法连接。
前端错误:
POST http://localhost:55271/hub/negotiate?negotiateVersion=1 404 (Not Found)
Utils.js:198 [2020-07-23T17:35:48.643Z] Error: Failed to complete negotiation with the server: Error: Not Found
[2020-07-23T17:35:48.648Z] Error: Failed to start the connection: Error: Not Found
在调试模式下: 在 vs2019 调试模式下,一切正常:
IIS
我在我的 Win 10 盒子上的 IIS 中添加了一个新网站,将路径设置为 C:\dev\git-projects\myProject\WebApp\NotificationsHub\NotificationsHub
,并设置了绑定:
Web.Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Request-Headers" value="User-Agent,Content-Type,Authorization,X-RequestDigest,X-ClientService-ClientTag,XMLHttpRequest,x-requested-with" />
<add name="Access-Control-Allow-Headers" value="User-Agent,Content-Type,Authorization,X-RequestDigest,X-ClientService-ClientTag,XMLHttpRequest,x-requested-with" />
<!-- not sure if really needed; also see AllowAnyMethod() in startup.cs -->
<add name="Access-Control-Request-Method" value="GET,POST,HEAD,OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
C# 中心项目,startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowedSpecificOrigins,
builder =>
{
// without .AllowAnyMethod() we might get a request method cors error in debug mode.
builder.AllowAnyMethod();
});
});
services.AddSignalR(hubOptions => {
hubOptions.EnableDetailedErrors = true;
hubOptions.KeepAliveInterval = System.TimeSpan.FromMinutes(1000);
});
}
前端 Angular 9 代码,片段来自 notification.service.ts:
public startConnection = () => {
let url = this.config.getConfig('notificationsUrl'); // currently "http://localhost:55271/hub"
const sessionNum = this.appSession.session == undefined ? '' : this.appSession.session.SessionID;
this.hubConnection = new signalr.HubConnectionBuilder()
.withUrl(url, {accessTokenFactory: () => sessionNum})
.configureLogging(signalr.LogLevel.Debug)
.build();
this.hubConnection
.start()
.then(() => {
this.hubListener(); // setup some event listeners
this.hubDisconnected();
})
.catch((err) => console.log(`Error while starting connection: ${err}`));
this.hubConnection.serverTimeoutInMilliseconds = 50000;
}
感谢任何帮助,以找出我的 IIS 部署有什么问题,以及为什么我无法从前端建立集线器连接(即再次说明,当我的 IIS 站点关闭时一切正常,我正在调试在带有 IIS express 的 vs2019 中)。
所以我的同事帮助我将 .Net Core signalR 项目部署到我的本地 IIS 10
实例。
我们最终使用了 VS2019 Publish
工具,并将文件推送到 inetpub 下的新文件夹 - c:\inetpub\www\NotifHub
.
这最终为我的通知站点创建了所有必需的文件。运行成功,前端成功发送hub/negotiate
请求
IIS截图:
Chrome 控制台:
上一个问题: 我将 IIS 网站的 physical path
指向 C:\dev\git-projects\MyStuff\WebApp\NotificationsHub\NotificationsHub,这是我们为框架 4.x 项目做的方式(即在本地开发环境中测试 API 项目)。在这种情况下,IIS 似乎无法找到要执行的发布文件。
当我将我的 signalR Hub 部署到本地计算机的 IIS 时,它似乎 运行 在 http://localhost:5527
下没问题 - 但是我的前端无法连接。
前端错误:
POST http://localhost:55271/hub/negotiate?negotiateVersion=1 404 (Not Found)
Utils.js:198 [2020-07-23T17:35:48.643Z] Error: Failed to complete negotiation with the server: Error: Not Found
[2020-07-23T17:35:48.648Z] Error: Failed to start the connection: Error: Not Found
在调试模式下: 在 vs2019 调试模式下,一切正常:
IIS
我在我的 Win 10 盒子上的 IIS 中添加了一个新网站,将路径设置为 C:\dev\git-projects\myProject\WebApp\NotificationsHub\NotificationsHub
,并设置了绑定:
Web.Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Request-Headers" value="User-Agent,Content-Type,Authorization,X-RequestDigest,X-ClientService-ClientTag,XMLHttpRequest,x-requested-with" />
<add name="Access-Control-Allow-Headers" value="User-Agent,Content-Type,Authorization,X-RequestDigest,X-ClientService-ClientTag,XMLHttpRequest,x-requested-with" />
<!-- not sure if really needed; also see AllowAnyMethod() in startup.cs -->
<add name="Access-Control-Request-Method" value="GET,POST,HEAD,OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
C# 中心项目,startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowedSpecificOrigins,
builder =>
{
// without .AllowAnyMethod() we might get a request method cors error in debug mode.
builder.AllowAnyMethod();
});
});
services.AddSignalR(hubOptions => {
hubOptions.EnableDetailedErrors = true;
hubOptions.KeepAliveInterval = System.TimeSpan.FromMinutes(1000);
});
}
前端 Angular 9 代码,片段来自 notification.service.ts:
public startConnection = () => {
let url = this.config.getConfig('notificationsUrl'); // currently "http://localhost:55271/hub"
const sessionNum = this.appSession.session == undefined ? '' : this.appSession.session.SessionID;
this.hubConnection = new signalr.HubConnectionBuilder()
.withUrl(url, {accessTokenFactory: () => sessionNum})
.configureLogging(signalr.LogLevel.Debug)
.build();
this.hubConnection
.start()
.then(() => {
this.hubListener(); // setup some event listeners
this.hubDisconnected();
})
.catch((err) => console.log(`Error while starting connection: ${err}`));
this.hubConnection.serverTimeoutInMilliseconds = 50000;
}
感谢任何帮助,以找出我的 IIS 部署有什么问题,以及为什么我无法从前端建立集线器连接(即再次说明,当我的 IIS 站点关闭时一切正常,我正在调试在带有 IIS express 的 vs2019 中)。
所以我的同事帮助我将 .Net Core signalR 项目部署到我的本地 IIS 10
实例。
我们最终使用了 VS2019 Publish
工具,并将文件推送到 inetpub 下的新文件夹 - c:\inetpub\www\NotifHub
.
这最终为我的通知站点创建了所有必需的文件。运行成功,前端成功发送hub/negotiate
请求
IIS截图:
Chrome 控制台:
上一个问题: 我将 IIS 网站的 physical path
指向 C:\dev\git-projects\MyStuff\WebApp\NotificationsHub\NotificationsHub,这是我们为框架 4.x 项目做的方式(即在本地开发环境中测试 API 项目)。在这种情况下,IIS 似乎无法找到要执行的发布文件。