2

我刚刚开始使用 SignalR,并且能够毫无问题地将消息广播到所有连接的客户端。我现在想让我的客户(javascript)加入一个组并只接收该组的消息。在我的集线器中,我有以下方法可以将连接添加到命名组:

    public Task JoinGroup(string groupName)
    {
        return this.Groups.Add(Context.ConnectionId, groupName);
    }

在客户端,我有以下 JS:

        var latLonBounds = new google.maps.LatLngBounds();
        var tripIdentifier = '@Model.TripIdentifier';
        $(function () {
            var hub = $.connection.locationHub;
            hub.client.updateLocation = function (coordinates) {
                  // Implementation omitted
                });

                latLonBounds.extend(position);
                map.fitBounds(latLonBounds);
            };
            $.connection.hub.logging = true;
            $.connection.hub.start().done(function() {
                hub.server.joinGroup(tripIdentifier);
            });
        });

当我的页面在 Chrome 中加载时,我在控制台中看到以下 URL 的 HTTP 500:

http://localhost:49914/signalr/send?transport=serverSentEvents&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAA8OmZGDrRfEaOjaHahkkarwAAAAACAAAAAAAQZgAAAAEAACAAAADM78kCBInmZa7OROdPoXraiugBXLR5Xu3htxYC2JSnSAAAAAAOgAAAAAIAACAAAAAZwnPx6GMKOiw%2FivqQPlSfCb4WNP342YxvGyBpalmjSDAAAADljl1vg%2F7GYtD3R4AA2A9LXEnNZkyQVQjDUrW7ZVbvfPpWz1GCcOn6aw5yrkrWFrhAAAAAyX5otz0Xhx8tuIRgX2Pr1b9ZWFMvoairvqzns1%2FhjN%2BRhDIuAqAfonTbsZDjkdyDAZXzAKQqwTEKhpa5LYBhKg%3D%3D

在 Visual Studio 中,我看到以下堆栈跟踪:

SignalR exception thrown by Task: System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: No parameterless constructor defined for this object.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.AspNet.SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor descriptor)
   at Microsoft.AspNet.SignalR.Hubs.DefaultHubManager.ResolveHub(String hubName)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.CreateHub(IRequest request, HubDescriptor descriptor, String connectionId, StateChangeTracker tracker, Boolean throwIfFailedToCreate)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data)
   at Microsoft.AspNet.SignalR.PersistentConnection.<>c__DisplayClassa.<>c__DisplayClassc.<ProcessRequest>b__7()
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func)
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.MissingMethodException: No parameterless constructor defined for this object.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.AspNet.SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor descriptor)
   at Microsoft.AspNet.SignalR.Hubs.DefaultHubManager.ResolveHub(String hubName)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.CreateHub(IRequest request, HubDescriptor descriptor, String connectionId, StateChangeTracker tracker, Boolean throwIfFailedToCreate)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data)
   at Microsoft.AspNet.SignalR.PersistentConnection.<>c__DisplayClassa.<>c__DisplayClassc.<ProcessRequest>b__7()
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func)<---

如果我从我的 JS 中删除对的调用hub.server.joinGroup(tripIdentifier);,那么我不会得到 HTTP 500/异常,所以这行似乎是异常的来源。

任何关于我可以做些什么来使团体注册工作的建议将不胜感激。

谢谢!

4

1 回答 1

7

这是堆栈的一部分,它告诉您出了什么问题:

---> (Inner Exception #0) System.MissingMethodException: No parameterless constructor defined    for this object.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.AspNet.SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor descriptor

基本上这就是说您的集线器类没有无参数构造函数。如果您尝试为集线器设置依赖注入,您似乎在向 SignalR 注册依赖解析器时遇到问题。如果您提供更多详细信息,我们可能也可以帮助您解决该问题。

于 2013-06-19T21:19:36.717 回答