我们在ASP.NET web应用程序中使用SignalR库。代码看起来如下:SignalR hub挂在IIS上
服务器:
[HubName("ticketsCounterHub")]
public class MassivePrintHub : Hub
{
public void PostTicketsCount(long count)
{
Clients.All.Send(count);
}
}
public class HubFactory
{
private HubFactory() {}
public static readonly HubFactory Current = new HubFactory();
public IHubProxy GetMassivePrintHubProxy()
{
var hubConnection = new HubConnection(ConfigUtils.GetRequiredSettingValue("adminPath"));
var hubProxy = hubConnection.CreateHubProxy("ticketsCounterHub");
hubConnection.Start().Wait();
return hubProxy;
}
}
客户端(JavaScript的):
MassivePrintApp.controller("ListController", function ($scope, Dates) {
var hubManager = (function() {
var massivePrintHub = $.connection.ticketsCounterHub;
$.connection.hub.start();
return { massivePrintHub: massivePrintHub };
}());
hubManager.massivePrintHub.client.Send = function (ticketsCount) {
$scope.action.Quantity = ticketsCount;
$scope.$digest();
};
});
代码的关键部分是在MVC控制器:
public FileResult PrintAction(int actionId, int count, DateTime actionDate, bool isThermo=false)
{
var ticketsCount = _ticketService.GetTicketsInStatusCount(actionId, actionDate, TicketStatusEnum.ToPrint);
HubFactory.Current.GetMassivePrintHubProxy().Invoke("PostTicketsCount", ticketsCount);
var stream = new MemoryStream();
xmlResponse.Save(stream);
stream.Flush();
stream.Position = 0;
return File(stream,ContentTypeEnum.XML.ToString(),String.Format("массовая {0} мероприятия {1} {2}шт.xml", isThermo?"термопечать":"печать", action.Artist,count));
}
正如你可以看到,我们有这条线:
HubFactory.Current.GetMassivePrintHubProxy().Invoke("PostTicketsCount", ticketsCount);
而这会导致问题,即无论何时我们称之为集线器的一个实例已添加到IIS上的“请求”部分。
我知道我们已经在JavaScript代码中启动了集线器,但我不知道如何使用现有连接或如何摆脱HubFactory或删除创建的集线器实例。
我不明白为什么集线器挂在IIS上。
Hub类与您的MVC控制器在不同的应用程序中吗?如果不是,那么不用创建hubproxy,你可以通过集线器上下文来调用:http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub – DDan
MVC控制器放置在主ASP.NET应用程序使用的依赖项目中。我已经尝试以这种方式使用集线器上下文: IHubContext context = GlobalHost.ConnectionManager.GetHubContext(); context.Clients.All.Send(ticketsCount); 但它不起作用。也许我做错了什么。 –
tesicg
集线器也放置在另一个项目中。 – tesicg