SignalR挂与外部模型类

本文关键字:模型 外部 SignalR | 更新日期: 2023-09-27 18:05:21

我有一个非常简单的SignalR集线器,每当我尝试将模型类移动到类库时,都会注意到集线器上挂起。每当我在ASP.net项目中离开模型类时,它就会正常工作并从服务器检索对象,但是一旦我将模型类移动到类库(用于跨项目重用),SignalR调用来自客户端(js端)挂起。我可以看到请求通过设置一个断点使其成为集线器,但之后它不会在客户端返回。这里有什么线索吗?

我已经在客户端启用了日志记录:

[16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: Client subscribed to hub 'myHubhub'.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22myHubhub%22%7D%5D'.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents transport starting.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:1275/signalr/connect?transport=serverSentEvents&clientProt…VWFxHpWoC0T&connectionData=%5B%7B%22name%22%3A%22myHubhub%22%7D%5D&tid=6'.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: EventSource connected.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: serverSentEvents transport connected. Initiating start request.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: The start request succeeded. Transitioning to the connected state.
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
jquery.signalR.min-2.2.0.js:8 [16:37:39 GMT+0200 (W. Europe Daylight Time)] SignalR: Invoking myHubhub.RetrieveMyObjects

服务器端hub方法:

public IEnumerable<MyModel> RetrieveMyObjects()
{
    using (DatabaseContext dbContext = new DatabaseContext())
    {
        Debug.WriteLine("RetrieveMyObjects()");
        List<MyModel> objs = dbContext.MyObjects.ToList();
        return objs;
    }
}

客户端调用:

@section scripts
{        
    <script>       
        $(function () {           
            // Reference the auto-generated proxy for the hub.
            var chat = $.connection.myHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + htmlEncode(name)
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            $.connection.hub.logging = true;
            // Start the connection.
            $.connection.hub.start().done(function () {
                // Call the Send method on the hub.
                chat.server.retrieveMyObjects().done(function (result) {
                    console.log("yep");
                    //Process logic
                }).fail(function (err) {
                    console.log('Could not Connect! ' + err);
                });
            });
        });
    </script>
}

SignalR挂与外部模型类

嗯,我开始剥离我的模型,找到了答案。我有一个标记为"[JsonConverter(typeof(StringEnumConverter))]"的enum属性,用于将enum序列化为字符串值。这显然打破了一些东西,每当模型是在一个外部类库?

编辑:

问题:外部类库引用Netwonsoft。Json 7.0.0和我的ASP。. NET项目参考版本6.0.4..有一次我下调了网游的评级。