我有一个发票进口商中心,如下所示:
public class ImporterHub : Hub, IDisconnect, IConnected
{
public void InvoiceImported(InvoiceImportedMessage message)
{
Clients["importer"].InvoiceImported(message);
}
public void FileImported(FileImportedMessage message)
{
Clients["importer"].FileImported(message);
}
public System.Threading.Tasks.Task Disconnect()
{
return Clients["importer"].leave(Context.ConnectionId, DateTime.Now.ToString());
}
public System.Threading.Tasks.Task Connect()
{
return Clients["importer"].joined(Context.ConnectionId, DateTime.Now.ToString());
}
public System.Threading.Tasks.Task Reconnect(IEnumerable<string> groups)
{
return Clients["importer"].rejoined(Context.ConnectionId, DateTime.Now.ToString());
}
}
在我的控制器中,我正在为长时间运行的导入过程捕获事件,如下所示:
[HttpPost]
public ActionResult Index(IndexModel model)
{
if (ModelState.IsValid)
{
try
{
model.NotificationRecipient = model.NotificationRecipient.Replace(';', ',');
ImportConfiguration config = new ImportConfiguration()
{
BatchId = model.BatchId,
ReportRecipients = model.NotificationRecipient.Split(',').Select(c => c.Trim())
};
var context = GlobalHost.ConnectionManager.GetHubContext<ImporterHub>();
context.Groups.Add(this.Session.SessionID, "importer");
System.Threading.ThreadPool.QueueUserWorkItem(foo => LaunchFileImporter(config));
Log.InfoFormat("Queued the ImportProcessor to process invoices. Send Notification: {0} Email Recipient: {1}",
model.SendNotification, model.NotificationRecipient);
TempData["message"] = "The import processor job has been started.";
//return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
Log.Error("Failed to properly queue the invoice import job.", ex);
ModelState.AddModelError("", ex.Message);
}
}
private void LaunchFileImporter(ImportConfiguration config)
{
using (var processor = new ImportProcessor())
{
processor.OnFileProcessed += new InvoiceFileProcessing(InvoiceFileProcessingHandler);
processor.OnInvoiceProcessed += new InvoiceSubmitted(InvoiceSubmittedHandler);
processor.Execute(config);
}
}
private void InvoiceSubmittedHandler(object sender, InvoiceSubmittedEventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<ImporterHub>();
var message = new InvoiceImportedMessage()
{
FileName = e.FileName,
TotalErrorsInFileProcessed = e.TotalErrors,
TotalInvoicesInFileProcessed = e.TotalInvoices
};
context.Clients["importer"].InvoiceImported(message);
}
private void InvoiceCollectionSubmittedHandler(object sender, InvoiceCollectionSubmittedEventArgs e)
{
}
private void InvoiceFileProcessingHandler(object sender, InvoiceFileProcessingEventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<ImporterHub>();
var message = new FileImportedMessage()
{
FileName = e.FileName
};
context.Clients["importer"].FileImported(message);
}
我认为导入器有以下脚本:
<script type="text/javascript">
jQuery.connection.hub.logging = true;
var importerHub = jQuery.connection.importerHub;
importerHub.InvoiceImported = function (message) {
jQuery('#' + message.FileName + '_Invoices').text(message.TotalInvoicesInFileProcessed);
jQuery('#' + message.FileName + '_Errors').text(message.TotalErrorsInFileProcessed);
};
importerHub.FileImported = function (message) {
jQuery('#' + message.FileName + '_Processed').text('Done');
};
jQuery.connection.hub.start();
</script>
我期望发生的事情:
我期待服务器端事件触发,这将向客户端发送消息,而客户端又会触发事件以更新导入过程的状态。
似乎发生了什么:
所有服务器端事件触发,一切正常。signalR 库似乎已正确初始化,但事件从未触发,而且我从未让更新出现在屏幕上。
我究竟做错了什么?这是我第一次尝试使用 signalR 库,所以我完全有可能做错了一切。