问题
Is the output from the SignalR hub proxy generator essentially the same as the dynamically generated hub proxy file? If not, what are the differences?
Some background on my question: I am struggling creating the hub proxy using the command line tool due to dependency issues during execution and I do think obtaining the dynamically generated file might be an easier way.
回答1:
As stated on this ASP.NET page about using hubs with 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.
To make long story short:
This makes your life easier with real JS errors if you mistype SignalR hubs or method names.
With proxy:
var contosoChatHubProxy = $.connection.contosoChatHub;
contosoChatHubProxy.client.addContosoChatMessageToPage = function (name, message) {
console.log(name + ' ' + message);
};
Without proxy:
var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
contosoChatHubProxy.on('addContosoChatMessageToPage', function(name, message) {
console.log(name + ' ' + message);
});
If you need to generate the Proxy file once instead of generating it at runtime, you can follow this section, which allows you generate it beforehand (for caching or bundling behavior).
来源:https://stackoverflow.com/questions/39975129/signalr-generated-proxy-vs-dynamically-created-hub-file