C# MVC3中的信令触发事件客户端

C# MVC3中的信令触发事件客户端,c#,jquery,asp.net-mvc,asp.net-mvc-3,signalr,C#,Jquery,Asp.net Mvc,Asp.net Mvc 3,Signalr,我有一个发票进口商中心,如下所示: public class ImporterHub : Hub, IDisconnect, IConnected { public void InvoiceImported(InvoiceImportedMessage message) { Clients["importer"].InvoiceImported(message); } public void FileImported(FileImportedM

我有一个发票进口商中心,如下所示:

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());
    }
}
public类ImporterHub:Hub、IDisconnect、IConnected
{
公共无效InvoiceImported(InvoiceImportedMessage消息)
{
客户[“进口商”]。发票已导入(信息);
}
已导入公共无效文件(FileImportedMessage消息)
{
客户端[“导入程序”]。文件导入(消息);
}
public System.Threading.Tasks.Task断开连接()
{
返回客户端[“导入程序”]。离开(Context.ConnectionId,DateTime.Now.ToString());
}
public System.Threading.Tasks.Task Connect()
{
返回客户端[“导入程序”].joined(Context.ConnectionId,DateTime.Now.ToString());
}
public System.Threading.Tasks.Task重新连接(IEnumerable group)
{
返回客户端[“导入程序”]。重新加入(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);
    }
[HttpPost]
公共行动结果索引(IndexModel模型)
{
if(ModelState.IsValid)
{
尝试
{
model.NotificationRecipient=model.NotificationRecipient.Replace(“;”,“,”);
ImportConfiguration配置=新的ImportConfiguration()
{
BatchId=model.BatchId,
ReportRecipients=model.NotificationRecipient.Split(',')。选择(c=>c.Trim())
};
var context=GlobalHost.ConnectionManager.GetHubContext();
context.Groups.Add(this.Session.SessionID,“导入器”);
System.Threading.ThreadPool.QueueUserWorkItem(foo=>LaunchFileImporter(config));
InfoFormat(“将ImportProcessor排队以处理发票。发送通知:{0}电子邮件收件人:{1}”,
model.SendNotification、model.NotificationRecipient);
TempData[“message”]=“导入处理器作业已启动。”;
//返回重定向到操作(“索引”、“主页”);
}
捕获(例外情况除外)
{
Log.Error(“未能将发票导入作业正确排队。”,例如);
ModelState.addmodeleror(“,例如Message”);
}
}
专用void启动文件导入器(导入配置配置)
{
使用(var processor=new ImportProcessor())
{
processor.OnFileProcessed+=新的InvoiceFileProcessing(InvoiceFileProcessingHandler);
processor.OnInvoiceProcessed+=新发票已提交(发票提交处理程序);
processor.Execute(配置);
}
}
私有无效InvoiceSubmittedHandler(对象发送方,InvoiceSubmittedEventArgs e)
{
var context=GlobalHost.ConnectionManager.GetHubContext();
var message=new InvoiceImportedMessage()
{
FileName=e.FileName,
TotalErrorsFileProcessed=e.TotalErrors,
TotalInvoicesInFileProcessed=e.TotalInvoices
};
context.Clients[“importer”].InvoiceImported(消息);
}
私有void InvoiceCollectionSubmittedHandler(对象发送方,InvoiceCollectionSubmittedEventArgs e)
{
}
私有void InvoiceFileProcessingHandler(对象发送方,InvoiceFileProcessingEventArgs e)
{
var context=GlobalHost.ConnectionManager.GetHubContext();
var message=新文件导入消息()
{
FileName=e.FileName
};
context.Clients[“importer”].FileImported(消息);
}
我的视图中有以下针对导入程序的脚本:

<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>

jQuery.connection.hub.logging=true;
var importerHub=jQuery.connection.importerHub;
importerHub.InvoiceImported=函数(消息){
jQuery(“#”+message.FileName+“_发票”).text(message.totalinvoicessinfileprocessed);
jQuery(“#”+message.FileName+“_Errors”).text(message.totalErrorsFileProcessed);
};
importerHub.FileImported=函数(消息){
jQuery('#'+message.FileName+'_Processed').text('Done');
};
jQuery.connection.hub.start();
我预期会发生什么:

我希望触发服务器端事件,这将向客户端发送消息, 这将触发事件以更新导入进程的状态

似乎正在发生的事情:

所有服务器端事件都会触发,一切正常。信号器库似乎初始化正确,但事件从未触发,我也从未在屏幕上显示更新

我做错了什么?这是我第一次尝试使用signalR库,所以完全有可能我做错了所有事情。

我相信您的问题在于客户端中心事件是用init caps命名的,signalR的默认行为是在发布到客户端时将这些事件转换为init lower,以与公共Ja对齐vaScript约定。请尝试将中心事件注册更改为:

importerHub.invoiceImported = function (message) { 

我相信您的问题在于客户端中心事件是用init caps命名的,Signal的默认行为是在发布到客户端时将这些事件转换为init lower,以符合常见的JavaScript约定。请尝试将中心事件注册更改为:

importerHub.invoiceImported = function (message) { 


在控制器中,如果删除
[“importer”]
参数,即
context.Clients.InvoiceImported(“消息”),是否会有任何区别;
我试过了,谢谢;它似乎没有什么区别。SessionID?为什么要将SessionID传递给组。Add?这是错误的。在控制器中,如果删除
[“importer”]
参数,即
context.Clients.InvoiceImported(“message”),会有什么区别吗;
我试过了,谢谢;似乎没什么区别。SessionID?为什么要将SessionID传递给组。Add?那是错误的。我确实改变了这个,而且似乎是