【发布时间】:2017-05-26 09:53:41
【问题描述】:
我正在使用带有 javascript 客户端的 signalR 为 Web 应用程序后端中长时间运行的 powershell 任务提供进度条。在任务启动后,它会导致应用程序池回收(当调用 powershell 时会发生这种情况,我似乎无法解决这个问题),signalR 执行重新连接,这看起来成功,但进度消息停止出现通过。如果我调试路径,单步执行代码,消息仍然从 powershell 发出,并且广播被调用,但客户端没有收到任何东西。
启动:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
枢纽:
public class ProgressHub : Hub
{
public void SendProgress(string progressMessage, int progressCount)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
hubContext.Clients.All.AddProgress(progressMessage, progressCount);
}
}
客户:
var progress = $.connection.progressHub;
console.log(progress);
progress.client.addProgress = function (message, percentage) {
$('#actionDetail').html(message);
$("#progressBar").progressbar({
value: percentage
});
};
$.connection.hub.logging = true;
$.connection.hub.start().done(function () {
var connectionId = $.connection.hub.id;
console.log(connectionId);
});
客户日志:
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents transport starting.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://2012r2-en-09:7750/signalr/connect?transport=serverSentEvents&clientP…BoA%3D%3D&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=10'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: EventSource connected.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents transport connected. Initiating start request.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: The start request succeeded. Transitioning to the connected state.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
VM145:1 718e2f95-4950-4350-8dff-4b7cb9f1158a
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:46 GMT+0000 (GMT Standard Time)] SignalR: EventSource readyState: 0.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:46 GMT+0000 (GMT Standard Time)] SignalR: EventSource reconnecting due to the server connection ending.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:48 GMT+0000 (GMT Standard Time)] SignalR: EventSource calling close().
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:48 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents reconnecting.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:48 GMT+0000 (GMT Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://2012r2-en-09:7750/signalr/reconnect?transport=serverSentEvents&messa…SBoA%3D%3D&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=7'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:50 GMT+0000 (GMT Standard Time)] SignalR: EventSource connected.
VM145:1 718e2f95-4950-4350-8dff-4b7cb9f1158a
5jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:07:59 GMT+0000 (GMT Standard Time)] SignalR: Triggering client hub event 'AddProgress' on hub 'ProgressHub'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:08:06 GMT+0000 (GMT Standard Time)] SignalR: EventSource readyState: 0.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:08:06 GMT+0000 (GMT Standard Time)] SignalR: EventSource reconnecting due to the server connection ending.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:08:08 GMT+0000 (GMT Standard Time)] SignalR: EventSource calling close().
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:08:08 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents reconnecting.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:08:08 GMT+0000 (GMT Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://2012r2-en-09:7750/signalr/reconnect?transport=serverSentEvents&messa…SBoA%3D%3D&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=9'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1
[10:08:10 GMT+0000 (GMT Standard Time)] SignalR: EventSource connected.
VM145:1 718e2f95-4950-4350-8dff-4b7cb9f1158a
从日志中,您可以看到在初始连接后,执行了重新连接,在此之后消息仍然继续通过。在应用程序池被回收的第二个之后,它们停止了。
那里有类似的问题,例如这个:SignalR client is reconnected after Owin restart, but message is not published 但是,尝试提到的解决方案,他甚至在重新连接之前就停止了所有消息。
【问题讨论】:
-
从你的代码我看不到你在哪里打电话
SendProgress -
SendProgress 是从控制器类中调用的,一个委托回调被传递到后端方法中,该方法回调到控制器,进而调用SendProgress。如果我逐步执行代码,这将按预期工作,在应用程序池回收后,我们仍然调用 SendProgress,但客户端没有收到消息。
-
您是否调试到 ser 停止的位置或未调用的 ser?
-
你的中心方法
SendProgress中的代码不需要调用GetHubContext。只需替换为:Clients.All.AddProgress(progressMessage, progressCount); -
如果这样做,我会收到错误消息“不支持使用不是由 HubPipeline 创建的 Hub 实例。”
标签: c# asp.net asp.net-mvc signalr