C# SignalR:调用集线器方法“XXX”时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19688673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:37:30  来源:igfitidea点击:

SignalR: There was an error invoking Hub method "XXX"

c#.netwindows-8windows-runtimesignalr

提问by Isilm? O.

Server:

服务器:

public void AddLine(string line)
{
    Clients.Others.addLine(line);
}

.NET Client:

.NET 客户端:

await rtHubProxy.Invoke("AddLine", "lineInfo");

Exception:

例外:

InvalidOperationException: There was an error invoking Hub method 'xxx.AddLine'.

Actually, I was trying to invoke the method with complex object, only to find the exception. Therefore, I changed the type of the parameter and left the AddLine() body blank for debugging purposes, which weirdly enough, it still threw the same exception.

实际上,我试图用复杂对象调用方法,却发现异常。因此,我更改了参数的类型并将 AddLine() 主体留空以进行调试,这很奇怪,它仍然抛出相同的异常。

I also have another SignalR invocation on the client-side, just several lines above, which runs responsively without error. The corresponding server-side code as follows:

我在客户端还有另一个 SignalR 调用,就在上面几行,它可以响应地运行而没有错误。对应的服务端代码如下:

public void Hello(string text)
{
    Clients.All.hello(text);
}

Could anyone find out where I've got wrong? I've been debugging for more than 4 hours and still cannot find the undoing even after I simplified the code.

谁能找出我错在哪里?我已经调试了 4 个多小时,即使我简化了代码,仍然找不到撤消的地方。

(Spelling strictly checked, no mismatch.)

(拼写严格检查,没有错别字。)

采纳答案by Hirad Nikoo

Clearly you have some problems on the server side. But to find out what's the problem you need to see a detailed error. For security reasons SignalR doesn't give you a detailed error by default. But you can edit your code to get the detailed error.

显然,您在服务器端遇到了一些问题。但是要找出问题所在,您需要查看详细的错误。出于安全原因,SignalR 默认情况下不会为您提供详细的错误信息。但是您可以编辑代码以获取详细的错误信息。

First you need to add this code to the Startup.cs:

首先,您需要将此代码添加到 Startup.cs 中:

var hubConfiguration = new HubConfiguration();
    hubConfiguration.EnableDetailedErrors = true;
    app.MapSignalR(hubConfiguration);

Then to show the error on the client side you need to add this code to your project:

然后要在客户端显示错误,您需要将此代码添加到您的项目中:

$.connection.hub.error(function (error) {
    console.log('SignalR error: ' + error)
});

As simple as that and you're good to go.

就这么简单,你就可以开始了。

Sometimes you don't even get to the client part to see the error. But by doing the first part you'll enable the Detailed Error and you can see the errors on the SignalR response. You just need a tool like Chrome Browser Web Developer Tool which gives you the Network part of the operation where all the data transfer status are logged. you can check the SignalR errors there. The information on the log will be very detailed and helpful.

有时您甚至没有到客户端查看错误。但是通过执行第一部分,您将启用详细错误,并且您可以在 SignalR 响应中看到错误。您只需要一个像 Chrome 浏览器网络开发工具这样的工具,它为您提供操作的网络部分,其中记录了所有数据传输状态。您可以在那里检查 SignalR 错误。日志上的信息将非常详细和有用。

This is a sample debug with Chrome Browser for people who what to see SignalR errors through Chrome debug tool:

这是使用 Chrome 浏览器调试的示例,适用于通过 Chrome 调试工具查看 SignalR 错误的人:

Download Full Image Size

下载完整图像尺寸

This is for people who want to debug the ajax response through Chrome Browser

这适用于想要通过 Chrome 浏览器调试 ajax 响应的人

Let me know if you have other problems regarding that.

如果您对此有其他问题,请告诉我。

For extra information go to: SignalR Errors

有关更多信息,请访问:SignalR 错误

回答by Isilm? O.

OK, after 2 hours and half's rocky debugging, I finally found out the truth, realizing that weiredly, however I changed my server-side code, the undercover mechanism or functionalities didn't somehow get synchronized until I accidently rebuilt the project and waited the IIS Express to warm-up again.

好吧,经过2个半小时的艰难调试,我终于找到了真相,奇怪地意识到,然而我改变了我的服务器端代码,卧底机制或功能并没有以某种方式同步,直到我不小心重建了项目并等待IIS Express 再次预热。

It's not SignalR's fault, but Visual Studio's I think. Every time I make some changes to the Hub class, I have to rebuild the server-side project. That's it. I have to say, it really aches, though I don't know why this should happen - Maybe that's because my solution consists of WinRT and ASP.NET project that don't get along well? I don't know.

这不是 SignalR 的错,而是我认为 Visual Studio 的错。每次对Hub类做一些改动,都得重新构建服务器端项目。就是这样。我不得不说,它真的很痛,虽然我不知道为什么会发生这种情况 - 也许那是因为我的解决方案由无法很好相处的 WinRT 和 ASP.NET 项目组成?我不知道。

FYI, I will attach a link to a post, in which a similar "rebuild" issue did happen to someone else.

仅供参考,我将附上一个帖子的链接,其中其他人确实发生了类似的“重建”问题。

http://forum.kooboo.com/yaf_postst2250_How-to-use-SignalR-in-a-Module.aspx

http://forum.kooboo.com/yaf_postst2250_How-to-use-SignalR-in-a-Module.aspx

And the workaround for now is more than simple - just go and REBUILD.

现在的解决方法不仅仅是简单 - 只需去重新构建。