2

我有以下客户端代码(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
4

1 回答 1

0

我有同样的问题......使用 VS2010、Silverlight4、Nuget 2.1:

像 EkoostikMartin 一样使用 System.Threading.Tasks.SL4 2.1.2,但同样的错误。

在命名空间 Microsoft.AspNet.SignalR.Client.Http;HttpHelper 类:

public static Task<HttpWebResponse> GetHttpResponseAsync(this HttpWebRequest request)
        {
            try
            {
                return Task.Factory.FromAsync<HttpWebResponse>(request.BeginGetResponse, ar => (HttpWebResponse)request.EndGetResponse(ar), null);
            }
            catch (Exception ex)
            {
                return TaskAsyncHelper.FromError<HttpWebResponse>(ex);
            }
        }

错误:(HttpWebResponse)request.EndGetResponse(ar)

WebException:我可以看到看起来正确的请求 URL:localhost:12345/signalr/...

于 2012-11-27T15:49:57.037 回答