我有以下客户端代码(Silverlight 4):
HubConnection hubConnection;
IHubProxy alertHub;
hubConnection = new HubConnection("http://localhost:59805/");
alertHub = hubConnection.CreateProxy("alertservice");
Task startTask = hubConnection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
}
alertHub.Invoke("NewMessage", "asdf", "jkl").Wait();
});
以及以下服务器代码(托管在 ASP.Net Web 项目中):
[HubName("alertservice")]
public class AlertServiceHub : Hub
{
public void NewMessage(string userName, string messageText)
{
Clients.AddMessage(userName, messageText);
}
}
在该alertHub.Invoke
方法上,我收到错误“必须先调用 Start 才能发送数据”。
奇怪的是,当我检查 Fiddler 的连接尝试时,返回以下内容(注意“连接:关闭”):
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 14 Aug 2012 15:11:29 GMT
X-AspNet-Version: 4.0.30319
Transfer-Encoding: chunked
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: text/event-stream
Connection: Close