如何等待/加入从Silverlight调用的WCF Web服务?
如果您像这样从Silverlight调用web服务:如何等待/加入从Silverlight调用的WCF Web服务?,wcf,silverlight,web-services,Wcf,Silverlight,Web Services,如果您像这样从Silverlight调用web服务: MyServiceClient serviceClient = new MyServiceClient(); void MyMethod() { serviceClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(serviceClient_GetDataCompleted); serviceClient.GetDataAsync();
MyServiceClient serviceClient = new MyServiceClient();
void MyMethod()
{
serviceClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(serviceClient_GetDataCompleted);
serviceClient.GetDataAsync();
// HOW DO I WAIT/JOIN HERE ON THE ASYNC CALL, RATHER THAN BEING FORCE TO LEAVE THIS METHOD?
}
MyServiceClient serviceClient=newmyserviceclient();
void MyMethod()
{
serviceClient.GetDataCompleted+=新事件处理程序(serviceClient\u GetDataCompleted);
serviceClient.GetDataAsync();
//如何在这里等待/加入异步调用,而不是被迫离开此方法?
}
我宁愿在“MyMethod”中等待/加入asych服务线程,也不愿在调用“GetDataAsync”后离开“MyMethod”,最好的方法是什么
谢谢,
Jeff要做到这一点,您需要在类中使用一个变量(类级别变量),然后等待它
void MyMethod()
{
wait = new ManualResetEvent(false);
// call your service
wait.WaitOne();
// finish working
}
在事件处理程序代码中
void serviceClient_GetDataCompleted(...)
{
// Set values you need from service
wait.Set();
}
要做到这一点,您需要在类中使用一个变量(类级别变量),然后等待它
void MyMethod()
{
wait = new ManualResetEvent(false);
// call your service
wait.WaitOne();
// finish working
}
在事件处理程序代码中
void serviceClient_GetDataCompleted(...)
{
// Set values you need from service
wait.Set();
}
您还可以使用lambda和闭包来获得类似的行为:
serviceClient.GetDataCompleted += (s,e) =>
{
// Your code here
};
serviceClient.GetDataAsync();
您还可以使用lambda和闭包来获得类似的行为:
serviceClient.GetDataCompleted += (s,e) =>
{
// Your code here
};
serviceClient.GetDataAsync();
不,你不能这样做。你会陷入僵局。GetDataCompleted由mainthreed调用。同样的三人在WaitOne等待。不,你不能这样做。你会陷入僵局。GetDataCompleted由mainthreed调用。同样的3D thait正在WaitOne中等待。如果您有一个基类提供构建WCF通道的机制,那么它可以用于构建异步调用的BeginX/EndX方法
public class ServiceFooCoordinator : CoordinatorBase<IServiceFoo>
{
public IAsyncResult BeginMethodFoo ()
{
IAsyncResult ar = null;
IServiceFoo channel = null;
channel = _factory.GetChannel();
Begin( channel, () => ar = channel.BeginMethodFoo( null, channel ) );
return ar;
}
public Bar[] EndMethodFoo ( IAsyncResult ar )
{
IServiceFoo channel = null;
channel = _factory.GetChannel();
return channel.EndMethodFoo( ar );
}
}
它以同步的方式获取异步调用。如果您有一个基类提供构建WCF通道的机制,那么它可以用于构建异步调用的BeginX/EndX方法
public class ServiceFooCoordinator : CoordinatorBase<IServiceFoo>
{
public IAsyncResult BeginMethodFoo ()
{
IAsyncResult ar = null;
IServiceFoo channel = null;
channel = _factory.GetChannel();
Begin( channel, () => ar = channel.BeginMethodFoo( null, channel ) );
return ar;
}
public Bar[] EndMethodFoo ( IAsyncResult ar )
{
IServiceFoo channel = null;
channel = _factory.GetChannel();
return channel.EndMethodFoo( ar );
}
}
它以同步的方式为您提供异步调用。我不得不问;为什么?关键是为用户提供流畅的体验,而等待web服务调用并不一定能做到这一点。我想您希望在加载Silverlight控件之前加载完整的内容块。在这种情况下,我会转向缓存内容,而不是强迫客户端无限期地等待;为什么?关键是为用户提供流畅的体验,而等待web服务调用并不一定能做到这一点。我想您希望在加载Silverlight控件之前加载完整的内容块。在这种情况下,我将转向缓存内容,而不是强迫客户端无限期地等待