SignalR:生成的代理与动态创建的集线器文件

SignalR: Generated proxy vs. dynamically created hub file

SignalR 集线器代理生成器的输出是否与动态生成的集线器代理文件基本相同?如果不是,有什么区别?

关于我的问题的一些背景知识:由于执行期间的依赖性问题,我正在努力使用命令行工具创建集线器代理,我确实认为获取动态生成的文件可能是一种更简单的方法。

on this ASP.NET page所述,关于将集线器与 SignalR 结合使用:

The generated proxy and what it does for you

You can program a JavaScript client to communicate with a SignalR service with or without a proxy that SignalR generates for you. What the proxy does for you is simplify the syntax of the code you use to connect, write methods that the server calls, and call methods on the server.

When you write code to call server methods, the generated proxy enables you to use syntax that looks as though you were executing a local function: you can write serverMethod(arg1, arg2) instead of invoke('serverMethod', arg1, arg2). The generated proxy syntax also enables an immediate and intelligible client-side error if you mistype a server method name. And if you manually create the file that defines the proxies, you can also get IntelliSense support for writing code that calls server methods.

长话短说:

如果您输入错误的 SignalR 集线器或方法名称,这会让您的生活更轻松,因为真正的 JS 错误。

使用代理:

var contosoChatHubProxy = $.connection.contosoChatHub;
contosoChatHubProxy.client.addContosoChatMessageToPage = function (name, message) {
    console.log(name + ' ' + message);
};

没有代理:

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
contosoChatHubProxy.on('addContosoChatMessageToPage', function(name, message) {
    console.log(name + ' ' + message);
});

如果您需要生成代理文件一次而不是在运行时生成它,您可以按照this section,这允许您预先生成它(用于缓存或捆绑行为)。