【发布时间】:2013-03-23 05:35:14
【问题描述】:
我正在编写一个简单的 Silverlight 应用程序和 WCF 服务。 我想创建一个返回值的同步方法。 方法本身,从 WCF 服务调用异步方法。调用异步方法后,我想获取它的值,并返回给发送者。 听说Rx可以解决这类问题。
这是我的代码:
private void btnCreate_Click(object sender, RoutedEventArgs e)
{
string myResult = getMyBook(txtBookName.Text);
MessageBox.Show("Result\n" + myResult);
// myResult will be use for another purpose here..
}
// I want this method can be called anywhere, as long as the caller still in the same namespace.
public string getMyBook(string bookName)
{
Servo.ServoClient svc = new ServoClient();
string returnValue = "";
var o = Observable.FromEventPattern<GetBookCompletedEventArgs>(svc, "GetBookCompleted");
o.Subscribe(
b => returnValue = b.EventArgs.Result
);
svc.GetBookAsync(bookName);
return returnValue;
}
当我单击 btnCreate 时,myResult 变量仍然为空。我的代码有问题吗?或者也许我只是不了解 Rx 的概念?我是 Rx 的新手。
我的目标是:我需要从异步方法中获取结果(myResult变量),然后在后面的代码中使用。
【问题讨论】:
标签: c# wcf silverlight asynchronous system.reactive