SignalR / Silverlight:必须先调用Start才能发送数据。

时间:2012-08-14 15:21:05

标签: c# silverlight signalr

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

1 个答案:

答案 0 :(得分:0)

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

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

在命名空间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 /...