我创建了一个 WCF 服务,它使用异步模式异步执行冗长的操作。我参考了下面的链接以 在 ServiceContract中实现BeginAddNumbers
和方法。http://aspalliance.com/1335_Asynchronous_Pattern_in_Windows_Communication_Foundation.5
一切正常。但我不明白为什么我们需要这种方法? EndAddNumbers
即使它在服务器上异步操作,客户端仍然会阻塞,我们 也必须在客户端异步调用此操作。 因此,它总是在服务器上异步实现操作,而不是 最好在客户端异步调用操作以获得响应式 UI。
谁能帮我理解在服务器端实现异步操作的概念?有什么我需要一起玩的实际例子AsyncPattern=true
吗OperationContract
?
添加客户端代码。客户端使用 WPF 应用程序实现
private void button1_Click(object sender, RoutedEventArgs e)
{
MathOperationClient c = new MathOperationClient();
Task t = new Task(new Action(() =>
{
///Even if AddNumbers is is implemented as asynchronous operation
///second call to AddNumbers get chance only after completing below
///call.
///Note: AddNumbers method takes 10 sec to execute
int nResult = c.AddNumbers(2, 3);
this.Dispatcher.BeginInvoke(new Action(()=>{
label1.Content = nResult.ToString();
})
, null);
}));
t.Start();
Task t1 = new Task(new Action(() =>
{
///Below method is invoked only after executing first call ( above call with parameters 2 and 3 )
///in other words below call is blocked for 10 seconds.
///So what is advantage of implementing asynchronous AddNumbers method on server side?
int result = c.AddNumbers(5,5);
this.Dispatcher.BeginInvoke(new Action(() =>
{
label2.Content = result.ToString();
})
, null);
}));
t1.Start();
}
谢谢,赫曼特