SignalR组注册服务器方法未被命中



我已经遵循了本指南,ASP.NET SignalR Hubs API指南(如何从Hub类管理组成员资格),但无法执行服务器端ShipmentHub方法。

我的ShipmentHub类如下:

public class ShipmentHub : Hub
{
    IShipmentLogic shipmentLogic;
    public ShipmentHub(IShipmentLogic shipmentLogic)
    {
        this.shipmentLogic = shipmentLogic;
    }
    public void CreateShipment(IEnumerable<Shipment> shipments)
    {
        // Clients.All.createShipment(shipments.OrderByDescending(s => s.CreatedDate));
        Clients.Group(shipments.FirstOrDefault().ShipmentId)
               .createShipment(shipments.OrderByDescending(s => s.CreatedDate));
    }
    public async Task WatchShipmentId(string shipmentId)
    {
        await Groups.Add(Context.ConnectionId, shipmentId);
        Clients.Group(shipmentId).createShipment(shipmentLogic.Get(shipmentId, true));
    }
    public Task StopWatchingShipmentId(string shipmentId)
    {
        return Groups.Remove(Context.ConnectionId, shipmentId);
    }
}

我的客户,或多或少,看起来是这样的:

var shipmentHub = $.connection.shipmentHub;
$.connection.hub.logging = true;
$.connection.hub.start();
var shipmentId = "SHP-W-GE-100122";
        
if (previousShipmentId) {
    shipmentHub.server.stopWatchingShipmentId(previousShipmentId);
}
if (shipmentId.length) {
    previousShipmentId = shipmentId;
    shipmentHub.server.watchShipmentId(shipmentId);
}

在SignalR客户端日志中,我看到这些被调用:

信号R:调用shipmenthub。WatchShipmentId

信号R:调用shipmenthub。StopWatchingShipmentId

信号R:调用shipmenthub。WatchShipmentId

而且,除了日志之外,这些方法也受到了打击:

proxies['shipmentHub'].server = {
    createShipment: function (shipments) {
        return proxies['shipmentHub'].invoke.apply(proxies['shipmentHub'], $.merge(["CreateShipment"], $.makeArray(arguments)));
     },
    stopWatchingShipmentId: function (shipmentId) {
        return proxies['shipmentHub'].invoke.apply(proxies['shipmentHub'], $.merge(["StopWatchingShipmentId"], $.makeArray(arguments)));
     },
    watchShipmentId: function (shipmentId) {
        return proxies['shipmentHub'].invoke.apply(proxies['shipmentHub'], $.merge(["WatchShipmentId"], $.makeArray(arguments)));
     }
};

最后要注意的是,在我添加WatchStopWatching方法之前,其他一切都有效(即,CreateShipment将毫无问题地调用Client.All.createShipment方法)。

您需要等待与服务器的连接建立,然后才能从客户端开始在服务器上调用方法。hub.start()返回一个promise,这是在该promise被解析后执行某些操作的基本模式。

 var shipmentHub = $.connection.shipmentHub;
    $.connection.hub.logging = true;
    $.connection.hub.start().done(talkToServer);
    var talkToServer=function(){
    var shipmentId = "SHP-W-GE-100122";
        if (previousShipmentId) {
            shipmentHub.server.stopWatchingShipmentId(previousShipmentId);
        }
        if (shipmentId.length) {
            previousShipmentId = shipmentId;
            shipmentHub.server.watchShipmentId(shipmentId);
        }
}

此问题是由于ShipmentHub中的参数化构造函数引起的。根据SignalR:中的依赖注入

默认情况下,SignalR期望集线器类具有无参数构造函数。但是,您可以很容易地注册一个函数来创建集线器实例,并使用该函数来执行DI。通过调用GlobalHost.DependencyResolver.Register.注册函数

因此,您需要修改Startup.Configuration(IAppBuilder app)方法来为您解决依赖关系:

GlobalHost
    .DependencyResolver
    .Register(
        typeof(ShipmentHub), 
           () => new ShipmentHub(new ShipmentLogic()));

相关内容

  • 没有找到相关文章

最新更新