[英]Cannot open signalr connection in silverlight client
I am trying to implement SignalR in Silverlight based on this blog: SignalR and Silverlight . 我正在尝试基于这个博客在Silverlight中实现SignalR: SignalR和Silverlight 。
When I try to .Invoke() I get a runtime error "System.InvalidOperationException: The Start method must be called before data can be sent. at Microsoft.AspNet.SignalR.Client.Connection.Send(String data) at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[T](String method, Object[] args)..." 当我尝试.Invoke()时,我收到一个运行时错误“System.InvalidOperationException:必须先调用Start方法,然后才能发送数据。在Microsoft.AspNet的Microsoft.AspNet.SignalR.Client.Connection.Send(字符串数据)中.SignalR.Client.Hubs.HubProxy.Invoke [T](String method,Object [] args)...“
I have _conn.Start() on my connection. 我的连接上有_conn.Start()。 If I try to Start() it a second time, right before invoke, it throws an exception. 如果我再次尝试Start()它,在调用之前,它会抛出异常。 Here is my code: 这是我的代码:
private IHubProxy _hub;
private HubConnection _conn;
public AddProductView()
{
InitializeComponent();
var url = Application.Current.Host.Source.GetComponents(UriComponents.Scheme | UriComponents.HostAndPort,
UriFormat.Unescaped);
_conn = new HubConnection(url);
_hub = _conn.CreateHubProxy("SilverlightPrism.Mvc.Services.ProductHub");
_hub.On<string>("NewMessage", message => Deployment.Current.Dispatcher.BeginInvoke(() => DoAddItem(message) ));
_conn.Start();
}
private void DoAddItem(string item)
{
var product = DeserializeToProduct(item);
ProductData.Products.Add(product);
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
Random random = new Random();
var id = Guid.NewGuid();
var product = new Product
{
Price = random.Next(1000,100000),
ProdId = id,
ProdName = "New prod."
};
var jsonMessage = SerializeToJsonString(product);
_hub.Invoke("SendMessage", jsonMessage);
}
It is throwing the exception on _hub.Invoke(); 它在_hub.Invoke()上抛出异常;
How do I correctly get the hub connect and send a message? 如何正确连接集线器并发送消息?
HubConnection.Start
is asynchronous. HubConnection.Start
是异步的。 You cannot call Start
right before Invoke
because your HubConnection
is probably still in the Connecting
state. 您无法在Invoke
之前Invoke
Start
,因为您的HubConnection
可能仍处于Connecting
状态。
You need to wait for the Task
returned from Start
to complete before you can call IHubProxy.Invoke
. 您需要等待从Start
返回的Task
完成,然后才能调用IHubProxy.Invoke
。 You can use await
(or Task.ContinueWith
if you aren't running .NET 4.5) to ensure Start
finishes before you enable buttonAdd
. 您可以使用await
(如果您没有运行.NET 4.5,则使用Task.ContinueWith
)以确保在启用buttonAdd
之前Start
完成。
You could also create your AddProductView
object asynchronously in a factory method. 您还可以在工厂方法中异步创建AddProductView
对象。 Instead of calling HubConnection.Start
in the constructor, you could do it in a static Task<AddProductView> CreateAddProductView()
method. 您可以在静态Task<AddProductView> CreateAddProductView()
方法中执行此操作,而不是在构造函数中调用HubConnection.Start
。
Alternatively, if you don't care about the creation of your AddProductView
being asynchronous, you can just call Start
synchronously: 或者,如果您不关心AddProductView
的创建是异步的,您只需同步调用Start
:
_conn.Start().Wait();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.