我有一个ASP.NET WebForms应用程序,它有许多SignalR集线器,将从WPF应用程序调用。最初我通过控制台应用程序而不是WPF应用程序测试了这个,只是为了证明这个概念。这是辉煌的,是瞬间的。
最近我开始使用完全相同的代码在WPF应用程序中调用集线器,并且我遇到了很多速度问题(在本地和远程调用时) - connection.Start()。等待( )通话最多可能需要30秒!
有没有明确的理由说明为什么在WPF中连接到http网址可能会很慢?我的调用代码如下。我试过在一个单独的线程中执行调用,但这没有帮助。
private static void Execute(string hubName, List<KeyValuePair<string,object[]>> items)
{
FBDBDataContext dc = new FBDBDataContext();
string url = ConfigurationManager.AppSettings["WebURL"];
var connection = new HubConnection(url);
IHubProxy myHub = connection.CreateHubProxy(hubName);
connection.Start().Wait(); // not sure if you need this if you are simply posting to the hub
foreach (var kvp in items)
myHub.Invoke(kvp.Key, kvp.Value);
}
答案 0 :(得分:2)
每次希望与服务器通信时,都不应创建新连接。在程序开始时创建一次并重复使用它。